<?php
include('session_validator.php');
include("includes/db.php");

function sanitizeInput($input) {
    return htmlspecialchars(stripslashes(trim($input)));
}

// Fetch classes for dropdown
$fetchClassesQuery = "SELECT class_id, class_name, section FROM classes";
$classesResult = $conn->query($fetchClassesQuery);
$classes = [];

if ($classesResult->num_rows > 0) {
    while ($row = $classesResult->fetch_assoc()) {
        $classes[] = $row;
    }
}

$conn->close();
?>

<!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">
    <title>SRMS Admin Manage Subjects</title>
    <link rel="stylesheet" href="css/bootstrap.min.css" media="screen">
    <link rel="stylesheet" href="css/font-awesome.min.css" media="screen">
    <link rel="stylesheet" href="css/animate-css/animate.min.css" media="screen">
    <link rel="stylesheet" href="css/lobipanel/lobipanel.min.css" media="screen">
    <link rel="stylesheet" href="css/prism/prism.css" media="screen">
    <link rel="stylesheet" href="css/select2/select2.min.css">
    <link rel="stylesheet" href="css/main.css" media="screen">
    <script src="js/modernizr/modernizr.min.js"></script>
</head>

<body class="top-navbar-fixed">
    <div class="main-wrapper">
        <!-- ========== TOP NAVBAR ========== -->
        <?php include('includes/topbar.php'); ?>
        <!-- ========== WRAPPER FOR BOTH SIDEBARS & MAIN CONTENT ========== -->
        <div class="content-wrapper">
            <div class="content-container">
                <!-- ========== LEFT SIDEBAR ========== -->
                <?php include('includes/leftbar.php'); ?>
                <!-- /.left-sidebar -->
                <div class="main-page">
                    <div class="container-fluid">
                        <!-- Add your page title and breadcrumb -->
                    </div>
                    <div class="container-fluid">
                        <div class="row">
                            <div class="col-md-12">
                                <div class="panel">
                                    <div class="panel-heading">
                                        <div class="panel-title">
                                            <h5>Exam Routines</h5>
                                        </div>
                                    </div>

                                    <div class="panel-body">
                                        <!-- Display exam routines based on selected class -->
                                        <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
                                            <div class="form-group col-md-4">
                                                <label for="class_id">Select Class:</label>
                                                <select id="class_id" name="class_id" class="form-control" required>
                                                    <option value="">Select Class</option>
                                                    <?php foreach ($classes as $class) : ?>
                                                        <option value="<?php echo $class['class_id']; ?>"><?php echo $class['class_name'] . '-' . $class['section']; ?></option>
                                                    <?php endforeach; ?>
                                                </select>
                                            </div>
                                            <input type="submit" name="submit" value="View Routine" style="background: blue; color: white; font-weight: bold;" class="form-control">
                                        </form>

                                        <?php
                                        if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['submit'])) {
                                            $selectedClassId = sanitizeInput($_POST['class_id']);
                                            displayExamRoutines($selectedClassId);
                                        }

                                        function displayExamRoutines($classId)
                                        {
                                            include("includes/db.php");
                                            // Fetch exam routines based on selected class
                                            $fetchExamRoutinesQuery = "SELECT examination_routine.*, subjects.subject_name FROM examination_routine 
                                            INNER JOIN subjects ON examination_routine.subject_id = subjects.id 
                                            WHERE examination_routine.class_id = $classId";
                                            $examRoutinesResult = $conn->query($fetchExamRoutinesQuery);
                                            ?>

                                            <div class="table-responsive">
                                                <table id="examRoutinesTable" class="table table-bordered table-striped">
                                                    <thead>
                                                        <tr>
                                                            <th>ID</th>
                                                            <th>Class</th>
                                                            <th>Exam Type</th>
                                                            <th>Exam Year</th>
                                                            <th>Subject</th>
                                                            <th>Exam Date</th>
                                                            <th>Action</th>
                                                        </tr>
                                                    </thead>
                                                    <tbody>
                                                        <?php
                                                        if ($examRoutinesResult->num_rows > 0) {
                                                            while ($row = $examRoutinesResult->fetch_assoc()) {
                                                                echo '<tr>';
                                                                echo '<td>' . $row['id'] . '</td>';
                                                                echo '<td>' . $row['class_id'] . '</td>';
                                                                echo '<td>' . $row['exam_type'] . '</td>';
                                                                echo '<td>' . $row['exam_year'] . '</td>';
                                                                echo '<td>' . $row['subject_name'] . '</td>';
                                                                echo '<td>' . $row['exam_date'] . '</td>';
                                                                echo '<td>
                                                                        <button class="btn btn-info btn-sm" data-toggle="modal" data-target="#viewModal' . $row['id'] . '">View</button>
                                                                        <button class="btn btn-warning btn-sm" data-toggle="modal" data-target="#editModal' . $row['id'] . '" 
            data-id="' . $row['id'] . '" 
            data-class="' . $row['class_id'] . '" 
            data-exam-type="' . $row['exam_type'] . '" 
            data-exam-year="' . $row['exam_year'] . '" 
            data-subject="' . $row['subject_name'] . '" 
            data-exam-date="' . $row['exam_date'] . '">Edit</button>
      </td>';
                                                                      echo '</tr>';

                                                                // Include the view and edit modals for each exam routine
                                                                include('view_modal.php');
                                                                include('edit_modal.php');
                                                            }
                                                        } else {
                                                            echo '<tr><td colspan="7">No exam routines found</td></tr>';
                                                        }
                                                        ?>
                                                    </tbody>
                                                </table>
                                            </div>
                                            <?php
                                            $conn->close();
                                        }
                                        ?>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- Include necessary scripts -->
        <script src="js/jquery/jquery-2.2.4.min.js"></script>
        <script src="js/bootstrap/bootstrap.min.js"></script>
        <script src="js/pace/pace.min.js"></script>
        <script src="js/lobipanel/lobipanel.min.js"></script>
        <script src="js/iscroll/iscroll.js"></script>
        <script src="js/prism/prism.js"></script>
        <script src="js/main.js"></script>
        <script src="js/modernizr/modernizr.min.js"></script>

        <script>
            // Initialize DataTable
            $(document).ready(function () {
                $('#examRoutinesTable').DataTable();
            });
        </script>
    </body>
</html>
