<?php error_reporting(0); ?>
<?php    session_start(); ?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <?php include '../admin/includes/styles.php'; ?>
    <title> Online Exam Results | <?php echo $school_name; ?> </title>

    <?php
    session_start();
    if(isset($_SESSION['student'])){

    // Fetch student ID using the session username
    $conn->query("SELECT studentID FROM student WHERE username = :username");
    $conn->bind(":username", $_SESSION['student']);
    $student_id = $conn->fetchColumn();


    ?>

</head>
<body class="hold-transition sidebar-mini layout-fixed">
<div class="wrapper">

    <!-- Navbar -->
    <?php include 'includes/notifications.php'; ?>
    <!-- /.navbar -->

    <!-- Main Sidebar Container -->
    <?php include "includes/sidebar.php";?>

    <!-- Content Wrapper. Contains page content -->
    <div class="content-wrapper">
        <!-- Content Header (Page header) -->
        <div class="content-header">
            <div class="container-fluid">
                <div class="row mb-2">
                    <div class="col-sm-6">
                        <h1 class="m-0"> Online Exam Results </h1>
                    </div><!-- /.col -->
                    <div class="col-sm-6">
                        <ol class="breadcrumb float-sm-right">
                            <li class="breadcrumb-item"><a href="<?php echo $conn->student_url(); ?>">Home</a></li>
                            <li class="breadcrumb-item active">Online Results </li>
                        </ol>
                    </div><!-- /.col -->
                </div><!-- /.row -->
            </div><!-- /.container-fluid -->
        </div>
        <!-- /.content-header -->

        <!-- Main content -->
        <section class="content">
            <div class="container-fluid">

                <div class="row">
                    <div class="col-md-10">
                        <div class="card card-info">
                            <div class="card-header">
                                <h3 class="card-title">View online exam reults</h3>

                                <?php
                                // Fetch all exams the student has taken (including expired ones)
                                $conn->query("
                                    SELECT 
                                        e.title, 
                                        e.exam_date, 
                                        e.time_start, 
                                        e.time_end, 
                                        s.subject_name, 
                                        r.correct_answers, 
                                        r.total_questions, 
                                        r.score_percentage, 
                                        r.exam_id 
                                    FROM cbt_results r
                                    JOIN cbt_exams e ON r.exam_id = e.id
                                    JOIN subjects s ON e.subject_id = s.id
                                    WHERE r.student_id = :student_id
                                    ORDER BY e.exam_date DESC
                                ");
                                $conn->bind(":student_id", $student_id);
                                $results = $conn->fetchMultiple();


                                ?>
                            </div>

                                <div class="card-body">

                                    <h2 class="text-center">My exam results</h2>
                                        <?php if (!empty($results)) : ?>
                                            <div class="row">
                                                <?php foreach ($results as $result) : ?>
                                                    <div class="col-md-4 mb-3">
                                                        <div class="card shadow-sm">
                                                            <div class="card-body">
                                                                <h5 class="card-title"><?php echo htmlspecialchars($result->exam_name); ?></h5>
                                                                <p class="card-text"><strong>Subject:</strong> <?php echo htmlspecialchars($result->subject_name); ?></p>
                                                                <p class="card-text"><strong>Date:</strong> <?php echo htmlspecialchars($result->exam_date); ?></p>
                                                                <p class="card-text"><strong>Score:</strong> <?php echo $result->score_percentage; ?>%</p>
                                                                <p class="card-text"><strong>Correct Answers:</strong> <?php echo $result->correct_answers . '/' . $result->total_questions; ?></p>
                                                                <a href="exam_result.php?exam_id=<?php echo $result->exam_id; ?>" class="btn btn-success w-100">View Full Result</a>
                                                            </div>
                                                        </div>
                                                    </div>
                                                <?php endforeach; ?>
                                            </div>
                                        <?php else : ?>
                                            <p class="alert alert-info">You have not taken any exams yet.</p>
                                        <?php endif; ?>

                                </div>
                        </div>
                        <!-- /.card -->
                    </div>
                </div>


                <!-- /.row (main row) -->
            </div><!-- /.container-fluid -->
        </section>
        <!-- /.content -->
    </div>
    <!-- /.content-wrapper -->
    <?php include "../admin/includes/footer.php";?>


    <script>
        $(document).ready(function(){
            $('.select2').select2();

        })

    </script>


    <?php
    }else{
    ?>
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, shrink-to-fit=no">


        <?php include '../admin/includes/styles.php'; ?>
        <title>Login to continue </title>

        <!-- END PAGE LEVEL PLUGINS/CUSTOM STYLES -->

    </head>
    <body>
    <div class="container" style="margin-top: 20%;">
        <div class="row">
            <div class="col-lg-12 col-md-12">
                <div class="section-title text-center">
                    <h6> You need to login to continue </h6>
                    <div class="spinner-border text-info" role="status">
                        <span class="sr-only">Loading...</span>
                        <meta http-equiv='refresh' content='3; <?php echo $student_login_link; ?>'>
                    </div>
                    <p>Redirecting to the login  page...</p>
                </div>
            </div>
        </div>
    </div>

    <?php
    }

    ?>
