<script>
    window.onload = function() {
        sessionStorage.clear();
    }
</script>
<?php
session_start();
session_unset();  // Unset all session variables
session_destroy(); 
session_start();
include('connection/config.php');

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $matric = $_POST['matric_number'];

    // Validate matric number
    $stmt = $pdo->prepare("SELECT * FROM students WHERE matric = :matric_number and status=''");
    $stmt->bindParam(':matric_number', $matric);
    $stmt->execute();
    
    if ($stmt->rowCount() > 0) {
        $_SESSION['matric_number'] = $matric;
		$record = $stmt->fetch(PDO::FETCH_ASSOC);
		$fetched_question = strtolower($record['question_level']); //Just Added
		$_SESSION['question_level']=$fetched_question;
		$updateStatus = $pdo->prepare("UPDATE students SET status = 'Attempted' WHERE matric = :matric");
		$updateStatus->execute(['matric' => $matric]);
        header('Location: cbtqmodule.php');  // Redirect to the question page
        exit();
    } else {
        $error = "Invalid Matric Number or Previous Attempt Found!";
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>School Login</title>
    <link rel="stylesheet" href="css/indexstyle.css">
</head>
<body>
<script>
    sessionStorage.clear(); // Clears all stored answers when a new exam starts
</script>
 <div class="container">
        <h2>OSCOTECH CBT </h2>
        <form method="POST" action="">
            <div class="form-group">
                <label for="matric_number"><center>Computer Based Examination</center></label>
                <input type="text" id="matric_number" name="matric_number" placeholder="Type in your Matric No" autocomplete="off" required>
            </div>
            <button type="submit">Login</button>

            <?php if (isset($error)): ?>
                <p class="error"><?php echo $error; ?></p>
            <?php endif; ?>
        </form>
    </div>

</body>
</html>