<?php
include('session_validator.php');
// include('access_control.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">
        <!-- ========== 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">
                        <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 following fields to generate Gradesheet</h5>
                                        </div>
                                    </div>
                                    <div class="panel-body">
        <form id="gradesheetForm" method="post">
            <div class="row">
                <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
                    // Include the database configuration file
                    include("includes/db.php");

                    // Fetch class names from the 'classes' table
                    $queryexam = "SELECT id, exm_year , exam_type FROM examination_type";
                    $resultexam = $conn->query($queryexam);

                    // Populate the dropdown with class names and sections
                    while ($row = $resultexam->fetch_assoc()) {
                        echo "<option value='" . $row['id'] . "'>" . $row['exam_type'] . " - " . $row['exm_year'] . "</option>";
                    }

                    ?>

</select>

    </div>

        <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>
                        <!-- Populate dropdown options dynamically from your database -->
                            <?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>
          <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" />
        </div>


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

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



                <div class="form-group col-md-4">
            <label for="duplicateissuedate"> Copy of origional issue date: </label>
                <input type="" id="duplicateissuedate" name="duplicateissuedate" class="form-control"  />
       </div>
            

                <div class="form-group col-md-4">
                    <label for="report_type">Select Report Type:</label>
                    <select name="report_type" id="report_type" class="form-control" required>
                        <option value="gradesheet">Grade Sheet</option>
                        <option value="marksheet">Mark Sheet</option>
                    </select>
                </div>
            </div>
            <button type="button" id="generateReport" class="btn btn-success">Generate Report</button>
        </form>
    </div>
</div>
        

        <div id="reportContainer" class="panel-body">
       
    </div>
    

  <!-- /.main-wrapper -->
    <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 () {
            $('#feedate_visible').val(NepaliFunctions.ConvertDateFormat(NepaliFunctions.GetCurrentBsDate(), "YYYY-MM-DD"));
        // $('#issuedate').val(NepaliFunctions.ConvertDateFormat(NepaliFunctions.GetCurrentBsDate(), "YYYY-MM-DD"));
            var mainInput1 = document.getElementById("duplicateissuedate");
            mainInput1.nepaliDatePicker({
                ndpYear: true,
                ndpMonth: true,
                ndpYearCount: 100,
            })
        };

        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;
                }
            });
        }

    function fetchResultDate() {
    var examId = document.getElementById("exam_id").value;
    var classId = document.getElementById("class_id").value;

    if (examId && classId) {
        // Use AJAX to fetch the result publishing date
        $.ajax({
            type: "POST",
            url: "get_result_date.php", // Create a PHP file to handle this request
            data: {
                exam_id: examId,
                class_id: classId
            },
            success: function (response) {
                document.getElementById("result_date").value = response || "No Date Found";
            },
            error: function () {
                document.getElementById("result_date").value = "Error Fetching Date";
            }
        });
    }
}

// Attach the function to the onchange event of the dropdowns
document.getElementById("exam_id").addEventListener("change", fetchResultDate);
document.getElementById("class_id").addEventListener("change", fetchResultDate);

    </script>
    <script>
        $(document).ready(function() {
            $('#generateReport').click(function() {
                var exam_id = $('#exam_id').val();
                var class_id = $('#class_id').val();
                var  checked_by = $('#checked_by').val();
                var  prepared_by= $('#prepared_by').val();
                var  result_date = $('#result_date').val();
                var  duplicateissuedate = $('#duplicateissuedate').val();

                var report_type = $('#report_type').val();

                if (exam_id && class_id) {
                    $.ajax({
                        type: 'POST',
                        url: report_type === 'gradesheet' ? 'get_gradesheet1.php' : 'get_marksheet.php',
                        data: { exam_id: exam_id, class_id: class_id, checked_by : checked_by, prepared_by:prepared_by, result_date:result_date ,duplicateissuedate:duplicateissuedate },
                        success: function(response) {
                            $('#reportContainer').html(response);
                        },
                        error: function() {
                            alert('Error generating report.');
                        }
                    });
                } else {
                    alert('Please select all fields.');
                }
            });
        });
    </script>
</body>
</html>
