<?php include app()->basePath('app/Views/partials/header.php'); ?>

<section class="panel">
    <p class="eyebrow">Administracao</p>
    <h1>Gestao de utilizadores</h1>
    <p>Controle alunos, instrutores, instituicoes e administradores da plataforma.</p>

    <div class="stats compact">
        <div class="stat">
            <span>Total</span>
            <strong><?= e((string) $stats['total']) ?></strong>
        </div>
        <div class="stat">
            <span>Alunos</span>
            <strong><?= e((string) $stats['students']) ?></strong>
        </div>
        <div class="stat">
            <span>Instrutores</span>
            <strong><?= e((string) $stats['instructors']) ?></strong>
        </div>
        <div class="stat">
            <span>Instituicoes</span>
            <strong><?= e((string) $stats['institutions']) ?></strong>
        </div>
        <div class="stat warning">
            <span>Pendentes</span>
            <strong><?= e((string) $stats['pending']) ?></strong>
        </div>
    </div>

    <form method="get" action="/admin/users" class="filterbar">
        <input name="search" placeholder="Pesquisar por nome, email ou telefone" value="<?= e($filters['search']) ?>">
        <select name="role">
            <option value="">Todos os perfis</option>
            <option value="student" <?= $filters['role'] === 'student' ? 'selected' : '' ?>>Alunos</option>
            <option value="instructor" <?= $filters['role'] === 'instructor' ? 'selected' : '' ?>>Instrutores</option>
            <option value="institution" <?= $filters['role'] === 'institution' ? 'selected' : '' ?>>Instituicoes</option>
            <option value="admin" <?= $filters['role'] === 'admin' ? 'selected' : '' ?>>Administradores</option>
        </select>
        <select name="status">
            <option value="">Todos os estados</option>
            <option value="pending" <?= $filters['status'] === 'pending' ? 'selected' : '' ?>>Pendentes</option>
            <option value="active" <?= $filters['status'] === 'active' ? 'selected' : '' ?>>Activos</option>
            <option value="suspended" <?= $filters['status'] === 'suspended' ? 'selected' : '' ?>>Suspensos</option>
            <option value="rejected" <?= $filters['status'] === 'rejected' ? 'selected' : '' ?>>Rejeitados</option>
        </select>
        <button class="button" type="submit">Filtrar</button>
    </form>

    <div class="table-wrap">
        <table>
            <thead>
                <tr>
                    <th>Nome</th>
                    <th>Perfil</th>
                    <th>Estado</th>
                    <th>Contacto</th>
                    <th>Criado em</th>
                    <th>Accoes</th>
                </tr>
            </thead>
            <tbody>
                <?php if (!$users): ?>
                    <tr>
                        <td colspan="6">Nenhum utilizador encontrado.</td>
                    </tr>
                <?php endif; ?>

                <?php foreach ($users as $item): ?>
                    <tr>
                        <td>
                            <strong><?= e($item['name']) ?></strong>
                            <span class="muted"><?= e($item['email']) ?></span>
                        </td>
                        <td><span class="badge"><?= e(role_label($item['role'])) ?></span></td>
                        <td><span class="badge <?= e($item['status']) ?>"><?= e(status_label($item['status'])) ?></span></td>
                        <td><?= e($item['phone'] ?: '-') ?></td>
                        <td><?= e(format_date($item['created_at'] ?? '')) ?></td>
                        <td class="row-actions">
                            <a class="mini-button" href="/admin/users/show?id=<?= e((string) $item['id']) ?>">Ver</a>

                            <?php if ($item['status'] === 'pending'): ?>
                                <form method="post" action="/admin/users/approve">
                                    <?= csrf_field() ?>
                                    <input type="hidden" name="user_id" value="<?= e((string) $item['id']) ?>">
                                    <button class="mini-button good" type="submit">Aprovar</button>
                                </form>
                                <form method="post" action="/admin/users/reject">
                                    <?= csrf_field() ?>
                                    <input type="hidden" name="user_id" value="<?= e((string) $item['id']) ?>">
                                    <button class="mini-button danger" type="submit">Rejeitar</button>
                                </form>
                            <?php elseif ($item['status'] === 'active' && $item['role'] !== 'admin'): ?>
                                <form method="post" action="/admin/users/suspend">
                                    <?= csrf_field() ?>
                                    <input type="hidden" name="user_id" value="<?= e((string) $item['id']) ?>">
                                    <button class="mini-button danger" type="submit">Suspender</button>
                                </form>
                            <?php elseif (in_array($item['status'], ['suspended', 'rejected'], true)): ?>
                                <form method="post" action="/admin/users/activate">
                                    <?= csrf_field() ?>
                                    <input type="hidden" name="user_id" value="<?= e((string) $item['id']) ?>">
                                    <button class="mini-button good" type="submit">Activar</button>
                                </form>
                            <?php endif; ?>
                        </td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</section>

<?php include app()->basePath('app/Views/partials/footer.php'); ?>
