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

<div class="admin-card mb-6">
    <div class="flex justify-between items-center mb-6">
        <h2 class="text-xl font-semibold">User Management</h2>
        <div class="flex gap-2">
            <a href="/admin/dashboard" class="admin-button secondary">
                <i class="fas fa-arrow-left"></i> Back to Dashboard
            </a>
        </div>
    </div>

    <div class="grid grid-cols-1 md:grid-cols-3 gap-4 mb-6">
        <div class="bg-dark p-4 rounded-md border border-gray-800">
            <div class="flex items-center justify-between">
                <div>
                    <h3 class="text-lg font-medium text-primary">Total Users</h3>
                    <p class="text-3xl font-bold mt-2"><?= $userStats['total_users'] ?></p>
                </div>
                <div class="text-4xl text-primary opacity-50">
                    <i class="fas fa-users"></i>
                </div>
            </div>
        </div>

        <div class="bg-dark p-4 rounded-md border border-gray-800">
            <div class="flex items-center justify-between">
                <div>
                    <h3 class="text-lg font-medium text-primary">Admins</h3>
                    <p class="text-3xl font-bold mt-2"><?= $userStats['total_admins'] ?></p>
                </div>
                <div class="text-4xl text-primary opacity-50">
                    <i class="fas fa-user-shield"></i>
                </div>
            </div>
        </div>

        <div class="bg-dark p-4 rounded-md border border-gray-800">
            <div class="flex items-center justify-between">
                <div>
                    <h3 class="text-lg font-medium text-primary">Regular Users</h3>
                    <p class="text-3xl font-bold mt-2"><?= $userStats['total_regular_users'] ?></p>
                </div>
                <div class="text-4xl text-primary opacity-50">
                    <i class="fas fa-user"></i>
                </div>
            </div>
        </div>
    </div>

    <div class="mb-6">
        <form action="/admin/users" method="GET" class="flex gap-2">
            <div class="flex-grow">
                <input type="text" name="search" placeholder="Search users..." value="<?= htmlspecialchars($search) ?>" class="w-full px-4 py-2 bg-darker border border-gray-800 rounded-md">
            </div>
            <button type="submit" class="admin-button">
                <i class="fas fa-search"></i> Search
            </button>
            <?php if (!empty($search)): ?>
            <a href="/admin/users" class="admin-button secondary">
                <i class="fas fa-times"></i> Clear
            </a>
            <?php endif; ?>
        </form>
    </div>

    <?php if (isset($_SESSION['admin_success'])): ?>
    <div class="bg-green-800 text-white p-4 rounded-md mb-6">
        <i class="fas fa-check-circle mr-2"></i> <?= $_SESSION['admin_success'] ?>
        <?php unset($_SESSION['admin_success']); ?>
    </div>
    <?php endif; ?>

    <?php if (isset($_SESSION['admin_error'])): ?>
    <div class="bg-red-800 text-white p-4 rounded-md mb-6">
        <i class="fas fa-exclamation-circle mr-2"></i> <?= $_SESSION['admin_error'] ?>
        <?php unset($_SESSION['admin_error']); ?>
    </div>
    <?php endif; ?>

    <div class="overflow-x-auto">
        <table class="w-full">
            <thead>
                <tr class="bg-dark">
                    <th class="p-3 text-left">ID</th>
                    <th class="p-3 text-left">Username</th>
                    <th class="p-3 text-left">Email</th>
                    <th class="p-3 text-left">Name</th>
                    <th class="p-3 text-left">Role</th>
                    <th class="p-3 text-left">Status</th>
                    <th class="p-3 text-left">Created</th>
                    <th class="p-3 text-left">Actions</th>
                </tr>
            </thead>
            <tbody>
                <?php if (empty($users)): ?>
                <tr>
                    <td colspan="8" class="p-3 text-center text-gray-400">No users found</td>
                </tr>
                <?php else: ?>
                <?php foreach ($users as $user): ?>
                <tr class="border-t border-gray-800 hover:bg-dark">
                    <td class="p-3"><?= $user['id'] ?></td>
                    <td class="p-3"><?= htmlspecialchars($user['username']) ?></td>
                    <td class="p-3"><?= htmlspecialchars($user['email']) ?></td>
                    <td class="p-3"><?= htmlspecialchars($user['name'] ?? '-') ?></td>
                    <td class="p-3">
                        <span class="px-2 py-1 rounded-full text-xs <?= $user['role'] === 'admin' ? 'bg-red-900 text-red-100' : 'bg-blue-900 text-blue-100' ?>">
                            <?= ucfirst($user['role']) ?>
                        </span>
                    </td>
                    <td class="p-3">
                        <?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; ?>

                        <?php if (!empty($user['google_id'])): ?>
                        <span class="ml-1 px-2 py-1 rounded-full text-xs bg-blue-900 text-blue-100" title="Google Account">
                            <i class="fab fa-google"></i>
                        </span>
                        <?php endif; ?>
                    </td>
                    <td class="p-3"><?= date('M d, Y', strtotime($user['created_at'])) ?></td>
                    <td class="p-3">
                        <div class="flex gap-2">
                            <a href="/admin/edit-user?id=<?= $user['id'] ?>" class="admin-button small">
                                <i class="fas fa-edit"></i> Edit
                            </a>
                            <?php if ($user['role'] !== 'admin'): ?>
                            <form action="/admin/delete-user?id=<?= $user['id'] ?>" method="POST" onsubmit="return confirm('Are you sure you want to delete this user?');" class="inline">
                                <button type="submit" class="admin-button small danger">
                                    <i class="fas fa-trash"></i> Delete
                                </button>
                            </form>
                            <?php endif; ?>
                        </div>
                    </td>
                </tr>
                <?php endforeach; ?>
                <?php endif; ?>
            </tbody>
        </table>
    </div>

    <?php if ($totalPages > 1): ?>
    <div class="mt-6 flex justify-center">
        <div class="flex gap-2">
            <?php if ($currentPage > 1): ?>
            <a href="/admin/users?page=<?= $currentPage - 1 ?><?= !empty($search) ? '&search=' . urlencode($search) : '' ?>" class="admin-button small secondary">
                <i class="fas fa-chevron-left"></i> Previous
            </a>
            <?php endif; ?>

            <?php for ($i = max(1, $currentPage - 2); $i <= min($totalPages, $currentPage + 2); $i++): ?>
            <a href="/admin/users?page=<?= $i ?><?= !empty($search) ? '&search=' . urlencode($search) : '' ?>" class="admin-button small <?= $i === $currentPage ? 'primary' : 'secondary' ?>">
                <?= $i ?>
            </a>
            <?php endfor; ?>

            <?php if ($currentPage < $totalPages): ?>
            <a href="/admin/users?page=<?= $currentPage + 1 ?><?= !empty($search) ? '&search=' . urlencode($search) : '' ?>" class="admin-button small secondary">
                Next <i class="fas fa-chevron-right"></i>
            </a>
            <?php endif; ?>
        </div>
    </div>
    <?php endif; ?>
</div>

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