<?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']) || strlen($_SESSION['sid']) == 0) {

    header('location:logout.php');

    exit();

}

?>

<!DOCTYPE html>

<html lang="en">

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



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

    <div class="wrapper">

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

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



        <div class="content-wrapper">

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

                        </div>

                        <div>

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

                                Result Management

                                <span class="premium-subtitle text-secondary">Bulk Download (All Students)</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">Download Result</li>

                        </ol>

                    </nav>

                </div>

            </div>

        </div>

    </div>

</section>   



            <!-- Main Content -->

            <section class="content">

                <div class="container-fluid">

                    <div class="row">

                        <!-- Mid Term Section -->

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

                            <div class="card shadow-sm">

                                <div class="card-header bg-primary text-white">

                                    <h5 class="card-title font-weight-bold">

                                        <i class="fas fa-layer-group"></i> Download Mid-Term Results

                                    </h5>

                                </div>

                                <div class="card-body">

                                    <form method="GET" action="bulk_download_mid.php" class="mb-4">

                                        <input type="hidden" name="exam_type" value="midterm">

                                        <div class="row">

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

                                                <label for="session_id_mid">Academic Session</label>

                                                <select name="session_id" id="session_id_mid" class="form-control select2" required>

                                                    <option value="">-- Select Session --</option>

                                                    <?php

                                                    $sessionResult = $con->query("SELECT DISTINCT id, session_name FROM school_sessions ORDER BY session_name DESC");

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

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

                                                    }

                                                    ?>

                                                </select>

                                            </div>



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

                                                <label for="term_id_mid">Term</label>

                                                <select name="term_id" id="term_id_mid" class="form-control select2" required>

                                                    <option value="">-- Select Term --</option>

                                                </select>

                                            </div>



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

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

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

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

                                                    <?php

                                                    $classResult = $con->query("SELECT DISTINCT class FROM class_section ORDER BY class");

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

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

                                                    }

                                                    ?>

                                                </select>

                                            </div>



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

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

                                                <select name="section" id="section_mid" class="form-control select2" required>

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

                                                    <?php

                                                    $armResult = $con->query("SELECT DISTINCT section FROM class_section ORDER BY section");

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

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

                                                    }

                                                    ?>

                                                </select>

                                            </div>

                                        </div>



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

                                            <button type="submit" name="download_bulk" class="btn btn-primary mt-3">

                                                <i class="fas fa-check-circle"></i> Download Mid-Term Results

                                            </button>

                                        </div>

                                    </form>

                                </div>

                            </div>

                        </div>



                        <!-- End of Term Section -->

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

                            <div class="card shadow-sm">

                                <div class="card-header bg-primary text-white">

                                    <h5 class="card-title font-weight-bold">

                                        <i class="fas fa-layer-group"></i> Download End-of-Term Results

                                    </h5>

                                </div>

                                <div class="card-body">

                                    <form method="GET" action="bulk_download.php" class="mb-4">

                                        <input type="hidden" name="exam_type" value="endterm">

                                        <div class="row">

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

                                                <label for="session_id_end">Academic Session</label>

                                                <select name="session_id" id="session_id_end" class="form-control select2" required>

                                                    <option value="">-- Select Session --</option>

                                                    <?php

                                                    $sessionResult = $con->query("SELECT DISTINCT id, session_name FROM school_sessions ORDER BY session_name DESC");

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

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

                                                    }

                                                    ?>

                                                </select>

                                            </div>



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

                                                <label for="term_id_end">Term</label>

                                                <select name="term_id" id="term_id_end" class="form-control select2" required>

                                                    <option value="">-- Select Term --</option>

                                                </select>

                                            </div>



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

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

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

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

                                                    <?php

                                                    $classResult = $con->query("SELECT DISTINCT class FROM class_section ORDER BY class");

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

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

                                                    }

                                                    ?>

                                                </select>

                                            </div>



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

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

                                                <select name="section" id="section_end" class="form-control select2" required>

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

                                                    <?php

                                                    $armResult2 = $con->query("SELECT DISTINCT section FROM class_section ORDER BY section");

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

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

                                                    }

                                                    ?>

                                                </select>

                                            </div>

                                        </div>



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

                                            <button type="submit" name="download_bulk" class="btn btn-primary mt-3">

                                                <i class="fas fa-check-circle"></i> Download End-of-Term Results

                                            </button>

                                        </div>

                                    </form>

                                </div>

                            </div>

                        </div>



                    </div>

                </div>

            </section>

        </div>



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

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

    </div>



    <!-- jQuery and Bootstrap JS -->

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

    <script>

        $(document).ready(function() {

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

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

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

                if (selectedClass) {

                    $.ajax({

                        url: 'fetch_session.php',

                        type: 'POST',

                        data: {

                            class_name: selectedClass

                        },

                        success: function(response) {

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

                        }

                    });

                } else {

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

                }

            });

        });

    </script>

        <script>

        $(document).ready(function() {

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

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

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

                if (selectedClass) {

                    $.ajax({

                        url: 'fetch_session.php',

                        type: 'POST',

                        data: {

                            class_name: selectedClass

                        },

                        success: function(response) {

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

                        }

                    });

                } else {

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

                }

            });

        });

    </script>

</body>

</html>



