<?php
    session_start();

    if (!isset($_SESSION['admin_username'])) {
        header("Location: admin_login.php");
        exit;
    } else {
        include('config.php');

        // Initialize last_checked_submission_id if not set
        if (!isset($_SESSION['last_checked_submission_id'])) {
            $_SESSION['last_checked_submission_id'] = 0; // Assuming your submission IDs start from 1
        } else {
            try {
                // Prepare the SQL query
                $sql = "SELECT MAX(id) AS max_id FROM submissions";
                $stmt = $pdo->prepare($sql);

                // Execute the query
                $stmt->execute();

                // Fetch the result
                $result = $stmt->fetch(PDO::FETCH_ASSOC);

                // Check if any row was returned
                if ($result && $result['max_id'] > $_SESSION['last_checked_submission_id']) {
                    // New submission detected
                    $_SESSION['last_checked_submission_id'] = $result['max_id'];
                    echo "new_submission";
                }
            } catch (PDOException $e) {
                echo "Error: " . $e->getMessage();
            }
        }
    }
?>
