<?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 Exams | <?php echo $school_name; ?> </title>

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


    ?>

</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 Exams </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 Exams </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="callout callout-info">
                        <span>N:B: You can only take any exam once. So make sure you are fully prepared before taking the particular exam.</span>
                      </div>
                        <div class="card card-info">
                            <div class="card-header">
                                <h3 class="card-title">View available online exams</h3>

                                <?php
                                // Fetch available exams for the student based on class and section
                                $username = $_SESSION['student'];
                                //fetch student details
                                $sql = "SELECT * FROM student WHERE username =:username";
                                $conn->query($sql);
                                $conn->bind(":username", $username);
                                $result = $conn->fetchSingle();

                                $student_id = $result->studentID;
                                $class_id = $result->classesID;
                                $section_id = $result->sectionid;
                                $conn->query("SELECT classesID, sectionid FROM student WHERE studentID = :student_id");
                                $conn->bind(":student_id", $student_id);
                                $student_info = $conn->fetchSingle();

                                //fetch current_academic session
                                $conn->query("SELECT current_school_session FROM general_settings");
                                $academic_session = $conn->fetchColumn();

                                //fetch term
                                $conn->query("SELECT active_term FROM general_settings");
                                $academic_term = $conn->fetchColumn();

                                //fetch term id
                                $conn->query("SELECT id FROM terms WHERE name = :term_name");
                                $conn->bind(":term_name", $academic_term);
                                $term_id = $conn->fetchColumn();


                                // Get the current date and time
                                $current_datetime = date('Y-m-d H:i:s');

                                // Fetch exams that are still available
                                $conn->query("SELECT * FROM cbt_exams WHERE class_id = :class_id 
                                        AND section_id = :section_id 
                                        AND academic_session = :academic_session 
                                        AND term_id = :term_id 
                                        AND status = 'published' 
                                        AND CONCAT(exam_date, ' ', time_end) >= :current_datetime
                                        ORDER BY exam_date DESC");

                                $conn->bind(":class_id", $class_id);
                                $conn->bind(":section_id", $section_id);
                                $conn->bind(":academic_session", $academic_session);
                                $conn->bind(":term_id", $term_id);
                                $conn->bind(":current_datetime", $current_datetime);
                                $exams = $conn->fetchMultiple();


                                ?>
                            </div>

                                    <h2 class="text-center">Available Exams</h2>
                                    <div class="row">
                                        <?php if (!empty($exams)) : ?>
                                            <?php foreach ($exams as $exam) : ?>
                                                <div class="col-md-4 mb-4">
                                                    <div class="card shadow-sm">
                                                        <div class="card-body">
                                                            <h5 class="card-title"><?php echo htmlspecialchars($exam->exam_name); ?></h5>
                                                            <?php
                                                            // Fetch exam subject
                                                            $conn->query("SELECT subject_name FROM subjects WHERE id = :subject_id");
                                                            $conn->bind(":subject_id", $exam->subject_id);
                                                            $subject_name = $conn->fetchColumn();

                                                            // Check if the student has already taken this exam
                                                            $conn->query("SELECT id FROM cbt_results WHERE student_id = :student_id AND exam_id = :exam_id");
                                                            $conn->bind(":student_id", $student_id);
                                                            $conn->bind(":exam_id", $exam->id);
                                                            $has_taken_exam = $conn->fetchColumn();
                                                            ?>

                                                            <p class="card-text"><strong>Subject:</strong> <?php echo htmlspecialchars($subject_name); ?></p>
                                                            <p class="card-text"><strong>Date:</strong> <?php echo htmlspecialchars($exam->exam_date); ?></p>
                                                            <p class="card-text"><strong>Time:</strong> <?php echo date('h:i A', strtotime($exam->time_start)) . " - " . date('h:i A', strtotime($exam->time_end)); ?></p>

                                                            <?php if ($has_taken_exam): ?>
                                                                <a href="<?php echo $conn->student_url(); ?>exam_result.php?exam_id=<?php echo $exam->id; ?>"
                                                                   class="btn btn-success w-100">View Exam Results</a>
                                                            <?php else: ?>
                                                                <a href="<?php echo $conn->student_url(); ?>start_exam.php?exam_id=<?php echo $exam->id; ?>"
                                                                   class="btn btn-primary w-100">Take Exam</a>
                                                            <?php endif; ?>
                                                        </div>
                                                    </div>

                                                </div>
                                            <?php endforeach; ?>
                                        <?php else : ?>
                                            <div class="col-12">
                                                <div class="alert alert-info text-center">No exams available at the moment.</div>
                                            </div>
                                        <?php endif; ?>
                                    </div>

                                </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
    }

    ?>
