<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>OSCOTECH CBT EXAMINATION</title>
    <link rel="stylesheet" href="css/indexstyle.css">
	<style>

/* DISABLED INPUT STYLE */
input:disabled {
    background: #ddd;
}

/* OVERLAY POPUP */
#overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0,0,0,0.7);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 9999;
}

/* MODAL BOX */
.modal {
    background: white;
    padding: 25px;
    border-radius: 10px;
    width: 80%;
    max-width: 400px;
    text-align: center;
}

.modal button {
    padding: 10px 20px;
    margin-top: 15px;
    background: green;
    color: white;
    border: none;
    cursor: pointer;
}
</style>
</head>
<body>
<script>
    sessionStorage.clear(); // Clears all stored answers when a new exam starts
</script>
<!--  POPUP MODAL -->
<div id="overlay">
    <div class="modal">
        <h2>IMPORTANT INSTRUCTIONS</h2>
        <p align="left">
            1. Do not refresh the page<br>
            2. Do not switch tabs<br>
            3. Once started, exam cannot be paused<br>
            4. Any malpractice leads to disqualification
        </p>

        <button onclick="agree()">I AGREE</button>
    </div>
</div>

<script>
function agree() {
    document.getElementById("overlay").style.display = "none";

    // enable inputs
    document.getElementById("matric").disabled = false;
    document.getElementById("loginBtn").disabled = false;
}
</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>