<?php

session_name('super_admin_session');

if (!is_dir(dirname(__DIR__) . '/session_data/super')) { mkdir(dirname(__DIR__) . '/session_data/super', 0700, true); } session_save_path(dirname(__DIR__) . '/session_data/super');

session_set_cookie_params(['path' => '/', 'httponly' => true, 'samesite' => 'Strict']);

session_start();

error_reporting(0);

include('includes/dbconnection.php');
require_once 'includes/session_term_helper.php';
$stResult = resolveCurrentSessionTerm($con);
$currentSessionName = $stResult['session_name'];
$currentTermName    = $stResult['term_name'];




// CSRF Token

if (empty($_SESSION['csrf_token'])) {

    $_SESSION['csrf_token'] = bin2hex(random_bytes(32));

}



// Fetch teachers

$teachers_result = $con->query("SELECT id, name, lastname FROM tblusers WHERE permission='Teacher' AND status=2");

$teachers = $teachers_result->fetch_all(MYSQLI_ASSOC);



// Fetch classes

$classes_result = $con->query("SELECT id, class, section FROM class_section");

$classes = $classes_result->fetch_all(MYSQLI_ASSOC);



?>









<!DOCTYPE html>

<html>

<?php @include("includes/head.php"); ?>



<body class="hold-transition sidebar-mini layout-fixed">

    <div class="wrapper">

        <!-- Navbar -->

        <?php @include("includes/header.php"); ?>

        <!-- /.navbar -->



        <!-- Main Sidebar Container -->

        <?php @include("includes/sidebar.php"); ?>



        <!-- Content Wrapper. Contains page content -->

        <div class="content-wrapper">

            <!-- Content Header (Page header) -->

<section class="content-header premium-header">

    <div class="container-fluid">

        <div class="header-glass-card">

            <div class="row align-items-center">

                <div class="col-md-8">

                    <div class="title-section">

                        <div class="icon-box bg-primary-gradient shadow-sm">

                            <i class="fas fa-user-tie"></i>

                        </div>

                        <div>

                            <h1 class="premium-title text-primary">

                                Teacher Information Management

                                <span class="premium-subtitle text-secondary">Manage Teacher</span>

                            </h1>
                            <div class="mt-1">
                              <?php if ($currentSessionName): ?>
                                <span class="badge badge-primary mr-1">
                                  <i class="fas fa-calendar-alt"></i>
                                  <?= htmlspecialchars($currentSessionName) ?>
                                </span>
                              <?php endif; ?>
                              <?php if ($currentTermName): ?>
                                <span class="badge badge-success">
                                  <i class="fas fa-bookmark"></i>
                                  <?= htmlspecialchars($currentTermName) ?>
                                </span>
                              <?php else: ?>
                                <span class="badge badge-warning">
                                  <i class="fas fa-exclamation-triangle"></i> No active term
                                </span>
                              <?php endif; ?>
                            </div>

                        </div>

                    </div>

                </div>

                <div class="col-md-4">

                    <nav aria-label="breadcrumb">

                        <ol class="breadcrumb premium-breadcrumb">

                            <li class="breadcrumb-item"><a href="dashboard.php" class="text-primary">Home</a></li>

                            <li class="breadcrumb-item active" aria-current="page">Assign Teacher</li>

                        </ol>

                    </nav>

                </div>

            </div>

        </div>

    </div>

</section>             





            <!-- Main content -->

            <section class="content">

                <div class="container-fluid">

                    <div class="row ">

                        <div class="col-md-12">

                            <!-- general form elements -->

                            <div class="card card-primary">

                                <div class="card-header">

                                    <h5 class="card-title font-weight-bold"><i class="fas fa-chalkboard-teacher"></i> Assign Teacher to class and subject</h5>

                                </div>

                                <!-- /.card-header -->

                                <!-- form start -->



                                <form action="assign_teacher_backend.php" method="POST">

                                    <div class="card-body">

                                        <input type="hidden" name="csrf_token" value="<?php echo $_SESSION['csrf_token']; ?>">



                                        <div class="row">

                                            <!-- Select Teacher -->

                                            <div class="form-group col-md-6">

                                                <label class="form-label">Teacher:</label>

                                                <select name="teacher_id" class="form-control select2" required>

                                                    <option value="">Select a Teacher</option>

                                                    <?php foreach ($teachers as $teacher): ?>

                                                        <option value="<?= htmlspecialchars($teacher['id']); ?>">

                                                            <?= htmlspecialchars("{$teacher['name']} {$teacher['lastname']}"); ?>

                                                        </option>

                                                    <?php endforeach; ?>

                                                </select>



                                            </div>



                                            <!-- Assign as Class Teacher -->

                                            <div class="form-group col-md-6">

                                                <label class="form-label">Assign as Class Teacher (Optional):</label>

                                                <select name="class_id" class="form-control select2">

                                                    <option value="">None</option>

                                                    <?php foreach ($classes as $class): ?>

                                                        <option value="<?= htmlspecialchars($class['id']); ?>">

                                                            <?= htmlspecialchars("{$class['class']} - {$class['section']}"); ?>

                                                        </option>

                                                    <?php endforeach; ?>

                                                </select>

                                            </div>



                                            <!-- Select Class -->

                                            <div class="form-group col-md-6">

                                                <label class="form-label">Class:</label>

                                                <select id="classSelector" class="form-control select2">

                                                    <option value="">Select Class</option>

                                                    <?php

                                                    $query2 = "SELECT DISTINCT class FROM class_section ORDER BY class";

                                                    $result2 = $con->query($query2);

                                                    while ($row2 = $result2->fetch_assoc()) {

                                                        echo "<option value='{$row2['class']}' data-classname='{$row2['class']}'> {$row2['class']}</option>";

                                                    }

                                                    ?>

                                                </select>

                                            </div>



                                            <!-- Select Section -->

                                            <div class="form-group col-md-6">

                                                <label class="form-label">Arm:</label>

                                                <select id="sectionSelector" class="form-control select2">

                                                    <option value="">Select Arm</option>

                                                </select>

                                            </div>



                                            <!-- Assign as Subject Teacher -->

                                            <div class="form-group col-md-12">

                                                <label class="form-label">Assign as Subject Teacher (Select Subjects):</label>

                                                <div id="subjectContainer" class="border p-3 rounded bg-light">

                                                    <p class="text-muted">Select a class and arm first to see subjects.</p>

                                                </div>

                                            </div>

                                            <input type="hidden" name="subject_class_id" id="subject_class_id">





                                            <!-- Submit Button -->

                                            <div class="form-group col-md-12 text-center">

                                                <button type="submit" class="btn btn-primary px-4">

                                                    <i class="fas fa-check-circle"></i> Assign Teacher

                                                </button>

                                            </div>

                                        </div>

                                </form>









                            </div>

                            <!-- /.card -->

                        </div>

                        <!-- /.col -->

                    </div>

                    <!-- /.row -->

                </div>

                <!-- /.container-fluid -->

            </section>

            <!-- /.content -->

        </div>

        <!-- /.content-wrapper -->

        <?php @include("includes/footer.php"); ?>



    </div>



    <!-- ./wrapper -->

    <?php @include("includes/foot.php"); ?>



    <script src="dist/js/select2.min.js"></script>

    <script src="dist/js/jquery.min.js"></script> <!-- Ensure jQuery is included -->



    <script>

        $(document).ready(function() {

            $('.select2').select2();



            $('#classSelector').change(function() {

                var selectedClassId = $(this).val();

                var selectedClassName = $(this).find(':selected').data('classname');



                if (selectedClassId) {

                    $.ajax({

                        url: 'fetch_sectionss.php',

                        type: 'POST',

                        data: {

                            class_name: selectedClassName

                        },

                        success: function(response) {

                            $('#sectionSelector').html(response);

                        }

                    });

                } else {

                    $('#sectionSelector').html('<option value="">Select Arm</option>');

                    $('#subjectContainer').html('<p>Select a class and arm first to see subjects.</p>');

                }

            });



            $('#sectionSelector').change(function() {

                var subjectClassId = $(this).val(); // class_section.id from dropdown

                $('#subject_class_id').val(subjectClassId); // Set hidden input for backend



                var selectedClassName = $('#classSelector').find(':selected').data('classname');

                var selectedSection = $(this).find(':selected').data('section');



                if (selectedClassName && selectedSection) {

                    $.ajax({

                        url: 'fetch_subjects.php',

                        type: 'POST',

                        data: {

                            class_name: selectedClassName,

                            section: selectedSection

                        },

                        success: function(response) {

                            $('#subjectContainer').html(response);

                        }

                    });

                } else {

                    $('#subjectContainer').html('<p>Select a class and arm first to see subjects.</p>');

                }

            });

        });

    </script>

</body>



</html>