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

// 1️⃣ Find active quiz
$quiz = null;

$result = $conn->query(
    "SELECT * FROM computer_quizzes 
     WHERE is_active = 1 
     ORDER BY created_at DESC 
     LIMIT 1"
);

if ($result && $result->num_rows > 0) {
    $quiz = $result->fetch_assoc();
}

$error = '';

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

    if (!$quiz) {
        $error = "Quiz not started yet.";
    } else {
        $name  = trim($_POST['student_name']);
        $class = trim($_POST['class']);
        $roll  = trim($_POST['roll_number']);

        if ($name === '' || $class === '' || $roll === '') {
            $error = "All fields are required.";
        } else {

            // Save attempt
            $stmt = $conn->prepare(
                "INSERT INTO computer_quiz_attempts 
                 (quiz_id, student_name, class, roll_number)
                 VALUES (?, ?, ?, ?)"
            );
            $stmt->bind_param(
                "isss",
                $quiz['id'],
                $name,
                $class,
                $roll
            );
            $stmt->execute();
            $attempt_id = $stmt->insert_id;
            $stmt->close();

            // Store attempt in session
            $_SESSION['quiz_attempt_id'] = $attempt_id;
            $_SESSION['quiz_id'] = $quiz['id'];

            header("Location: computer_quiz_questions.php");
            exit;
        }
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Computer Quiz | RS Learning Lab</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;
    display:flex;
    align-items:center;
    justify-content:center;
    min-height:100vh;
}

.card{
    width:100%;
    max-width:420px;
    background:#0f172a;
    border:1px solid #1e293b;
    border-radius:18px;
    padding:32px;
}
h1{margin-top:0;font-size:22px}
p{color:#94a3b8}

label{display:block;margin-top:18px}
input{
    width:100%;
    margin-top:6px;
    padding:14px;
    border-radius:12px;
    border:1px solid #1e293b;
    background:#020617;
    color:#e5e7eb;
}

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

.error{
    margin-top:14px;
    color:#fca5a5;
    font-size:14px;
}
</style>
</head>

<body>
<div class="card">

    <h1>Learning Style Quiz</h1>

    <?php if ($quiz): ?>
        <p>Please enter your details to begin.</p>
    <?php else: ?>
        <p>No active quiz at the moment.</p>
    <?php endif; ?>

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

    <?php if ($quiz): ?>
    <form method="post">
        <label>Student Name</label>
        <input name="student_name" required>

        <label>Class</label>
        <input name="class" required>

        <label>Roll Number</label>
        <input name="roll_number" required>

        <button type="submit">Start Quiz</button>
    </form>
    <?php endif; ?>

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