<?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();

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

include('includes/flash.php');



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

    header('location:logout.php');

    exit();

}



// Get all classes for dropdown

$class_options = [];

$class_query = mysqli_query($con, "SELECT DISTINCT class FROM class_section ORDER BY class ASC");

while ($row = mysqli_fetch_assoc($class_query)) {

    $class_options[] = $row['class'];

}



// Handle demotion

if (isset($_POST['demote_students'])) {

    if (!empty($_POST['student_ids'])) {

        foreach ($_POST['student_ids'] as $student_id) {

            $demote_to_class = $_POST['demote_class'][$student_id] ?? '';

            $demote_to_arm = $_POST['demote_arm'][$student_id] ?? '';



            if (!empty($demote_to_class) && !empty($demote_to_arm)) {

                $stmt = $con->prepare("UPDATE students SET class = ?, section = ?, demote_status = 1 WHERE id = ?");

                $stmt->bind_param("ssi", $demote_to_class, $demote_to_arm, $student_id);

                $stmt->execute();

            } else {

                flash_alert('Demotion class not selected for one or more students.');

            }

        }



        flash_alert('Selected students have been demoted successfully.');

    } else {

        flash_alert('No students selected.');

    }

}



// Handle restore

if (isset($_POST['restore_students'])) {

    if (!empty($_POST['restore_ids'])) {

        foreach ($_POST['restore_ids'] as $student_id) {

            $restore_to_class = $_POST['restore_class'][$student_id] ?? '';

            $restore_to_arm = $_POST['restore_arm'][$student_id] ?? '';



            if (!empty($restore_to_class) && !empty($restore_to_arm)) {

                $stmt = $con->prepare("UPDATE students SET class = ?, section = ?, demote_status = 0 WHERE id = ?");

                $stmt->bind_param("ssi", $restore_to_class, $restore_to_arm, $student_id);

                $stmt->execute();

            } else {

                flash_alert('Restore class not selected for one or more students.');

            }

        }

        flash_alert('Selected students have been restored successfully.');

    } else {

        flash_alert('No students selected for restore.');

    }

}



?>



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

                        </div>

                        <div>

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

                                Student Information Management

                                <span class="premium-subtitle text-secondary">Manage Demotion</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">Demote Students</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-user-minus"></i> Demote Students (Admitted Students Only)</h5>

                                    <div class="card-tools">

                                    </div>

                                </div>



                                <div class="card-body">

                                    <form method="GET">

                                        <div class="card card-primary">

                                            <div class="card-body row">

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

                                                    <label>Class</label>

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

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

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

                                                            <option value="<?= $class ?>" <?= @$_GET['class'] == $class ? 'selected' : '' ?>>

                                                                <?= $class ?>

                                                            </option>

                                                        <?php endforeach; ?>

                                                    </select>

                                                </div>

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

                                                    <label>Arm</label>

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

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

                                                        <?php

                                                        $section_query = mysqli_query($con, "SELECT DISTINCT section FROM class_section ORDER BY section ASC");

                                                        while ($row = mysqli_fetch_assoc($section_query)) {

                                                            $section = $row['section'];

                                                            echo "<option value='$section' " . (@$_GET['section'] == $section ? 'selected' : '') . ">$section</option>";

                                                        }

                                                        ?>

                                                    </select>

                                                </div>

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

                                                    <button type="submit" class="btn btn-primary mt-2"><i class="fas fa-search"></i> Fetch Students</button>

                                                </div>

                                            </div>

                                        </div>

                                    </form>





                                    <?php if (isset($_GET['class'], $_GET['section'])): ?>

                                        <?php

                                        $class = $_GET['class'];

                                        $section = $_GET['section'];

                                        $students_query = mysqli_query($con, "SELECT id, studentName, studentno, class FROM students WHERE class = '$class' AND section = '$section' AND status = 1");

                                        ?>



                                        <form method="POST">

                                            <input type="hidden" name="demote_students" value="1">

                                            <div class="card card-primary">

                                                <div class="card-header">

                                                    <h4 class="card-title"><i class="fas fa-users"></i> Students in <?= $class ?> - <?= $section ?></h4>

                                                </div>

                                                <div class="card-body">

                                                    <table id="example1" class="table table-bordered table-hover">

                                                        <thead>

                                                            <tr>

                                                                <th><input type="checkbox" onclick="toggleAll(this)"></th>

                                                                <th>Name</th>

                                                                <th>Admission No</th>

                                                                <th>Current Class</th>

                                                                <th>Demote To</th>

                                                            </tr>

                                                        </thead>

                                                        <tbody>

                                                            <?php while ($row = mysqli_fetch_assoc($students_query)): ?>

                                                                <tr>

                                                                    <td><input type="checkbox" name="student_ids[]" value="<?= $row['id'] ?>"></td>

                                                                    <td><?= $row['studentName'] ?></td>

                                                                    <td><?= $row['studentno'] ?></td>

                                                                    <td><?= $row['class'] ?></td>

                                                                    <td>

                                                                        <select name="demote_class[<?= $row['id'] ?>]" class="form-control" id="class1">

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

                                                                            <?php foreach ($class_options as $option): ?>

                                                                                <option value="<?= $option ?>"><?= $option ?></option>

                                                                            <?php endforeach; ?>

                                                                        </select>

                                                                        <select name="demote_arm[<?= $row['id'] ?>]" class="form-control" id="arm1" >

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

                                                                            <?php

                                                                            $section_query = mysqli_query($con, "SELECT DISTINCT section FROM class_section ORDER BY section ASC");

                                                                            while ($row = mysqli_fetch_assoc($section_query)) {

                                                                                $section = $row['section'];

                                                                                echo "<option value='$section' " . (@$_GET['section'] == $section ? 'selected' : '') . ">$section</option>";

                                                                            }

                                                                            ?>

                                                                        </select>

                                                                    </td>

                                                                </tr>

                                                            <?php endwhile; ?>

                                                        </tbody>

                                                    </table>

                                                </div>

                                                <div class="card-footer text-center">

                                                    <button type="submit" class="btn btn-danger"><i class="fas fa-arrow-down"></i> Demote Selected Students</button>

                                                </div>

                                            </div>

                                        </form>

                                    <?php endif; ?>



                                    <!-- Demoted Students Section -->

                                    <div class="card card-primary">

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

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

                                                <i class="fas fa-user-clock"></i> Recently Demoted Students

                                            </h5>

                                        </div>

                                        <div class="card-body">





                                            <?php

                                            $demoted_students = mysqli_query($con, "SELECT id, studentName, studentno, class, section, demote_status FROM students WHERE demote_status = 1 AND status = 1 ORDER BY class ASC");

                                            if (mysqli_num_rows($demoted_students) > 0): ?>

                                                <div class="table-responsive">

                                                    <table id="example11" class="table table-bordered table-hover">

                                                        <thead class="bg-light">

                                                            <tr>

                                                                <th>Name</th>

                                                                <th>Admission No</th>

                                                                <th>Current Class</th>

                                                                <th>Arm</th>

                                                                <th>Status</th>

                                                            </tr>

                                                        </thead>

                                                        <tbody>

                                                            <?php while ($row = mysqli_fetch_assoc($demoted_students)): ?>

                                                                <tr>

                                                                    <td><?= htmlspecialchars($row['studentName']) ?></td>

                                                                    <td><?= htmlspecialchars($row['studentno']) ?></td>

                                                                    <td><?= htmlspecialchars($row['class']) ?></td>

                                                                    <td><?= htmlspecialchars($row['section']) ?></td>

                                                                    <td><?php if (

                                                                            htmlspecialchars($row['demote_status']) == 1

                                                                        ) {

                                                                            echo "Active";

                                                                        } else {

                                                                            echo "Inactive";

                                                                        } ?></td>



                                                                </tr>

                                                            <?php endwhile; ?>

                                                        </tbody>

                                                    </table>

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

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

                                                            <i class="fas fa-user-clock"></i> Restore Demoted Students

                                                        </h5>

                                                    </div>

                                                    <form method="POST">

                                                        <input type="hidden" name="restore_students" value="1">

                                                        <div class="table-responsive mt-3">

                                                            <table class="table table-bordered table-striped">

                                                                <thead class="bg-light">

                                                                    <tr>

                                                                        <th><input type="checkbox" onclick="toggleAllRestore(this)"></th>

                                                                        <th>Name</th>

                                                                        <th>Admission No</th>

                                                                        <th>Current Class</th>

                                                                        <th>Section</th>

                                                                        <th>Restore To Class</th>

                                                                    </tr>

                                                                </thead>

                                                                <tbody>

                                                                    <?php

                                                                    mysqli_data_seek($demoted_students, 0); // Rewind result set

                                                                    while ($row = mysqli_fetch_assoc($demoted_students)):

                                                                    ?>

                                                                        <tr>

                                                                            <td><input type="checkbox" name="restore_ids[]" value="<?= $row['id'] ?>"></td>

                                                                            <td><?= htmlspecialchars($row['studentName']) ?></td>

                                                                            <td><?= htmlspecialchars($row['studentno']) ?></td>

                                                                            <td><?= htmlspecialchars($row['class']) ?></td>

                                                                            <td><?= htmlspecialchars($row['section']) ?></td>

                                                                            <td>

                                                                                <select name="restore_class[<?= $row['id'] ?>]" class="form-control" >

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

                                                                                    <?php foreach ($class_options as $option): ?>

                                                                                        <option value="<?= $option ?>"><?= $option ?></option>

                                                                                    <?php endforeach; ?>

                                                                                </select>

                                                                                <select name="restore_arm[<?= $row['id'] ?>]" class="form-control">

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

                                                                                    <?php

                                                                                    $section_query = mysqli_query($con, "SELECT DISTINCT section FROM class_section ORDER BY section ASC");

                                                                                    while ($row = mysqli_fetch_assoc($section_query)) {

                                                                                        $section = $row['section'];

                                                                                        echo "<option value='$section' " . (@$_GET['section'] == $section ? 'selected' : '') . ">$section</option>";

                                                                                    }

                                                                                    ?>

                                                                                </select>

                                                                            </td>

                                                                        </tr>

                                                                    <?php endwhile; ?>

                                                                </tbody>

                                                            </table>

                                                            <div class="text-center">

                                                                <button type="submit" class="btn btn-success">

                                                                    <i class="fas fa-arrow-up"></i> Restore Selected Students

                                                                </button>

                                                            </div>

                                                        </div>

                                                    </form>



                                                </div>

                                            <?php else: ?>

                                                <p class="text-muted">No demoted students found.</p>

                                            <?php endif; ?>

                                        </div>

                                    </div>





            </section>

        </div>



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

    </div>



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

    <script>

        function toggleAll(source) {

            const checkboxes = document.querySelectorAll("input[name='student_ids[]']");

            checkboxes.forEach(cb => cb.checked = source.checked);

        }

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

        $(document).ready(function() {

            $('#class,#section,#class1,#arm1,#classd,#armd').select2();

        });



        function toggleAllRestore(source) {

            const checkboxes = document.querySelectorAll("input[name='restore_ids[]']");

            checkboxes.forEach(cb => cb.checked = source.checked);

        }

    </script>



</body>



</html>