<?php
session_start();

// If you want to set an exam time, uncomment the following line:
$_SESSION['exam_time'] = time() + (60 * 30); // Example: 30 minutes from now

// This will be used for AJAX requests
header('Content-Type: application/json');

// If an AJAX request is made to fetch exam time
if ($_SERVER['REQUEST_METHOD'] === 'GET' && isset($_GET['fetch_exam_time'])) {
    // Here you could implement logic to check if the exam has started
    // For demonstration, we'll just return true
    echo json_encode(['exam_time' => true]);
    exit;
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Exam Stopwatch</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            text-align: center;
            margin-top: 50px;
        }
        #timer {
            font-size: 2em;
            margin: 20px 0;
        }
    </style>
    <script>
        let timer;
        let elapsedTime = 0; // Time in seconds

        function startStopwatch() {
            timer = setInterval(function() {
                elapsedTime++;

                let minutes = Math.floor(elapsedTime / 60);
                let seconds = elapsedTime % 60;

                document.getElementById('timer').innerHTML = 
                    minutes + " minutes " + seconds + " seconds";
            }, 1000);
        }

        window.onload = function() {
            // Fetch exam time from PHP session via AJAX
            fetch('<?= $_SERVER['PHP_SELF']; ?>?fetch_exam_time=true') // Fetch from the same file
                .then(response => response.json())
                .then(data => {
                    if (data.exam_time) {
                        // Start the stopwatch
                        startStopwatch();
                    }
                });
        }
    </script>
</head>
<body>
    <h1>Exam Stopwatch</h1>
    <div id="timer">0 minutes 0 seconds</div>
</body>
</html>
