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

<section class="panel narrow">
    <p class="eyebrow">Teste final</p>
    <h1><?= e($course['title']) ?></h1>
    <p>Nota minima: <?= e((string) ($course['final_quiz_pass_score'] ?? 70)) ?>%.</p>

    <?php if ($bestAttempt): ?>
        <div class="sub-panel">
            <strong>Melhor tentativa: <?= e((string) $bestAttempt['score']) ?>%</strong>
            <p><?= !empty($bestAttempt['passed']) ? 'Aprovado.' : 'Ainda nao aprovado.' ?></p>
        </div>
    <?php endif; ?>

    <?php if (!$questions): ?>
        <div class="sub-panel">
            <p>Este curso ainda nao tem teste configurado.</p>
            <a class="button" href="/learn/course?id=<?= e((string) $course['id']) ?>">Voltar ao curso</a>
        </div>
    <?php else: ?>
        <form class="form quiz-form" method="post" action="/learn/quiz/submit">
            <?= csrf_field() ?>
            <input type="hidden" name="course_id" value="<?= e((string) $course['id']) ?>">
            <input type="hidden" name="enrollment_id" value="<?= e((string) $enrollment['id']) ?>">

            <?php foreach ($questions as $index => $question): ?>
                <fieldset class="form-section">
                    <legend>Pergunta <?= e((string) ($index + 1)) ?></legend>
                    <p><strong><?= e($question['question']) ?></strong></p>
                    <?php foreach ($question['options'] as $option): ?>
                        <label class="check-row">
                            <input type="radio" name="answers[<?= e((string) $question['id']) ?>]" value="<?= e($option) ?>" required>
                            <?= e($option) ?>
                        </label>
                    <?php endforeach; ?>
                </fieldset>
            <?php endforeach; ?>

            <div class="actions">
                <button class="button" type="submit">Submeter teste</button>
                <a class="ghost-button" href="/learn/course?id=<?= e((string) $course['id']) ?>">Voltar</a>
            </div>
        </form>
    <?php endif; ?>
</section>

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