<?php
$activeStudents = $active_students ?? [];
$removedStudents = $removed_students ?? [];
$schoolYear = $school_year ?? '';
$activeSchoolYear = $active_school_year ?? '';
?>

<?= $this->extend('layout/management_layout') ?>
<?= $this->section('content') ?>

<div class="container-fluid">
    <div class="wrapper">
        <div class="content">
            <h2 class="text-center mt-4 mb-2">Student Removal</h2>
            <p class="text-center text-muted mb-4">
                Active school year: <?= esc($schoolYear !== '' ? $schoolYear : $activeSchoolYear) ?>.
                Active students have a class assignment in the active school year. Removed students are inactive or have no active-year assignment.
            </p>
            <?= $this->include('partials/flash_messages') ?>

            <div class="card mb-4 shadow-sm">
                <div class="card-header d-flex align-items-center justify-content-between">
                    <span>Active Students</span>
                    <span class="badge bg-primary"><?= count($activeStudents) ?></span>
                </div>
                <div class="card-body">
                    <div class="table-responsive">
                        <table id="activeStudentsTable" class="table table-striped table-hover align-middle">
                            <thead class="table-dark">
                                <tr>
                                    <th>School ID</th>
                                    <th>First Name</th>
                                    <th>Last Name</th>
                                    <th>Class</th>
                                    <th style="min-width: 140px;">Action</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php if (!empty($activeStudents)): ?>
                                    <?php foreach ($activeStudents as $student): ?>
                                        <tr>
                                            <td><?= esc($student['school_id'] ?? '') ?></td>
                                            <td><?= esc($student['firstname'] ?? '') ?></td>
                                            <td><?= esc($student['lastname'] ?? '') ?></td>
                                            <td><?= esc($student['class_section_name'] ?? 'No class assigned') ?></td>
                                            <td>
                                                <form method="post" action="<?= site_url('administrator/students/set-active') ?>" class="d-inline">
                                                    <?= csrf_field() ?>
                                                    <input type="hidden" name="student_id" value="<?= (int)($student['id'] ?? 0) ?>">
                                                    <input type="hidden" name="is_active" value="0">
                                                    <button type="submit" class="btn btn-outline-danger btn-sm" onclick="return confirm('Remove this student from classes and attendance?');">
                                                        Remove
                                                    </button>
                                                </form>
                                            </td>
                                        </tr>
                                    <?php endforeach; ?>
                                <?php else: ?>
                                    <tr>
                                        <td colspan="5" class="text-center">No active students found.</td>
                                    </tr>
                                <?php endif; ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>

            <div class="card shadow-sm">
                <div class="card-header d-flex align-items-center justify-content-between">
                    <span>Removed Students</span>
                    <span class="badge bg-secondary"><?= count($removedStudents) ?></span>
                </div>
                <div class="card-body">
                    <div class="table-responsive">
                        <table id="removedStudentsTable" class="table table-striped table-hover align-middle">
                            <thead class="table-dark">
                                <tr>
                                    <th>School ID</th>
                                    <th>First Name</th>
                                    <th>Last Name</th>
                                    <th>Last Class</th>
                                    <th style="min-width: 140px;">Action</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php if (!empty($removedStudents)): ?>
                                    <?php foreach ($removedStudents as $student): ?>
                                        <tr>
                                            <td><?= esc($student['school_id'] ?? '') ?></td>
                                            <td><?= esc($student['firstname'] ?? '') ?></td>
                                            <td><?= esc($student['lastname'] ?? '') ?></td>
                                            <td><?= esc($student['class_section_name'] ?? 'No class assigned') ?></td>
                                            <td>
                                                <form method="post" action="<?= site_url('administrator/students/set-active') ?>" class="d-inline">
                                                    <?= csrf_field() ?>
                                                    <input type="hidden" name="student_id" value="<?= (int)($student['id'] ?? 0) ?>">
                                                    <input type="hidden" name="is_active" value="1">
                                                    <button type="submit" class="btn btn-outline-success btn-sm" onclick="return confirm('Restore this student to classes and attendance?');">
                                                        Restore
                                                    </button>
                                                </form>
                                            </td>
                                        </tr>
                                    <?php endforeach; ?>
                                <?php else: ?>
                                    <tr>
                                        <td colspan="5" class="text-center">No removed students found.</td>
                                    </tr>
                                <?php endif; ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<?= $this->endSection() ?>

<?= $this->section('scripts') ?>
<script>
    document.addEventListener('DOMContentLoaded', () => {
        if (window.jQuery && typeof jQuery.fn.DataTable === 'function') {
            if (document.getElementById('activeStudentsTable')) {
                jQuery('#activeStudentsTable').DataTable();
            }
            if (document.getElementById('removedStudentsTable')) {
                jQuery('#removedStudentsTable').DataTable();
            }
        }
    });
</script>
<?= $this->endSection() ?>
