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

<section class="panel">
    <p class="eyebrow">Administracao</p>
    <h1>Pagamentos manuais</h1>
    <p>Aprove comprovativos e libere automaticamente o acesso ao curso.</p>

    <form method="get" action="/admin/payments" class="filterbar simple">
        <input name="search" placeholder="Pesquisar aluno, curso ou referencia" value="<?= e($filters['search']) ?>">
        <select name="status">
            <option value="">Todos os estados</option>
            <option value="pending" <?= $filters['status'] === 'pending' ? 'selected' : '' ?>>Pendentes</option>
            <option value="proof_submitted" <?= $filters['status'] === 'proof_submitted' ? 'selected' : '' ?>>Com comprovativo</option>
            <option value="approved" <?= $filters['status'] === 'approved' ? 'selected' : '' ?>>Aprovados</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>Aluno</th>
                    <th>Curso</th>
                    <th>Valor</th>
                    <th>Estado</th>
                    <th>Comprovativo</th>
                    <th>Accoes</th>
                </tr>
            </thead>
            <tbody>
                <?php if (!$payments): ?>
                    <tr><td colspan="6">Nenhum pagamento encontrado.</td></tr>
                <?php endif; ?>
                <?php foreach ($payments as $payment): ?>
                    <tr>
                        <td><strong><?= e($payment['student_name']) ?></strong><span class="muted"><?= e($payment['student_email']) ?></span></td>
                        <td><strong><?= e($payment['course_title']) ?></strong><span class="muted"><?= e($payment['reference']) ?></span></td>
                        <td><?= e(money_mzn($payment['amount'])) ?></td>
                        <td><span class="badge <?= e($payment['status']) ?>"><?= e(payment_status_label($payment['status'])) ?></span></td>
                        <td>
                            <?php if (!empty($payment['proof'])): ?>
                                <a class="mini-button" href="/admin/payments/proof?id=<?= e((string) $payment['proof']['id']) ?>" target="_blank">Abrir</a>
                            <?php else: ?>
                                <span class="muted">Sem ficheiro</span>
                            <?php endif; ?>
                        </td>
                        <td class="row-actions">
                            <?php if (in_array($payment['status'], ['pending', 'proof_submitted'], true)): ?>
                                <form method="post" action="/admin/payments/approve">
                                    <?= csrf_field() ?>
                                    <input type="hidden" name="payment_id" value="<?= e((string) $payment['id']) ?>">
                                    <input type="hidden" name="admin_note" value="Pagamento confirmado.">
                                    <button class="mini-button good" type="submit">Aprovar</button>
                                </form>
                                <form method="post" action="/admin/payments/reject">
                                    <?= csrf_field() ?>
                                    <input type="hidden" name="payment_id" value="<?= e((string) $payment['id']) ?>">
                                    <input type="hidden" name="admin_note" value="Comprovativo invalido ou nao confirmado.">
                                    <button class="mini-button danger" type="submit">Rejeitar</button>
                                </form>
                            <?php else: ?>
                                <span class="muted"><?= e(format_date($payment['updated_at'] ?? '')) ?></span>
                            <?php endif; ?>
                        </td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</section>

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