<?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'];




if (!isset($_SESSION['sid'])) {

    header('location:logout.php');

    exit();

}



?>





<!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-graduated"></i>

                        </div>

                        <div>

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

                                Student Information Management

                                <span class="premium-subtitle text-secondary">ID Card Generator</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">ID Cards</li>

                        </ol>

                    </nav>

                </div>

            </div>

        </div>

    </div>

</section>       



            <!-- Main content -->

            <section class="content">

                <div class="container-fluid">

                    <div class="row">

                        <div class="col-12">

                            <div class="card card-primary">

                                <div class="card-header">

                                    <h5 class="card-title font-weight-bold"> <i class="fas fa-id-card"></i> ID Card Generator (Admitted Students Only)</h5>

                                    <div class="card-tools">

                                    </div>

                                </div>



                                <div class="card-body">

                                    <!-- Date filter form -->

                                    <form method="GET" action="student_list.php">

                                        <div class="row p-0 m-0">

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

                                                <label for="class">Class</label>

                                                <select name="class" id="class" class="form-control" onchange="fetchArms()">

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

                                                    <?php

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

                                                    $result = $con->query($query);

                                                    while ($row = $result->fetch_assoc()) {

                                                        echo "<option value='{$row['class']}'>{$row['class']}</option>";

                                                    }

                                                    ?>

                                                </select>

                                            </div>



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

                                                <label for="arm">Arm</label>

                                                <select name="arm" id="arm" class="form-control">

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

                                                </select>

                                            </div>





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

                                                <button type="button" class="btn btn-primary" onclick="downloadFilteredPDF()"><i class="fas fa-id-card"></i> Generate & Download ID Card</button>

                                            </div>

                                        </div>

                                    </form>

                                </div>

                                <!-- /.card-body -->

                            </div>

                            <!-- /.card -->

                        </div>

                        <!-- /.col -->

                    </div>

                    <!-- /.row -->

                </div>

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

            </section>

            <!-- /.content -->

        </div>

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

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



        <!-- Control Sidebar -->

        <aside class="control-sidebar control-sidebar-dark">

            <!-- Control sidebar content goes here -->

        </aside>

        <!-- /.control-sidebar -->

    </div>



    <!-- ./wrapper -->

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

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

    <script>

        // In your Javascript (external .js resource or <script> tag)

        $(document).ready(function() {

            $('#class').select2();

        });

        $(document).ready(function() {

            $('#arm').select2();

        });

    </script>



    <script>

        function fetchArms() {

            var classSelected = document.getElementById("class").value;

            var armDropdown = document.getElementById("arm");



            // Clear previous options

            armDropdown.innerHTML = '<option value="">Select Arm</option>';



            if (classSelected) {

                fetch("fetch_arms.php?class=" + classSelected)

                    .then(response => response.json())

                    .then(data => {

                        data.forEach(arm => {

                            let option = document.createElement("option");

                            option.value = arm;

                            option.textContent = arm;

                            armDropdown.appendChild(option);

                        });

                    })

                    .catch(error => console.error("Error fetching arms:", error));

            }

        }



        function downloadFilteredPDF() {

            var selectedClass = document.getElementById("class").value;

            var selectedArm = document.getElementById("arm").value;



            if (selectedClass) {

                var url = "generate_ids.php?class=" + selectedClass + "&section=" + selectedArm;

                window.location.href = url;

            } else {

                alert("Please select a class.");

            }

        }

    </script>

</body>



</html>