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

<section class="panel">
    <p class="eyebrow">Cursos</p>
    <h1>Meus cursos</h1>
    <div class="actions">
        <a class="button" href="/manage/courses/create">Criar curso</a>
    </div>

    <form method="get" action="/manage/courses" class="filterbar simple">
        <input name="search" placeholder="Pesquisar nos meus cursos" value="<?= e($filters['search']) ?>">
        <select name="status">
            <option value="">Todos os 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>Categoria</th>
                    <th>Preco</th>
                    <th>Estado</th>
                    <th>Prontidao</th>
                    <th>Accoes</th>
                </tr>
            </thead>
            <tbody>
                <?php if (!$courses): ?>
                    <tr><td colspan="6">Ainda nao existem cursos.</td></tr>
                <?php endif; ?>
                <?php foreach ($courses as $course): ?>
                    <tr>
                        <td><strong><?= e($course['title']) ?></strong><span class="muted"><?= e($course['subtitle']) ?></span></td>
                        <td><?= e($course['category_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>
                            <div class="mini-quality <?= !empty($course['publication_ready']) ? 'ready' : '' ?>">
                                <span><i style="width: <?= e((string) ($course['publication_score'] ?? 0)) ?>%"></i></span>
                                <strong><?= e((string) ($course['publication_score'] ?? 0)) ?>%</strong>
                            </div>
                        </td>
                        <td class="row-actions">
                            <a class="mini-button" href="/manage/courses/edit?id=<?= e((string) $course['id']) ?>">Editar</a>
                            <a class="mini-button" href="/manage/courses/content?id=<?= e((string) $course['id']) ?>">Conteudo</a>
                            <a class="mini-button" href="/manage/courses/assessment?id=<?= e((string) $course['id']) ?>">Teste</a>
                            <?php if (in_array($course['status'], ['draft', 'rejected'], true)): ?>
                                <?php if (!empty($course['publication_ready'])): ?>
                                    <form method="post" action="/manage/courses/submit">
                                        <?= csrf_field() ?>
                                        <input type="hidden" name="course_id" value="<?= e((string) $course['id']) ?>">
                                        <button class="mini-button good" type="submit">Submeter</button>
                                    </form>
                                <?php else: ?>
                                    <a class="mini-button" href="/manage/courses/edit?id=<?= e((string) $course['id']) ?>">Completar</a>
                                <?php endif; ?>
                            <?php endif; ?>
                        </td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</section>

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