<?php
session_start();
include "../db.php";
date_default_timezone_set('Asia/Kolkata');
$currentTime = date('M j, Y g:i a');

// Check if the user is logged in
if (!isset($_SESSION['uid']) && !isset($_SESSION['uref']) && !isset($_SESSION['utype']) && !isset($_SESSION['uname'])) {
    header("Location: logout.php");
    exit();
}

$u_sql = "SELECT sclass, admin_ref FROM users WHERE uref = '$_SESSION[uref]'";
$u_result = mysqli_query($con, $u_sql);
$u_row = mysqli_fetch_assoc($u_result);
$userClass = $u_row["sclass"];
$admin_2 = $u_row["admin_ref"];

$class_sql = mysqli_query($con, "SELECT * FROM class WHERE sclass = '$userClass'");
if ($class_row = mysqli_fetch_assoc($class_sql)) {
    $class_sl = $class_row["sid"];
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $correct_answers = isset($_POST['correct_answers']) ? $_POST['correct_answers'] : [];
    $total_correct = count($correct_answers);
    $total_questions = $_SESSION['total_questions'];
    $n_id = $_SESSION['n_id'];

    // Reset the session data
    unset($_SESSION['correct_answers']);
}
?>
<!DOCTYPE html>
<html lang="en">

<head>
    <?php include "includes/meta.php"; ?>
    <?php include "includes/css.php"; ?>
</head>

<body id="page-top" class="mt-55">
    <div style="margin-top: 70px;"></div>
    <?php include "includes/header2.php"; ?>
    <div class="container">
        <div class="row">
            <div class="col-12 col-sm-12 col-md-12">
                <div class="card-body">
                    <h1 class="h3 mb-2 text-gray-800 text-center">Quiz Results</h1>
                    <hr>
                    <div class="results-summary">
                        <p>Total Questions: <?php echo $total_questions; ?></p>
                        <p>Correct Answers: <?php echo $total_correct; ?></p>
                    </div>
                    <?php 
                    $sql = "SELECT * FROM subject WHERE sc_class_id = '$class_sl' ORDER BY sc_id DESC";
                    $res = mysqli_query($con,$sql);
                    if($rows = mysqli_fetch_assoc($res)){
                    ?>
                    <div>
                        <a href="view-notes.php?sid=<?php echo $rows['sc_id']; ?>" class="btn btn-warning"><i
                                class="fa fa-arrow-left"></i> Back to Notes</a>
                    </div>
                    <?php }?>
                </div>
            </div>
        </div>
    </div>

    <?php include "includes/ft.php"; ?>
    <?php include "includes/js.php"; ?>
</body>

</html>