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

<section class="panel">
    <div class="page-head">
        <div>
            <p class="eyebrow">Administracao</p>
            <h1>Relatorios</h1>
            <p>Resumo operacional da plataforma: vendas, pagamentos, cursos, alunos, comissoes e certificados.</p>
        </div>
        <a class="button" href="/admin/reports/export?<?= e(http_build_query($filters)) ?>">Exportar Excel</a>
    </div>

    <form method="get" action="/admin/reports" class="filterbar report-filter">
        <label>Inicio
            <input name="date_from" type="date" value="<?= e($filters['date_from']) ?>">
        </label>
        <label>Fim
            <input name="date_to" type="date" value="<?= e($filters['date_to']) ?>">
        </label>
        <label>Estado
            <select name="status">
                <option value="">Todos</option>
                <option value="pending" <?= $filters['status'] === 'pending' ? 'selected' : '' ?>>Pendente</option>
                <option value="proof_submitted" <?= $filters['status'] === 'proof_submitted' ? 'selected' : '' ?>>Com comprovativo</option>
                <option value="approved" <?= $filters['status'] === 'approved' ? 'selected' : '' ?>>Aprovado</option>
                <option value="rejected" <?= $filters['status'] === 'rejected' ? 'selected' : '' ?>>Rejeitado</option>
            </select>
        </label>
        <label>Pesquisa
            <input name="search" placeholder="Aluno, curso ou referencia" value="<?= e($filters['search']) ?>">
        </label>
        <button class="button" type="submit">Filtrar</button>
    </form>

    <div class="stats compact">
        <div class="stat"><span>Receita aprovada</span><strong><?= e(money_mzn($report['summary']['gross_revenue'])) ?></strong><small><?= e((string) $report['summary']['sales_count']) ?> vendas</small></div>
        <div class="stat warning"><span>Receita pendente</span><strong><?= e(money_mzn($report['summary']['pending_revenue'])) ?></strong><small>pagamentos por validar</small></div>
        <div class="stat"><span>Pagamentos</span><strong><?= e((string) $report['summary']['payments_count']) ?></strong><small>no periodo</small></div>
        <div class="stat"><span>Certificados</span><strong><?= e((string) $report['summary']['certificates_count']) ?></strong><small>emitidos</small></div>
        <div class="stat"><span>Comissao plataforma</span><strong><?= e(money_mzn($report['commission_totals']['platform'])) ?></strong><small><?= e((string) $report['commission_totals']['count']) ?> registos</small></div>
    </div>
</section>

<section class="detail-grid reports-grid">
    <div class="panel">
        <h2>Estado dos pagamentos</h2>
        <div class="table-wrap">
            <table>
                <thead><tr><th>Estado</th><th>Quantidade</th><th>Total</th></tr></thead>
                <tbody>
                    <?php foreach ($report['payment_statuses'] as $status => $item): ?>
                        <tr>
                            <td><span class="badge <?= e($status) ?>"><?= e(payment_status_label($status)) ?></span></td>
                            <td><?= e((string) $item['count']) ?></td>
                            <td><?= e(money_mzn($item['amount'])) ?></td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>

    <div class="panel">
        <h2>Utilizadores e cursos</h2>
        <div class="report-mini-grid">
            <div><span>Alunos</span><strong><?= e((string) $report['user_stats']['students']) ?></strong></div>
            <div><span>Instrutores</span><strong><?= e((string) $report['user_stats']['instructors']) ?></strong></div>
            <div><span>Instituicoes</span><strong><?= e((string) $report['user_stats']['institutions']) ?></strong></div>
            <div><span>Cursos publicados</span><strong><?= e((string) $report['course_stats']['published']) ?></strong></div>
            <div><span>Cursos pagos</span><strong><?= e((string) $report['course_stats']['paid']) ?></strong></div>
            <div><span>Cursos gratis</span><strong><?= e((string) $report['course_stats']['free']) ?></strong></div>
        </div>
    </div>
</section>

<section class="detail-grid reports-grid">
    <div class="panel">
        <h2>Cursos com mais vendas</h2>
        <div class="table-wrap">
            <table>
                <thead><tr><th>Curso</th><th>Vendas</th><th>Receita</th></tr></thead>
                <tbody>
                    <?php if (!$report['top_courses']): ?><tr><td colspan="3">Ainda nao existem vendas aprovadas.</td></tr><?php endif; ?>
                    <?php foreach ($report['top_courses'] as $course): ?>
                        <tr>
                            <td><strong><?= e($course['title']) ?></strong><span class="muted">#<?= e((string) $course['id']) ?></span></td>
                            <td><?= e((string) $course['sales']) ?></td>
                            <td><?= e(money_mzn($course['amount'])) ?></td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>

    <div class="panel">
        <h2>Pagamentos recentes</h2>
        <div class="table-wrap">
            <table>
                <thead><tr><th>Aluno</th><th>Curso</th><th>Valor</th><th>Estado</th></tr></thead>
                <tbody>
                    <?php if (!$report['recent_payments']): ?><tr><td colspan="4">Nenhum pagamento encontrado.</td></tr><?php endif; ?>
                    <?php foreach ($report['recent_payments'] as $payment): ?>
                        <tr>
                            <td><strong><?= e($payment['student_name']) ?></strong><span class="muted"><?= e(format_date($payment['updated_at'] ?? '')) ?></span></td>
                            <td><?= e($payment['course_title']) ?></td>
                            <td><?= e(money_mzn($payment['amount'])) ?></td>
                            <td><span class="badge <?= e($payment['status']) ?>"><?= e(payment_status_label($payment['status'])) ?></span></td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>
</section>

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