<?php $pageTitle = Lang::get('manage_users') . ' - ' . Lang::get('app_name'); ?>
<?php require APP_ROOT . '/views/layouts/header.php'; ?>

<div class="mb-6 flex items-center justify-between">
    <h1 class="text-2xl font-bold"><?= Lang::get('manage_users') ?></h1>
    <a href="<?= APP_URL ?>/admin" class="flex items-center px-4 py-2 bg-gray-200 dark:bg-gray-700 hover:bg-gray-300 dark:hover:bg-gray-600 text-gray-700 dark:text-gray-300 text-sm font-medium rounded-lg transition-colors">
        <svg class="w-4 h-4 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 19l-7-7m0 0l7-7m-7 7h18"/></svg>
        <?= Lang::get('back') ?>
    </a>
</div>

<div class="bg-white dark:bg-gray-800 rounded-xl border border-gray-200 dark:border-gray-700 overflow-hidden">
    <div class="overflow-x-auto">
        <table class="w-full text-sm text-left">
            <thead class="bg-gray-50 dark:bg-gray-700 text-gray-600 dark:text-gray-300 uppercase text-xs">
                <tr>
                    <th class="px-4 py-3">ID</th>
                    <th class="px-4 py-3"><?= Lang::get('username') ?></th>
                    <th class="px-4 py-3"><?= Lang::get('display_name') ?></th>
                    <th class="px-4 py-3"><?= Lang::get('email') ?></th>
                    <th class="px-4 py-3"><?= Lang::get('role') ?></th>
                    <th class="px-4 py-3"><?= Lang::get('status') ?></th>
                    <th class="px-4 py-3"><?= Lang::get('created_at') ?></th>
                    <th class="px-4 py-3"><?= Lang::get('actions') ?></th>
                </tr>
            </thead>
            <tbody class="divide-y divide-gray-200 dark:divide-gray-700">
                <?php foreach ($users as $u): ?>
                <tr class="hover:bg-gray-50 dark:hover:bg-gray-750">
                    <td class="px-4 py-3 text-gray-900 dark:text-gray-100"><?= $u['id'] ?></td>
                    <td class="px-4 py-3 text-gray-900 dark:text-gray-100 font-medium"><?= htmlspecialchars($u['username']) ?></td>
                    <td class="px-4 py-3 text-gray-700 dark:text-gray-300"><?= htmlspecialchars($u['display_name'] ?? '') ?></td>
                    <td class="px-4 py-3 text-gray-700 dark:text-gray-300"><?= htmlspecialchars($u['email'] ?? '') ?></td>
                    <td class="px-4 py-3">
                        <span class="px-2 py-1 text-xs font-medium rounded-full <?= $u['role'] === 'admin' ? 'bg-purple-100 text-purple-700 dark:bg-purple-900/30 dark:text-purple-400' : 'bg-gray-100 text-gray-700 dark:bg-gray-600 dark:text-gray-300' ?>">
                            <?= ucfirst($u['role']) ?>
                        </span>
                    </td>
                    <td class="px-4 py-3">
                        <?php
                            $statusColors = [
                                'approved'             => 'bg-green-100 text-green-700 dark:bg-green-900/30 dark:text-green-400',
                                'pending'              => 'bg-yellow-100 text-yellow-700 dark:bg-yellow-900/30 dark:text-yellow-400',
                                'pending_verification' => 'bg-blue-100 text-blue-700 dark:bg-blue-900/30 dark:text-blue-400',
                                'rejected'             => 'bg-red-100 text-red-700 dark:bg-red-900/30 dark:text-red-400',
                            ];
                            $color = $statusColors[$u['status']] ?? 'bg-gray-100 text-gray-700';
                        ?>
                        <span class="px-2 py-1 text-xs font-medium rounded-full <?= $color ?>">
                            <?= Lang::get('status_' . $u['status']) ?>
                        </span>
                    </td>
                    <td class="px-4 py-3 text-gray-500 dark:text-gray-400 text-xs"><?= $u['created_at'] ?></td>
                    <td class="px-4 py-3">
                        <div class="flex items-center space-x-1">
                            <?php if ($u['status'] === 'pending'): ?>
                                <a href="<?= APP_URL ?>/admin/users/<?= $u['id'] ?>/approve" class="p-1 text-green-500 hover:text-green-700 transition-colors" title="<?= Lang::get('approve') ?>">
                                    <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7"/></svg>
                                </a>
                                <a href="<?= APP_URL ?>/admin/users/<?= $u['id'] ?>/reject" class="p-1 text-red-500 hover:text-red-700 transition-colors" title="<?= Lang::get('reject') ?>">
                                    <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"/></svg>
                                </a>
                            <?php endif; ?>
                            <a href="<?= APP_URL ?>/admin/users/<?= $u['id'] ?>/edit" class="p-1 text-gray-400 hover:text-primary-600 dark:hover:text-primary-400 transition-colors" title="<?= Lang::get('edit') ?>">
                                <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z"/></svg>
                            </a>
                            <?php if ($u['id'] !== Session::getUserId()): ?>
                                <a href="javascript:void(0)" onclick="confirmDelete('<?= Lang::get('confirm_delete') ?>', function(){ window.location.href='<?= APP_URL ?>/admin/users/<?= $u['id'] ?>/delete'; })" class="p-1 text-gray-400 hover:text-red-600 dark:hover:text-red-400 transition-colors" title="<?= Lang::get('delete') ?>">
                                    <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"/></svg>
                                </a>
                            <?php endif; ?>
                        </div>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>

<?php require APP_ROOT . '/views/layouts/footer.php'; ?>
