<?php
include('session_validator.php');?>
<!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>
        <style>
            .form-group {
                margin-bottom: 15px;
            }
        </style>
    </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 Parent Meeting 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> Reports </li>
                                        <li class="">Parent Meeting 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 Class to Generate Parent Meeting Slip</h5>
                                            </div>
                                        </div>
                                        <div class="panel-body">
                                            <form action="meeting_slip.php" method="get" id="classForm">
                                                <div class="row">
                                                    <div class="col-md-4">
                                                        <div class="form-group">
                                                            <label for="class_id">Select Class:</label>
                                                            <select name="class_id" id="class_id" class="form-control select2" required onchange="loadSections(this.value)">
                                                                <option value="">Select Class</option>
                                                                <?php
                                                                // Include database connection code
                                                                include("includes/db.php");

                                                                // Query classes from the database
                                                                $classQuery = "SELECT c.class_id, c.class 
                                                                              FROM classes c
                                                                              INNER JOIN class_section cs ON c.class_id = cs.class_id
                                                                              WHERE c.status = 1
                                                                              GROUP BY c.class_id
                                                                              ORDER BY c.class_numeric";
                                                                $classResult = $conn->query($classQuery);

                                                                while ($row = $classResult->fetch_assoc()) {
                                                                    $selected = (isset($_GET['class_id']) && $_GET['class_id'] == $row['class_id']) ? 'selected' : '';
                                                                    echo "<option value='" . $row['class_id'] . "' $selected>" . 
                                                                         htmlspecialchars($row['class']) . "</option>";
                                                                }
                                                                $conn->close();
                                                                ?>
                                                            </select>
                                                        </div>
                                                    </div>
                                                    
                                                    <div class="col-md-4">
                                                        <div class="form-group">
                                                            <label for="section_id">Select Section:</label>
                                                            <select name="section_id" id="section_id" class="form-control select2" required>
                                                                <option value="">Select Section</option>
                                                                <?php
                                                                if (isset($_GET['class_id']) && !empty($_GET['class_id'])) {
                                                                    include("includes/db.php");
                                                                    $class_id = $_GET['class_id'];
                                                                    
                                                                    // Get sections for selected class
                                                                    $sectionQuery = "SELECT s.section_id, s.section_name 
                                                                                    FROM sections s
                                                                                    INNER JOIN class_section cs ON s.section_id = cs.section_id
                                                                                    WHERE cs.class_id = ? AND s.status = 1
                                                                                    ORDER BY s.section_name";
                                                                    $stmt = $conn->prepare($sectionQuery);
                                                                    $stmt->bind_param("i", $class_id);
                                                                    $stmt->execute();
                                                                    $sectionResult = $stmt->get_result();
                                                                    
                                                                    while ($row = $sectionResult->fetch_assoc()) {
                                                                        $selected = (isset($_GET['section_id']) && $_GET['section_id'] == $row['section_id']) ? 'selected' : '';
                                                                        echo "<option value='" . $row['section_id'] . "' $selected>" . 
                                                                             htmlspecialchars($row['section_name']) . "</option>";
                                                                    }
                                                                    $stmt->close();
                                                                    $conn->close();
                                                                }
                                                                ?>
                                                            </select>
                                                        </div>
                                                    </div>
                                                    
                                                    <div class="col-md-4">
                                                        <div class="form-group" style="padding-top: 28px;">
                                                            <button type="submit" class="btn btn-primary">
                                                                <i class="fa fa-print"></i> Generate Meeting Slip
                                                            </button>
                                                            <button type="button" onclick="resetForm()" class="btn btn-default">
                                                                <i class="fa fa-refresh"></i> Reset
                                                            </button>
                                                        </div>
                                                    </div>
                                                </div>
                                                
                                                <div class="row">
                                                    <div class="col-md-12">
                                                        <div class="alert alert-info">
                                                            <small>
                                                                <i class="fa fa-info-circle"></i> 
                                                                This will generate a parent meeting attendance slip with student names and spaces for parent signatures.
                                                            </small>
                                                        </div>
                                                    </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/select2/select2.min.js"></script>
        <script src="js/main.js"></script>
        
        <script>
        $(document).ready(function() {
            // Initialize Select2
            $('.select2').select2({
                placeholder: "Select option",
                allowClear: true
            });
            
            // Load sections if class is already selected
            if ($('#class_id').val()) {
                loadSections($('#class_id').val());
            }
        });
        
        function loadSections(classId) {
            if (!classId) {
                $('#section_id').html('<option value="">Select Section</option>').val('').trigger('change');
                return;
            }
            
            // Show loading
            $('#section_id').html('<option value="">Loading sections...</option>').val('').trigger('change');
            
            $.ajax({
                url: 'ajax/get_sections_by_class.php',
                type: 'GET',
                data: { class_id: classId },
                dataType: 'json',
                success: function(sections) {
                    let options = '<option value="">Select Section</option>';
                    if (sections && sections.length > 0) {
                        sections.forEach(function(section) {
                            options += '<option value="' + section.section_id + '">' + section.section_name + '</option>';
                        });
                    }
                    $('#section_id').html(options).trigger('change');
                },
                error: function() {
                    $('#section_id').html('<option value="">Error loading sections</option>').trigger('change');
                }
            });
        }
        
        function resetForm() {
            $('#classForm')[0].reset();
            $('#section_id').html('<option value="">Select Section</option>').val('').trigger('change');
            $('.select2').trigger('change');
        }
        </script>
    </body>
</html>