<?php $pageTitle = 'Test Result'; ?>
<div class="mb-3"><a href="<?= base_url('student/tests') ?>" class="text-secondary"><i class="fas fa-arrow-left me-1"></i> Back to Tests</a></div>
<div class="card-cix mb-4">
    <div class="text-center">
        <?php if ($attempt['passed']): ?>
            <div style="width:100px;height:100px;border-radius:50%;background:rgba(0,255,65,0.15);margin:0 auto 1rem;display:flex;align-items:center;justify-content:center;"><i class="fas fa-check-circle" style="font-size:3rem;color:var(--accent)"></i></div>
            <h2 style="color:var(--accent)">Congratulations! You Passed!</h2>
        <?php else: ?>
            <div style="width:100px;height:100px;border-radius:50%;background:rgba(255,68,68,0.15);margin:0 auto 1rem;display:flex;align-items:center;justify-content:center;"><i class="fas fa-times-circle" style="font-size:3rem;color:var(--danger)"></i></div>
            <h2 style="color:var(--danger)">Not Passed</h2>
        <?php endif; ?>
        <h3 class="mb-2" style="font-size:3rem;color:var(--secondary)"><?= e($attempt['score']) ?>%</h3>
        <p class="text-secondary">Score: <?= e($attempt['score']) ?> / <?= e($attempt['max_score']) ?> | Pass Mark: <?= e($attempt['pass_mark']) ?>%</p>
        <p class="text-secondary">Time spent: <?= floor((int)$attempt['time_spent'] / 60) ?>m <?= (int)$attempt['time_spent'] % 60 ?>s | Attempt #<?= e($attempt['attempt_number']) ?></p>
    </div>
</div>
<div class="card-cix mb-4">
    <div class="card-cix-header"><h5 class="mb-0">Question Review</h5></div>
    <?php
    $stmt = $pdo->prepare("SELECT taa.*, q.question_text, q.explanation, qo.option_text as selected_text, (SELECT qo2.option_text FROM question_options qo2 JOIN questions q2 ON qo2.question_id = q2.id WHERE q2.id = q.question_id AND qo2.is_correct = 1 LIMIT 1) as correct_text FROM test_attempt_answers taa JOIN questions q ON taa.question_id = q.id LEFT JOIN question_options qo ON taa.selected_option_id = qo.id WHERE taa.attempt_id = ? ORDER BY taa.id");
    $stmt->execute([$attempt['id']]);
    $answers = $stmt->fetchAll();
    ?>
    <?php foreach ($answers as $i => $ans): ?>
    <div style="padding:1rem;border-bottom:1px solid var(--border);">
        <div class="d-flex align-items-center gap-2 mb-1">
            <span class="badge-cix <?= $ans['is_correct'] ? 'badge-active' : 'badge-rejected' ?>"><?= $ans['is_correct'] ? 'Correct' : 'Incorrect' ?></span>
            <strong>Question <?= ($i + 1) ?></strong>
        </div>
        <p class="mb-1"><?= e($ans['question_text']) ?></p>
        <?php if (!$ans['is_correct']): ?>
            <p class="mb-1"><span class="text-secondary">Your answer:</span> <span style="color:var(--danger)"><?= e($ans['selected_text'] ?? 'No answer') ?></span></p>
            <p class="mb-1"><span class="text-secondary">Correct answer:</span> <span style="color:var(--accent)"><?= e($ans['correct_text'] ?? 'N/A') ?></span></p>
        <?php endif; ?>
        <?php if ($ans['explanation']): ?><p class="text-secondary small mt-1"><i class="fas fa-info-circle"></i> <?= e($ans['explanation']) ?></p><?php endif; ?>
    </div>
    <?php endforeach; ?>
</div>
