<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
session_start();

// Redirect to login if the user is not authenticated
if (!isset($_SESSION['admin_id'])) {
    header("Location: index.php");
    exit();
}

// Include Database Connection
require_once "../account/include/db.php";

$message = "";
$user_id = isset($_GET['id']) ? (int)$_GET['id'] : 0;

// --- UPDATE LOGIC ---
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['update_user'])) {
    $full_name = mysqli_real_escape_string($conn, $_POST['full_name']);
    $email = mysqli_real_escape_string($conn, $_POST['email']);
    $balance = mysqli_real_escape_string($conn, $_POST['balance']);
    $country = mysqli_real_escape_string($conn, $_POST['country']);
    $phone = mysqli_real_escape_string($conn, $_POST['phone']);
    $transaction_pin = mysqli_real_escape_string($conn, $_POST['transaction_pin']);
    $status = mysqli_real_escape_string($conn, $_POST['status']);
    $notice = mysqli_real_escape_string($conn, $_POST['notice']);

    $update_query = "UPDATE `users` SET 
                    `full_name` = '$full_name', 
                    `email` = '$email', 
                    `balance` = '$balance', 
                    `country` = '$country', 
                    `phone` = '$phone', 
                    `transaction_pin` = '$transaction_pin',
                    `status` = '$status',
                    `notice` = '$notice'
                    WHERE `id` = $user_id";

    if (mysqli_query($conn, $update_query)) {
        $message = "<div class='bg-emerald-500/10 text-emerald-400 p-4 rounded-lg border border-emerald-500/20 mb-6 flex items-center gap-2'>
                        <svg class='w-5 h-5' fill='none' stroke='currentColor' viewBox='0 0 24 24'><path stroke-linecap='round' stroke-linejoin='round' stroke-width='2' d='M5 13l4 4L19 7'></path></svg>
                        User profile and notice updated successfully.
                    </div>";
    } else {
        $message = "<div class='bg-red-500/10 text-red-400 p-4 rounded-lg border border-red-500/20 mb-6'>Error updating record: " . mysqli_error($conn) . "</div>";
    }
}

// --- FETCH CURRENT DATA ---
$query = "SELECT * FROM `users` WHERE `id` = $user_id";
$result = mysqli_query($conn, $query);
$user = mysqli_fetch_assoc($result);

if (!$user) {
    die("User not found.");
}

// Include Header and Sidebar
include 'includes/header.php';
include 'includes/sidebar.php';
?>

<style>
    body { font-family: 'Inter', system-ui, -apple-system, sans-serif !important; }
    .form-input {
        background-color: #0f172a;
        border: 1px solid #1e293b;
        color: #f1f5f9;
        transition: all 0.2s;
    }
    .form-input:focus {
        border-color: #3b82f6;
        outline: none;
        box-shadow: 0 0 0 2px rgba(59, 130, 246, 0.1);
    }
    select.form-input {
        appearance: none;
        background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 24 24' stroke='%2364748b'%3E%3Cpath stroke-linecap='round' stroke-linejoin='round' stroke-width='2' d='M19 9l-7 7-7-7'%3E%3C/path%3E%3C/svg%3E");
        background-repeat: no-repeat;
        background-position: right 1rem center;
        background-size: 1em;
    }
</style>

<main class="md:ml-64 p-6 min-h-[calc(100vh-65px)] flex flex-col relative z-10 bg-slate-950">
    
    <div class="mb-8 flex justify-between items-center">
        <div>
            <h1 class="text-3xl font-bold text-white tracking-tight">Edit User</h1>
            <p class="text-slate-400 mt-1 text-sm">Modifying account details for @<?php echo htmlspecialchars($user['username']); ?></p>
        </div>
        <a href="manage_users.php" class="text-xs bg-slate-800 hover:bg-slate-700 text-slate-200 px-4 py-2 rounded-lg border border-slate-700 transition-all">
            Back to Directory
        </a>
    </div>

    <?php echo $message; ?>

    <div class="bg-slate-900 border border-slate-800 rounded-xl p-8 max-w-4xl shadow-2xl">
        <form action="" method="POST" class="grid grid-cols-1 md:grid-cols-2 gap-6">
            
            <div class="flex flex-col gap-2">
                <label class="text-xs font-semibold text-slate-500 uppercase tracking-wider">Full Name</label>
                <input type="text" name="full_name" value="<?php echo htmlspecialchars($user['full_name']); ?>" class="form-input p-3 rounded-lg text-sm" required>
            </div>

            <div class="flex flex-col gap-2">
                <label class="text-xs font-semibold text-slate-500 uppercase tracking-wider">Email Address</label>
                <input type="email" name="email" value="<?php echo htmlspecialchars($user['email']); ?>" class="form-input p-3 rounded-lg text-sm" required>
            </div>

            <div class="flex flex-col gap-2">
                <label class="text-xs font-semibold text-slate-500 uppercase tracking-wider">Phone Number</label>
                <input type="text" name="phone" value="<?php echo htmlspecialchars($user['phone']); ?>" class="form-input p-3 rounded-lg text-sm">
            </div>

            <div class="flex flex-col gap-2">
                <label class="text-xs font-semibold text-slate-500 uppercase tracking-wider">Country</label>
                <input type="text" name="country" value="<?php echo htmlspecialchars($user['country']); ?>" class="form-input p-3 rounded-lg text-sm">
            </div>

            <div class="flex flex-col gap-2">
                <label class="text-xs font-semibold text-slate-500 uppercase tracking-wider">Account Balance (USD)</label>
                <div class="relative">
                    <span class="absolute left-3 top-3 text-slate-500 text-sm">$</span>
                    <input type="number" step="0.01" name="balance" value="<?php echo htmlspecialchars($user['balance']); ?>" class="form-input p-3 pl-7 rounded-lg text-sm w-full" required>
                </div>
            </div>

            <div class="flex flex-col gap-2">
                <label class="text-xs font-semibold text-blue-400 uppercase tracking-wider">Transaction PIN</label>
                <input type="text" name="transaction_pin" value="<?php echo htmlspecialchars($user['transaction_pin']); ?>" class="form-input p-3 rounded-lg text-sm font-mono border-blue-900/30" maxlength="6">
            </div>

            <div class="flex flex-col gap-2">
                <label class="text-xs font-semibold text-slate-500 uppercase tracking-wider">Account Status</label>
                <select name="status" class="form-input p-3 rounded-lg text-sm">
                    <option value="active" <?php echo ($user['status'] == 'active') ? 'selected' : ''; ?>>Active</option>
                    <option value="suspended" <?php echo ($user['status'] == 'suspended') ? 'selected' : ''; ?>>Suspended</option>
                    <option value="pending" <?php echo ($user['status'] == 'pending') ? 'selected' : ''; ?>>Pending Verification</option>
                </select>
            </div>

            <div class="flex flex-col gap-2 md:col-span-2">
                <label class="text-xs font-semibold text-amber-400 uppercase tracking-wider">Account Notice / Dashboard Alert</label>
                <textarea name="notice" rows="3" class="form-input p-3 rounded-lg text-sm resize-none" placeholder="Enter a custom notice to display on the user's dashboard..."><?php echo htmlspecialchars($user['notice']); ?></textarea>
                <p class="text-[10px] text-slate-500 italic">This message will be visible to the user upon login.</p>
            </div>

            <div class="md:col-span-2 pt-4 border-t border-slate-800 mt-4 flex justify-end gap-4">
                <button type="reset" class="px-6 py-2.5 text-sm text-slate-400 hover:text-white transition-colors">
                    Reset Changes
                </button>
                <button type="submit" name="update_user" class="bg-blue-600 hover:bg-blue-500 text-white px-8 py-2.5 rounded-lg text-sm font-semibold transition-all shadow-lg shadow-blue-900/20">
                    Save User Details
                </button>
            </div>
        </form>
    </div>

</main>

<?php include 'includes/footer.php'; ?>