<?php
session_start();
require 'connection/config.php'; // Ensure your database connection file is included

// Ensure matric number is set
if (!isset($_SESSION['matric_number'])) {
    die("Matric number is not set. Please log in again.");
}

$matric = $_SESSION['matric_number'];
$question_level_score = $_SESSION['question_level']."score"; // Get the dynamic score table

// Check if the table exists
$checkTableQuery = "SHOW TABLES LIKE :table_name";
$stmt = $pdo->prepare($checkTableQuery);
$stmt->execute(['table_name' => $question_level_score]);

if ($stmt->rowCount() == 0) {
    die("Score table does not exist.");
}

// Fetch the student's score
$query = "SELECT score FROM `$question_level_score` WHERE matric = :matric";
$stmt = $pdo->prepare($query);
$stmt->execute(['matric' => $matric]);

$score = $stmt->fetchColumn();

if ($score === false) {
    $score = "No score recorded yet.";
}

?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Score Confirmation</title>
    <style>
        body { font-family: Arial, sans-serif; text-align: center; margin: 50px; }
        .container { max-width: 400px; margin: auto; padding: 20px; border: 1px solid #ccc; border-radius: 10px; }
        button { padding: 10px 20px; font-size: 16px; margin: 10px; cursor: pointer; }
        .confirm { background: green; color: white; border: none; }
        .cancel { background: red; color: white; border: none; }
    </style>
</head>
<body>
    <div class="container">
        <h2>Score Confirmation</h2>
        <p><strong>Matric Number:</strong> <?php echo htmlspecialchars($matric); ?></p>
        <p><strong>Your Score:</strong> <?php echo htmlspecialchars($score); ?></p>
        
        <form action="submit_final.php" method="post">
            <button type="submit" name="confirm" class="confirm">Confirm & Submit</button>
            <button type="button" class="cancel" onclick="window.location.href='cbtqmodule.php'">Cancel</button>
        </form>
    </div>
</body>
</html>
