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

$conn = getConnection();
$user_id = isset($_GET['user_id']) ? mysqli_real_escape_string($conn, $_GET['user_id']) : 0;

if (!$user_id) {
    exit('No user ID provided');
}

// Get user details
$query = "SELECT * FROM users WHERE user_id = '$user_id'";
$result = mysqli_query($conn, $query);
$user = mysqli_fetch_assoc($result);

if (!$user) {
    exit('User not found');
}
?>

<form class="edit-form" id="editUserForm">
    <input type="hidden" name="user_id" value="<?php echo $user['user_id']; ?>">
    
    <div class="form-group">
        <label>Full Name</label>
        <input type="text" class="form-control" name="name" value="<?php echo htmlspecialchars($user['name'] ?? ''); ?>">
    </div>
    
    <div class="form-group">
        <label>Mobile Number</label>
        <input type="text" class="form-control" name="mobile" value="<?php echo $user['mobile'] ?? ''; ?>">
    </div>
    
    <div class="form-group">
        <label>Email Address</label>
        <input type="email" class="form-control" name="email" value="<?php echo htmlspecialchars($user['email'] ?? ''); ?>">
    </div>
    
    <div class="form-group">
        <label>Date of Birth</label>
        <input type="date" class="form-control" name="dob" value="<?php echo $user['dob'] ?? ''; ?>">
    </div>
    
    <div class="form-group">
        <label>Gender</label>
        <select class="form-control" name="gender">
            <option value="0" <?php echo ($user['gender'] ?? '') == 0 ? 'selected' : ''; ?>>Male</option>
            <option value="1" <?php echo ($user['gender'] ?? '') == 1 ? 'selected' : ''; ?>>Female</option>
            <option value="2" <?php echo ($user['gender'] ?? '') == 2 ? 'selected' : ''; ?>>Other</option>
        </select>
    </div>
    
    <div class="form-group">
        <label>PAN Number</label>
        <input type="text" class="form-control" name="panno" value="<?php echo $user['panno'] ?? ''; ?>">
    </div>
    
    <div class="form-group">
        <label>Aadhaar Number</label>
        <input type="text" class="form-control" name="aadharno" value="<?php echo $user['aadharno'] ?? ''; ?>">
    </div>
    
    <div class="form-group">
        <label>Wallet Balance</label>
        <input type="number" step="0.01" class="form-control" name="wallet" value="<?php echo $user['wallet'] ?? 0; ?>">
    </div>
    
    <div class="form-group">
        <label>Rank</label>
        <input type="text" class="form-control" name="rank" value="<?php echo htmlspecialchars($user['rank'] ?? ''); ?>">
    </div>
    
    <div class="form-group">
        <label>Status</label>
        <select class="form-control" name="status">
            <option value="1" <?php echo ($user['status'] ?? 1) == 1 ? 'selected' : ''; ?>>Active</option>
            <option value="0" <?php echo ($user['status'] ?? 1) == 0 ? 'selected' : ''; ?>>Inactive</option>
        </select>
    </div>
    
    <button type="submit" class="save-btn">
        <i class="fas fa-save me-2"></i>Save Changes
    </button>
</form>