<?php
session_start();
include('../includes/config.php');

$id = isset($_GET['id']) ? intval($_GET['id']) : 0;

$sql = "SELECT * FROM users WHERE id = $id";
$result = mysqli_query($conn, $sql);

if ($row = mysqli_fetch_assoc($result)) {
    $username = htmlspecialchars($row['username']);
    $email = htmlspecialchars($row['email']);
    $first_name = htmlspecialchars($row['first_name']);
    $last_name = htmlspecialchars($row['last_name']);
    $id_number = htmlspecialchars($row['id_number']);
    $phone = htmlspecialchars($row['phone']);
    $date_of_birth = $row['date_of_birth'];
    $address = htmlspecialchars($row['address']);
    $city = htmlspecialchars($row['city']);
    $province = htmlspecialchars($row['province']);
    $postal_code = htmlspecialchars($row['postal_code']);
    $status = $row['status'];
    $fund_balance = $row['fund_balance'];
    $available_balance = $row['available_balance'];
    $total_profit = $row['total_profit'];
    
    echo "<form id='editUserForm' method='POST'>";
    echo "<div class='modal-body'>";
    echo "<div class='row'>";
    echo "<div class='col-md-6'>";
    echo "<div class='form-group'>";
    echo "<label>First Name</label>";
    echo "<input type='text' class='form-control' name='first_name' value='$first_name' required>";
    echo "</div>";
    echo "</div>";
    echo "<div class='col-md-6'>";
    echo "<div class='form-group'>";
    echo "<label>Last Name</label>";
    echo "<input type='text' class='form-control' name='last_name' value='$last_name' required>";
    echo "</div>";
    echo "</div>";
    echo "</div>";
    echo "<div class='row'>";
    echo "<div class='col-md-6'>";
    echo "<div class='form-group'>";
    echo "<label>Username</label>";
    echo "<input type='text' class='form-control' name='username' value='$username' required>";
    echo "</div>";
    echo "</div>";
    echo "<div class='col-md-6'>";
    echo "<div class='form-group'>";
    echo "<label>Email</label>";
    echo "<input type='email' class='form-control' name='email' value='$email' required>";
    echo "</div>";
    echo "</div>";
    echo "</div>";
    echo "<div class='row'>";
    echo "<div class='col-md-6'>";
    echo "<div class='form-group'>";
    echo "<label>ID Number</label>";
    echo "<input type='text' class='form-control' name='id_number' value='$id_number'>";
    echo "</div>";
    echo "</div>";
    echo "<div class='col-md-6'>";
    echo "<div class='form-group'>";
    echo "<label>Phone</label>";
    echo "<input type='text' class='form-control' name='phone' value='$phone' required>";
    echo "</div>";
    echo "</div>";
    echo "</div>";
    echo "<div class='row'>";
    echo "<div class='col-md-6'>";
    echo "<div class='form-group'>";
    echo "<label>Date of Birth</label>";
    echo "<input type='date' class='form-control' name='date_of_birth' value='$date_of_birth'>";
    echo "</div>";
    echo "</div>";
    echo "<div class='col-md-6'>";
    echo "<div class='form-group'>";
    echo "<label>Status</label>";
    echo "<select class='form-control' name='status'>";
    echo "<option value='verified' " . ($status == 'verified' ? 'selected' : '') . ">Verified</option>";
    echo "<option value='pending' " . ($status == 'pending' ? 'selected' : '') . ">Pending</option>";
    echo "<option value='rejected' " . ($status == 'rejected' ? 'selected' : '') . ">Rejected</option>";
    echo "</select>";
    echo "</div>";
    echo "</div>";
    echo "</div>";
    echo "<div class='row'>";
    echo "<div class='col-md-12'>";
    echo "<div class='form-group'>";
    echo "<label>Address</label>";
    echo "<textarea class='form-control' name='address' rows='2'>$address</textarea>";
    echo "</div>";
    echo "</div>";
    echo "</div>";
    echo "<div class='row'>";
    echo "<div class='col-md-4'>";
    echo "<div class='form-group'>";
    echo "<label>City</label>";
    echo "<input type='text' class='form-control' name='city' value='$city'>";
    echo "</div>";
    echo "</div>";
    echo "<div class='col-md-4'>";
    echo "<div class='form-group'>";
    echo "<label>Province</label>";
    echo "<input type='text' class='form-control' name='province' value='$province'>";
    echo "</div>";
    echo "</div>";
    echo "<div class='col-md-4'>";
    echo "<div class='form-group'>";
    echo "<label>Postal Code</label>";
    echo "<input type='text' class='form-control' name='postal_code' value='$postal_code'>";
    echo "</div>";
    echo "</div>";
    echo "</div>";
    echo "<div class='row'>";
    echo "<div class='col-md-4'>";
    echo "<div class='form-group'>";
    echo "<label>Fund Balance ($)</label>";
    echo "<input type='number' class='form-control' name='fund_balance' step='0.01' value='$fund_balance'>";
    echo "</div>";
    echo "</div>";
    echo "<div class='col-md-4'>";
    echo "<div class='form-group'>";
    echo "<label>Available Balance ($)</label>";
    echo "<input type='number' class='form-control' name='available_balance' step='0.01' value='$available_balance'>";
    echo "</div>";
    echo "</div>";
    echo "<div class='col-md-4'>";
    echo "<div class='form-group'>";
    echo "<label>Total Profit ($)</label>";
    echo "<input type='number' class='form-control' name='total_profit' step='0.01' value='$total_profit'>";
    echo "</div>";
    echo "</div>";
    echo "</div>";
    echo "</div>";
    echo "<div class='modal-footer'>";
    echo "<button type='button' class='btn btn-secondary' data-bs-dismiss='modal'>Cancel</button>";
    echo "<button type='submit' class='btn btn-primary'>Update User</button>";
    echo "</div>";
    echo "</form>";
} else {
    echo "<div class='alert alert-danger'>User not found</div>";
}
?>