<?php
$pageTitle = 'Edit User';
ob_start();
?>

<div class="admin-card mb-6">
    <div class="flex justify-between items-center mb-6">
        <h2 class="text-xl font-semibold">Edit User: <?= htmlspecialchars($user['username']) ?></h2>
        <div class="flex gap-2">
            <a href="/admin/users" class="admin-button secondary">
                <i class="fas fa-arrow-left"></i> Back to Users
            </a>
        </div>
    </div>

    <?php if ($success): ?>
    <div class="bg-green-800 text-white p-4 rounded-md mb-6">
        <i class="fas fa-check-circle mr-2"></i> User updated successfully!
    </div>
    <?php endif; ?>

    <?php if (!empty($error)): ?>
    <div class="bg-red-800 text-white p-4 rounded-md mb-6">
        <i class="fas fa-exclamation-circle mr-2"></i> <?= $error ?>
    </div>
    <?php endif; ?>

    <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
        <!-- User Information -->
        <div class="bg-dark p-6 rounded-md border border-gray-800">
            <h3 class="text-lg font-medium mb-4 text-primary">User Information</h3>

            <form action="/admin/edit-user?id=<?= $user['id'] ?>" method="POST" class="space-y-4">
                <input type="hidden" name="action" value="update_user">

                <div>
                    <label for="username" class="block text-sm font-medium text-gray-300 mb-1">Username</label>
                    <input type="text" id="username" name="username" value="<?= htmlspecialchars($user['username']) ?>" required class="w-full px-4 py-2 bg-darker border border-gray-800 rounded-md">
                </div>

                <div>
                    <label for="email" class="block text-sm font-medium text-gray-300 mb-1">Email</label>
                    <input type="email" id="email" name="email" value="<?= htmlspecialchars($user['email']) ?>" required class="w-full px-4 py-2 bg-darker border border-gray-800 rounded-md">
                </div>

                <div>
                    <label for="name" class="block text-sm font-medium text-gray-300 mb-1">Name</label>
                    <input type="text" id="name" name="name" value="<?= htmlspecialchars($user['name'] ?? '') ?>" class="w-full px-4 py-2 bg-darker border border-gray-800 rounded-md">
                </div>

                <!-- Role dropdown hidden -->
                <input type="hidden" name="role" value="<?= htmlspecialchars($user['role']) ?>">

                <div class="flex items-center">
                    <input type="checkbox" id="is_active" name="is_active" value="1" <?= $user['is_active'] == 1 ? 'checked' : '' ?> class="mr-2">
                    <label for="is_active" class="text-sm font-medium text-gray-300">Active</label>
                </div>

                <div class="flex items-center">
                    <input type="checkbox" id="email_verified" name="email_verified" value="1" <?= $user['email_verified'] == 1 ? 'checked' : '' ?> class="mr-2">
                    <label for="email_verified" class="text-sm font-medium text-gray-300">Email Verified</label>
                </div>

                <div>
                    <button type="submit" class="admin-button">
                        <i class="fas fa-save"></i> Update User
                    </button>
                </div>
            </form>
        </div>

        <!-- Change Password -->
        <div class="bg-dark p-6 rounded-md border border-gray-800">
            <h3 class="text-lg font-medium mb-4 text-primary">Change Password</h3>

            <form action="/admin/edit-user?id=<?= $user['id'] ?>" method="POST" class="space-y-4">
                <input type="hidden" name="action" value="update_password">

                <div>
                    <label for="password" class="block text-sm font-medium text-gray-300 mb-1">New Password</label>
                    <input type="password" id="password" name="password" required class="w-full px-4 py-2 bg-darker border border-gray-800 rounded-md">
                </div>

                <div>
                    <label for="confirm_password" class="block text-sm font-medium text-gray-300 mb-1">Confirm New Password</label>
                    <input type="password" id="confirm_password" name="confirm_password" required class="w-full px-4 py-2 bg-darker border border-gray-800 rounded-md">
                </div>

                <div>
                    <button type="submit" class="admin-button">
                        <i class="fas fa-key"></i> Change Password
                    </button>
                </div>
            </form>
        </div>

        <!-- Points Management -->
        <?php if (($settings['enable_points_system'] ?? '0') == '1'): ?>
        <div class="bg-dark p-6 rounded-md border border-gray-800">
            <h3 class="text-lg font-medium mb-4 text-primary">Points Management</h3>

            <div class="mb-4">
                <p class="text-sm text-gray-400 mb-2">Current Balance</p>
                <p class="text-2xl font-bold text-yellow-400"><?= number_format($userCoins['balance'] ?? 0) ?> points</p>
                <p class="text-sm text-gray-400">Total Earned: <?= number_format($userCoins['total_earned'] ?? 0) ?> points</p>
            </div>

            <form action="/admin/edit-user?id=<?= $user['id'] ?>" method="POST" class="space-y-4">
                <input type="hidden" name="action" value="update_points">

                <div>
                    <label for="points_balance" class="block text-sm font-medium text-gray-300 mb-1">Set New Balance</label>
                    <input type="number" id="points_balance" name="points_balance" value="<?= $userCoins['balance'] ?? 0 ?>" min="0" required class="w-full px-4 py-2 bg-darker border border-gray-800 rounded-md">
                    <p class="text-sm text-gray-400 mt-1">Set the user's current points balance</p>
                </div>

                <div>
                    <button type="submit" class="admin-button">
                        <i class="fas fa-coins"></i> Update Points
                    </button>
                </div>
            </form>
        </div>
        <?php endif; ?>
    </div>

    <!-- User Details -->
    <div class="mt-6 bg-dark p-6 rounded-md border border-gray-800">
        <h3 class="text-lg font-medium mb-4 text-primary">User Details</h3>

        <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
            <div>
                <p class="text-sm text-gray-400">User ID</p>
                <p class="font-medium"><?= $user['id'] ?></p>
            </div>

            <div>
                <p class="text-sm text-gray-400">Created At</p>
                <p class="font-medium"><?= date('F j, Y, g:i a', strtotime($user['created_at'])) ?></p>
            </div>

            <div>
                <p class="text-sm text-gray-400">Last Updated</p>
                <p class="font-medium"><?= date('F j, Y, g:i a', strtotime($user['updated_at'] ?? $user['created_at'])) ?></p>
            </div>

            <div>
                <p class="text-sm text-gray-400">Status</p>
                <p class="font-medium">
                    <?php if ($user['is_active'] == 1): ?>
                    <span class="px-2 py-1 rounded-full text-xs bg-green-900 text-green-100">Active</span>
                    <?php else: ?>
                    <span class="px-2 py-1 rounded-full text-xs bg-gray-800 text-gray-300">Inactive</span>
                    <?php endif; ?>
                </p>
            </div>

            <div>
                <p class="text-sm text-gray-400">Authentication Method</p>
                <p class="font-medium">
                    <?php if (!empty($user['google_id'])): ?>
                    <span class="px-2 py-1 rounded-full text-xs bg-blue-900 text-blue-100">
                        <i class="fab fa-google mr-1"></i> Google Account
                    </span>
                    <?php else: ?>
                    <span class="px-2 py-1 rounded-full text-xs bg-gray-800 text-gray-300">
                        <i class="fas fa-key mr-1"></i> Password
                    </span>
                    <?php endif; ?>
                </p>
            </div>

            <?php if (($settings['enable_points_system'] ?? '0') == '1' && $userCoins): ?>
            <div>
                <p class="text-sm text-gray-400">Points Balance</p>
                <p class="font-medium text-yellow-400">
                    <i class="fas fa-coins mr-1"></i> <?= number_format($userCoins['balance'] ?? 0) ?> points
                </p>
            </div>

            <div>
                <p class="text-sm text-gray-400">Total Points Earned</p>
                <p class="font-medium"><?= number_format($userCoins['total_earned'] ?? 0) ?> points</p>
            </div>
            <?php endif; ?>
        </div>
    </div>
</div>

<?php
$content = ob_get_clean();
include __DIR__ . '/layout.php';
?>
