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

<section class="panel">
    <div class="page-head">
        <div>
            <p class="eyebrow">Administracao</p>
            <h1>Suporte</h1>
            <p>Gerir tickets abertos por alunos, instrutores e instituicoes.</p>
        </div>
        <a class="ghost-button" href="/support/create">Abrir ticket interno</a>
    </div>

    <form method="get" action="/admin/support" class="filterbar report-filter">
        <label>Estado
            <select name="status">
                <option value="">Todos</option>
                <?php foreach ($statuses as $value => $label): ?>
                    <option value="<?= e($value) ?>" <?= $filters['status'] === $value ? 'selected' : '' ?>><?= e($label) ?></option>
                <?php endforeach; ?>
            </select>
        </label>
        <label>Categoria
            <select name="category">
                <option value="">Todas</option>
                <?php foreach ($categories as $value => $label): ?>
                    <option value="<?= e($value) ?>" <?= $filters['category'] === $value ? 'selected' : '' ?>><?= e($label) ?></option>
                <?php endforeach; ?>
            </select>
        </label>
        <label>Pesquisa
            <input name="search" placeholder="Ticket, aluno, assunto" value="<?= e($filters['search']) ?>">
        </label>
        <button class="button" type="submit">Filtrar</button>
    </form>

    <div class="table-wrap">
        <table>
            <thead><tr><th>Ticket</th><th>Utilizador</th><th>Categoria</th><th>Prioridade</th><th>Estado</th><th>Actualizado</th><th></th></tr></thead>
            <tbody>
                <?php if (!$tickets): ?><tr><td colspan="7">Nenhum ticket encontrado.</td></tr><?php endif; ?>
                <?php foreach ($tickets as $ticket): ?>
                    <tr>
                        <td><strong><?= e($ticket['subject']) ?></strong><span class="muted"><?= e($ticket['ticket_number']) ?></span></td>
                        <td><?= e($ticket['user_name']) ?><span class="muted"><?= e($ticket['user_email']) ?></span></td>
                        <td><?= e($categories[$ticket['category']] ?? $ticket['category']) ?></td>
                        <td><span class="badge"><?= e(ucfirst($ticket['priority'])) ?></span></td>
                        <td><span class="badge <?= e($ticket['status']) ?>"><?= e($statuses[$ticket['status']] ?? $ticket['status']) ?></span></td>
                        <td><?= e(format_date($ticket['updated_at'] ?? '')) ?></td>
                        <td><a class="mini-button" href="/admin/support/show?id=<?= e((string) $ticket['id']) ?>">Atender</a></td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</section>

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