<?php
require_once '../config/database.php';
secureSessionStart();
requireLogin();

header('Content-Type: application/json');

$conn = getConnection();
$admin_id = $_SESSION['admin_user'];

$user_id = mysqli_real_escape_string($conn, $_POST['user_id']);
$name = mysqli_real_escape_string($conn, $_POST['name']);
$mobile = mysqli_real_escape_string($conn, $_POST['mobile']);
$email = mysqli_real_escape_string($conn, $_POST['email']);
$dob = mysqli_real_escape_string($conn, $_POST['dob']);
$gender = intval($_POST['gender']);
$panno = mysqli_real_escape_string($conn, $_POST['panno']);
$aadharno = mysqli_real_escape_string($conn, $_POST['aadharno']);
$wallet = floatval($_POST['wallet']);
$rank = mysqli_real_escape_string($conn, $_POST['rank']);
$status = intval($_POST['status']);

$query = "UPDATE users SET 
          name = '$name',
          mobile = '$mobile',
          email = '$email',
          dob = '$dob',
          gender = '$gender',
          panno = '$panno',
          aadharno = '$aadharno',
          wallet = '$wallet',
          rank = '$rank',
          status = '$status'
          WHERE user_id = '$user_id'";

if (mysqli_query($conn, $query)) {
    // Log the action
    $log = "INSERT INTO admin_logs (admin_id, action, details, created_at) 
            VALUES ('$admin_id', 'update_user', 'Updated user: $user_id', NOW())";
    mysqli_query($conn, $log);
    
    echo json_encode(['success' => true]);
} else {
    echo json_encode(['success' => false, 'error' => mysqli_error($conn)]);
}
exit();
?>