<?php
// computer_quiz_questions.php
session_start();
require_once __DIR__ . '/config.php';

// 🔐 Guard: must come from entry page
if (!isset($_SESSION['quiz_attempt_id']) || !isset($_SESSION['quiz_id'])) {
    header("Location: computer_quiz.php");
    exit;
}

$attemptId = $_SESSION['quiz_attempt_id'];

// 🔹 20 FIXED QUESTIONS (Learning-style based)
$questions = [
    1 => "When learning something new, what helps you most?",
    2 => "If you don’t understand a topic, what do you do first?",
    3 => "You remember things best when you…",
    4 => "In exams, you usually prefer questions that are…",
    5 => "While studying, you are more comfortable with…",
    6 => "When solving problems, you usually…",
    7 => "You feel confident when learning involves…",
    8 => "During group study, you usually…",
    9 => "You understand faster when lessons include…",
    10 => "When instructions are given, you prefer them to be…",
    11 => "You feel bored quickly if learning is…",
    12 => "You prefer teachers who…",
    13 => "While revising, you usually…",
    14 => "You feel learning is effective when it is…",
    15 => "When facing a difficult question, you…",
    16 => "You like assignments that involve…",
    17 => "You stay focused when tasks are…",
    18 => "You learn better from mistakes when…",
    19 => "You feel confident if learning materials are…",
    20 => "Overall, you feel learning is best when it is…"
];

// Options (same pattern for all)
$options = [
    'A' => 'Using diagrams, images or videos',
    'B' => 'Understanding logic and steps',
    'C' => 'Practicing with real examples',
    'D' => 'Thinking deeply and quietly'
];

$error = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    foreach ($questions as $qNo => $qText) {
        if (!isset($_POST['q'][$qNo])) {
            $error = "Please answer all questions.";
            break;
        }
    }

    if ($error === '') {

        foreach ($_POST['q'] as $qNo => $answer) {
            $stmt = $conn->prepare(
                "INSERT INTO computer_quiz_answers 
                 (attempt_id, question_no, selected_option)
                 VALUES (?, ?, ?)"
            );
            $stmt->bind_param("iis", $attemptId, $qNo, $answer);
            $stmt->execute();
            $stmt->close();
        }

        header("Location: generate_learning_pdf.php");
        exit;
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Learning Style Quiz</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">

<style>
*{box-sizing:border-box;font-family:'Inter',system-ui}
body{margin:0;background:#0b1220;color:#e5e7eb}
.container{max-width:860px;margin:40px auto;padding:0 20px}
h1{text-align:center;margin-bottom:26px}

.question{
    background:#0f172a;
    border:1px solid #1e293b;
    border-radius:16px;
    padding:22px;
    margin-bottom:18px;
}
.question h3{margin-top:0;font-size:16px}

.option{margin-top:10px}
.option label{cursor:pointer}

button{
    margin-top:28px;
    width:100%;
    padding:16px;
    border-radius:16px;
    border:0;
    background:#22d3ee;
    color:#001018;
    font-weight:700;
    font-size:16px;
    cursor:pointer;
}

.error{text-align:center;color:#fca5a5;margin-bottom:14px}
</style>
</head>

<body>
<div class="container">

<h1>Learning Style Assessment</h1>

<?php if ($error): ?>
<div class="error"><?= htmlspecialchars($error) ?></div>
<?php endif; ?>

<form method="post">
<?php foreach ($questions as $no => $text): ?>
    <div class="question">
        <h3><?= $no ?>. <?= htmlspecialchars($text) ?></h3>
        <?php foreach ($options as $key => $label): ?>
            <div class="option">
                <label>
                    <input type="radio" name="q[<?= $no ?>]" value="<?= $key ?>" required>
                    <?= $key ?>. <?= htmlspecialchars($label) ?>
                </label>
            </div>
        <?php endforeach; ?>
    </div>
<?php endforeach; ?>

<button type="submit">Submit Quiz</button>
</form>

</div>
</body>
</html>
