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

<section class="panel">
    <p class="eyebrow">Administracao</p>
    <h1>Gestao de cursos</h1>
    <p>Aprove, publique, suspenda e acompanhe os cursos da plataforma.</p>

    <div class="stats compact">
        <div class="stat"><span>Total</span><strong><?= e((string) $stats['total']) ?></strong></div>
        <div class="stat warning"><span>Em aprovacao</span><strong><?= e((string) $stats['submitted']) ?></strong></div>
        <div class="stat"><span>Publicados</span><strong><?= e((string) $stats['published']) ?></strong></div>
        <div class="stat"><span>Gratuitos</span><strong><?= e((string) $stats['free']) ?></strong></div>
        <div class="stat"><span>Pagos</span><strong><?= e((string) $stats['paid']) ?></strong></div>
    </div>

    <div class="actions">
        <a class="ghost-button" href="/admin/categories">Gerir categorias</a>
    </div>

    <form method="get" action="/admin/courses" class="filterbar">
        <input name="search" placeholder="Pesquisar cursos" value="<?= e($filters['search']) ?>">
        <select name="category_id">
            <option value="0">Categorias</option>
            <?php foreach ($categories as $category): ?>
                <option value="<?= e((string) $category['id']) ?>" <?= (int) $filters['category_id'] === (int) $category['id'] ? 'selected' : '' ?>>
                    <?= e($category['name']) ?>
                </option>
            <?php endforeach; ?>
        </select>
        <select name="status">
            <option value="">Estados</option>
            <option value="draft" <?= $filters['status'] === 'draft' ? 'selected' : '' ?>>Rascunho</option>
            <option value="submitted" <?= $filters['status'] === 'submitted' ? 'selected' : '' ?>>Em aprovacao</option>
            <option value="published" <?= $filters['status'] === 'published' ? 'selected' : '' ?>>Publicado</option>
            <option value="rejected" <?= $filters['status'] === 'rejected' ? 'selected' : '' ?>>Rejeitado</option>
            <option value="suspended" <?= $filters['status'] === 'suspended' ? 'selected' : '' ?>>Suspenso</option>
        </select>
        <button class="button" type="submit">Filtrar</button>
    </form>

    <div class="table-wrap">
        <table>
            <thead>
                <tr>
                    <th>Curso</th>
                    <th>Dono</th>
                    <th>Preco</th>
                    <th>Estado</th>
                    <th>Accoes</th>
                </tr>
            </thead>
            <tbody>
                <?php if (!$courses): ?>
                    <tr><td colspan="5">Nenhum curso encontrado.</td></tr>
                <?php endif; ?>

                <?php foreach ($courses as $course): ?>
                    <tr>
                        <td>
                            <strong><?= e($course['title']) ?></strong>
                            <span class="muted"><?= e($course['category_name']) ?> · <?= e(level_label($course['level'])) ?></span>
                        </td>
                        <td><?= e($course['owner_name']) ?></td>
                        <td><?= $course['is_free'] ? 'Gratis' : e(money_mzn($course['price'])) ?></td>
                        <td><span class="badge <?= e($course['status']) ?>"><?= e(course_status_label($course['status'])) ?></span></td>
                        <td class="row-actions">
                            <?php if ($course['status'] === 'published'): ?>
                                <a class="mini-button" href="/courses/show?id=<?= e((string) $course['id']) ?>">Ver publico</a>
                            <?php endif; ?>
                            <?php if ($course['status'] === 'submitted'): ?>
                                <form method="post" action="/admin/courses/approve">
                                    <?= csrf_field() ?>
                                    <input type="hidden" name="course_id" value="<?= e((string) $course['id']) ?>">
                                    <button class="mini-button good" type="submit">Aprovar</button>
                                </form>
                                <form method="post" action="/admin/courses/reject">
                                    <?= csrf_field() ?>
                                    <input type="hidden" name="course_id" value="<?= e((string) $course['id']) ?>">
                                    <input type="hidden" name="reason" value="Rever conteudo do curso.">
                                    <button class="mini-button danger" type="submit">Rejeitar</button>
                                </form>
                            <?php endif; ?>
                            <?php if ($course['status'] === 'published'): ?>
                                <form method="post" action="/admin/courses/suspend">
                                    <?= csrf_field() ?>
                                    <input type="hidden" name="course_id" value="<?= e((string) $course['id']) ?>">
                                    <button class="mini-button danger" type="submit">Suspender</button>
                                </form>
                            <?php endif; ?>
                        </td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</section>

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