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

<section class="panel">
    <p class="eyebrow">Aluno</p>
    <h1>Meus cursos</h1>
    <p>Continue de onde parou e acompanhe o seu progresso.</p>

    <div class="learning-grid">
        <?php if (!$items): ?>
            <div class="sub-panel">
                <h2>Ainda nao tem cursos</h2>
                <p>Explore o catalogo e inscreva-se em cursos gratuitos. Cursos pagos entram quando activarmos pagamentos.</p>
                <a class="button" href="/courses">Explorar cursos</a>
            </div>
        <?php endif; ?>

        <?php foreach ($items as $item): ?>
            <?php
                $course = $item['course'];
                $progress = $item['progress'];
                $certificate = $item['certificate'] ?? null;
                $eligibility = $item['certificate_eligibility'] ?? ['eligible' => false, 'reason' => ''];
            ?>
            <article class="learning-card">
                <a class="course-cover" style="<?= !empty($course['cover_image_url']) ? 'background-image: linear-gradient(180deg, rgba(0,0,0,.08), rgba(0,0,0,.55)), url(' . e($course['cover_image_url']) . ')' : 'background: ' . e($course['cover_color']) ?>" href="/learn/course?id=<?= e((string) $course['id']) ?>">
                    <span><?= e($course['category_name']) ?></span>
                </a>
                <div class="course-body">
                    <a class="course-title" href="/learn/course?id=<?= e((string) $course['id']) ?>"><?= e($course['title']) ?></a>
                    <span class="muted"><?= e($course['owner_name']) ?></span>
                    <div class="progress-wrap">
                        <div class="progress-bar"><span style="width: <?= e((string) $progress['percentage']) ?>%"></span></div>
                        <strong><?= e((string) $progress['percentage']) ?>%</strong>
                    </div>
                    <a class="button full" href="/learn/course?id=<?= e((string) $course['id']) ?>">Continuar</a>
                    <?php if (!empty($course['final_quiz_required'])): ?>
                        <a class="ghost-button full" href="/learn/quiz?course_id=<?= e((string) $course['id']) ?>">Teste final</a>
                    <?php endif; ?>
                    <?php if ($certificate): ?>
                        <a class="ghost-button full" href="/certificates/show?id=<?= e((string) $certificate['id']) ?>">Ver certificado</a>
                    <?php elseif (!empty($course['certificate_enabled'])): ?>
                        <?php if (!empty($eligibility['eligible'])): ?>
                            <form method="post" action="/certificates/issue">
                                <?= csrf_field() ?>
                                <input type="hidden" name="enrollment_id" value="<?= e((string) $item['enrollment']['id']) ?>">
                                <button class="ghost-button full" type="submit">Emitir certificado</button>
                            </form>
                        <?php else: ?>
                            <span class="muted"><?= e((string) ($eligibility['reason'] ?? 'Certificado ainda indisponivel.')) ?></span>
                        <?php endif; ?>
                    <?php endif; ?>
                </div>
            </article>
        <?php endforeach; ?>
    </div>
</section>

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