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

// Admin-only access
// checkAccess('admin');
?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <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="nepali.datepicker.v4.0.1\css\main.css" media="screen">
    <title>Grade Sheet Generator</title>
</head>

<body class="top-navbar-fixed">
    <div class="main-wrapper">
        <?php include('includes/topbar.php'); ?>

        <div class="content-wrapper">
            <div class="content-container">
                <?php include('includes/leftbar.php'); ?>

                <div class="main-page">
                    <div class="container-fluid">
                        <div class="row page-title-div">
                            <div class="col-md-6">
                                <h2 class="title">Select Class to Generate Blank Mark Slip</h2>
                            </div>
                        </div>
                        <div class="row breadcrumb-div">
                            <div class="col-md-6">
                                <ul class="breadcrumb">
                                    <li><a href="dashboard.php"><i class="fa fa-home"></i> Home</a></li>
                                    <li>Marks</li>
                                    <li class="">Blank Mark Slip</li>
                                </ul>
                            </div>
                        </div>
                    </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>Select the following fields to generate Gradesheet</h5>
                                        </div>
                                    </div>

                                    <div class="panel-body">
                                        <form id="gradesheetForm" action="get_gradesheet_sym.php" method="post">
                                            <div class="row">
                                                <!-- Select Exam -->
                                                <div class="form-group col-md-4">
                                                    <label for="exam_id">Select Exam:</label>
                                                    <select id="exam_id" name="exam_id" class="form-control" required>
                                                        <option value="">Select Exam</option>
                                                        <?php
                                                            $queryexam = "SELECT id, exm_year, exam_type FROM examination_type";
                                                            $resultexam = $conn->query($queryexam);
                                                            while ($row = $resultexam->fetch_assoc()) {
                                                                echo "<option value='" . $row['id'] . "'>" . $row['exam_type'] . " - " . $row['exm_year'] . "</option>";
                                                            }
                                                        ?>
                                                    </select>
                                                </div>

                                                <!-- Select Class -->
                                                <div class="form-group col-md-4">
                                                    <label for="class_id">Select Class:</label>
                                                    <select name="class_id" id="class_id" class="form-control" required onchange="updateCheckedBy()">
                                                        <option>Select Class</option>
                                                        <?php
                                                            $classQuery = "SELECT class_id, class_name, section FROM classes WHERE status = 'on'";
                                                            $classResult = $conn->query($classQuery);
                                                            while ($row = $classResult->fetch_assoc()) {
                                                                echo "<option value='" . $row['class_id'] . "'>" . $row['class_name'] . "- " . $row['section'] . "</option>";
                                                            }
                                                        ?>
                                                    </select>
                                                </div>
                                                

                                                <!-- Checked By -->
                                                <div class="form-group col-md-4">
                                                    <label for="chkby">Checked By:</label>
                                                    <input type="text" id="checked_by" name="checked_by" class="form-control" placeholder="Select class first" required />
                                                </div>

                                                <!-- Prepared By -->
                                                <div class="form-group col-md-4">
                                                    <label for="prepby">Prepared By:</label>
                                                    <select name="prepared_by" id="prepared_by" class="form-control" required>
                                                        <option value="Saurav Neupane">Saurav Neupane</option>
                                                        <option value="Dambaru Ballav Ojha">Dambaru Ballav Ojha</option>
                                                    </select>
                                                </div>

                                                <!-- Result Publishing Date -->
                                                <div class="form-group col-md-4">
                                                    <label for="result_date">Result Publishing Date:</label>
                                                    <input type="text" id="result_date" name="result_date" class="form-control" readonly placeholder="Select Exam and Class" />
                                                </div>

                                                <!-- Duplicate Issue Date -->
                                                <div class="form-group col-md-4">
                                                    <label for="duplicateissuedate">Copy of Original Issue Date:</label>
                                                    <input type="text" id="duplicateissuedate" name="duplicateissuedate" class="form-control" />
                                                </div>
                                            </div>

                                            <div class="row">
                                                <!-- Student Symbol Number -->
                                                <div class="form-group col-md-4">
                                                    <label for="std_symbolnumber">Student Symbol Number:</label>
                                                    <input type="text" class="form-control" id="std_symbolnumber" name="std_symbolnumber" />
                                                </div>
<input type="hidden" class="form-control" id="std_id_visible" name="std_id_visible">
                                                <!-- Student Name -->
                                                <div class="form-group col-md-4">
                                                    <label for="student_name">Student Name:</label>
                                                    <input type="hidden" class="form-control" id="student_name1" name="student_name1">
                                                    <input type="text" class="form-control" id="student_name" name="student_name" autocomplete="off" />
                                                    <div id="studentNameList" class="list-group"></div>
                                                </div>

                                                <!-- Submit Button -->
                                                <div class="form-group col-md-4">
                                                    <button type="submit" class="form-control" style="color:white; background:green">Show Gradesheets</button>
                                                </div>
                                            </div>
                                        </form>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <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>
    <link href="nepali.datepicker.v4.0.1/css/nepali.datepicker.v4.0.1.min.css" rel="stylesheet" type="text/css" />
    <script src="nepali.datepicker.v4.0.1/js/nepali.datepicker.v4.0.1.min.js"></script>
    
    <script>
        window.onload = function () {
            const mainInput1 = document.getElementById("duplicateissuedate");
            mainInput1.nepaliDatePicker({ ndpYear: true, ndpMonth: true, ndpYearCount: 100 });
        };

        // Fetching and displaying the teacher for the selected class
        function updateCheckedBy() {
            var classId = document.getElementById("class_id").value;

            // Use AJAX to fetch the class teacher based on the selected class
            $.ajax({
                type: "POST",
                url: "get_class_teacher.php", // Create a PHP file to handle this request
                data: {
                    class_id: classId
                },
                success: function (response) {
                    document.getElementById("checked_by").value = response;
                }
            });
        }
// Attach the function to the onchange event of the dropdowns
document.getElementById("exam_id").addEventListener("change", fetchResultDate);
document.getElementById("class_id").addEventListener("change", fetchResultDate);
        // Fetching result publishing date
        function fetchResultDate() {
            const examId = $('#exam_id').val();
            const classId = $('#class_id').val();
            if (examId && classId) {
                $.post("get_result_date.php", { exam_id: examId, class_id: classId }, function(response) {
                    $('#result_date').val(response || "No Date Found");
                }).fail(function() {
                    $('#result_date').val("Error Fetching Date");
                });
            }
        }

        // Autocomplete for student names
        $(document).ready(function () {
            $('#student_name').on('keyup', function () {
                const query = $(this).val();
                if (query.length > 0) {
                    $.post('fetch_student_names.php', { query: query }, function (data) {
                        $('#studentNameList').fadeIn().html(data);
                    });
                } else {
                    $('#studentNameList').fadeOut();
                }
            });

            $(document).on('click', '.student-name-item', function () {
                $('#student_name').val($(this).text());
                $('#studentNameList').fadeOut();
            });

            $('#std_symbolnumber').on('blur', function () {
                const symbolNumber = $(this).val();
                if (symbolNumber) {
                    $.post('get_student_details_leftover.php', { symbol_no: symbolNumber }, function (response) {
                        if (response) {
                            const studentData = JSON.parse(response);
                            $('#student_name').val(studentData.name).prop('disabled', true);
                            $('#student_name1').val(studentData.name);
                            $('#std_id_visible').val(studentData.student_id);
                        } else {
                            $('#student_name').val('');
                            alert('Student not found!');
                        }
                    });
                } else {
                    $('#student_name').val('').prop('disabled', true);
                }
            });
        });
    </script>
</body>

</html>
