<?= $this->extend('layout/main_layout') ?>
<?= $this->section('content') ?>
<div class="container my-5">
    <h3 class="text-center text-success" style="font-family: Arial, sans-serif;">Enroll in Classes</h3>
    <?php if (!empty($selectedYear)): ?>
        <div class="text-center text-muted small">School Year: <?= esc($selectedYear) ?></div>
    <?php endif; ?>
</div>

<?php
// Put this near the very top of the file (before output), or right after the extend/section lines.
$tz = (string) (config('School')->attendance['timezone'] ?? user_timezone());
$deadlineObj = (new DateTime($lastDayOfRegistration, new DateTimeZone($tz)))->setTime(23, 59, 59);
$nowObj      = new DateTime('now', new DateTimeZone($tz));
$deadlinePassed = $nowObj > $deadlineObj;
$deadlineISO = $deadlineObj->format('Y-m-d\TH:i:sP'); // for JS
?>
<!-- Registration Info -->
<div class="alert alert-info mb-3">
    <p>Enrollment is the process of officially signing up your child for the upcoming school year.</p>
    <ul>
        <li>Last Day for Enrollment is <strong><?= esc(local_date($lastDayOfRegistration, 'm-d-Y')) ?></strong>.</li>
        <li>Once you click "Save", the enrollment status will change to <strong>admission under review</strong>.</li>
        <li>Payments are processed on the first day of school: <strong><?= esc(local_date($schoolStartDate, 'm-d-Y')) ?></strong>.</li>
    </ul>
</div>

<?php if (!empty($students)): ?>
    <form action="<?= base_url('/parent/enroll_classes_handler') ?>" method="post">
        <?= csrf_field() ?>
        <div class="table-responsive">
            <table class="table table-striped table-bordered align-middle">
                <thead>
                    <tr>
                        <th>#</th>
                        <th>School ID</th>
                        <th>First Name</th>
                        <th>Last Name</th>
                        <th>Age</th>
                        <th>Gender</th>
                        <th>Grade</th>
                        <th>Enroll</th>
                        <th>Withdraw</th>
                        <th>Status</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($students as $index => $student): ?>
                        <tr>
                            <td><?= $index + 1 ?></td>
                            <td><?= esc($student['school_id'] ?? 'N/A') ?></td>
                            <td><?= esc($student['firstname'] ?? 'N/A') ?></td>
                            <td><?= esc($student['lastname'] ?? 'N/A') ?></td>
                            <td><?= esc($student['age'] ?? 'N/A') ?></td>
                            <td><?= esc($student['gender'] ?? 'N/A') ?></td>
                            <td>
                                <?php
                                $section = $student['class_section'] ?? null;
                                echo $section
                                    ? esc(preg_replace_callback('/\byouth\b/i', fn($m) => ucfirst(strtolower($m[0])), $section))
                                    : 'Not Assigned';
                                ?>
                            </td>

                            <!-- Enroll Checkbox -->
                            <td>
                                <?php if ($student['enrollment_status'] === 'not enrolled'): ?>
                                    <?php $disableEnrollUI = $deadlinePassed || !$isEditable; ?>
                                    <input type="checkbox"
                                        name="enroll[]"
                                        value="<?= esc($student['id']) ?>"
                                        <?= $disableEnrollUI ? 'disabled' : '' ?>>
                                <?php elseif (in_array($student['enrollment_status'], ['enrolled', 'admission under review', 'payment pending', 'withdraw under review'])): ?>
                                    <input type="checkbox" checked disabled>
                                <?php else: ?>
                                    <input type="checkbox" disabled>
                                <?php endif; ?>
                            </td>

                            <!-- Withdraw Checkbox -->
                            <td>
                                <?php if ($student['enrollment_status'] === 'enrolled'): ?>
                                    <input type="checkbox" name="withdraw[]" value="<?= esc($student['id']) ?>" <?= !$isEditable ? 'disabled' : '' ?>>
                                <?php elseif ($student['enrollment_status'] === 'withdrawn'): ?>
                                    <input type="checkbox" checked disabled>
                                <?php else: ?>
                                    <input type="checkbox" disabled>
                                <?php endif; ?>
                            </td>

                            <!-- Status -->
                            <td>
                                <?php
                                $status = strtolower(trim($student['enrollment_status'] ?? ''));

                                switch ($status) {
                                    case 'admission under review':
                                        echo '<span class="badge bg-primary">admission under review</span>';
                                        break;
                                    case 'payment pending':
                                        echo '<span class="badge bg-warning text-dark">payment pending</span>';
                                        break;
                                    case 'enrolled':
                                        echo '<span class="badge bg-success">enrolled</span>';
                                        break;
                                    case 'withdraw under review':
                                        echo '<span class="badge bg-warning text-dark">withdraw under review</span>';
                                        break;
                                    case 'refund pending':
                                        echo '<span class="badge bg-info text-dark">refund pending</span>';
                                        break;
                                    case 'withdrawn':
                                        echo '<span class="badge bg-danger">withdrawn</span>';
                                        break;
                                    case 'waitlist':
                                        echo '<span class="badge bg-secondary">waitlist</span>'; // lighter and neutral
                                        break;
                                    case 'denied':
                                        echo '<span class="badge bg-danger">denied</span>'; // red stands out clearly
                                        break;
                                    case 'not enrolled':
                                        echo '<span class="badge bg-light text-dark">not enrolled</span>'; // very neutral
                                        break;
                                    default:
                                        echo '<span class="badge bg-light text-dark">unknown</span>';
                                }
                                ?>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>

        <!-- Save Button -->
        <?php
        $today = local_date(utc_now(), 'Y-m-d');
        $disableDueToDate = ($today >= $lastDayOfRegistration);
        // Allow submit for withdrawals even after deadline; keep editability guard only
        $disableSave = !$isEditable;
        ?>

        <div class="d-flex justify-content-center">
            <button type="submit"
                class="btn btn-lg btn-success <?= $disableSave ? 'disabled' : '' ?>"
                <?= $disableSave ? 'disabled' : '' ?>
                title="<?php
                        if (!$isEditable) {
                            echo 'Editing is not allowed for this record.';
                        }
                        ?>">
                Submit
            </button>
        </div>

        <br>
    </form>
<?php else: ?>
    <p>No students found for the selected school year. Please register your kids first.</p>
<?php endif; ?>
<!-- Enrollment Deadline Modal -->
<div class="modal fade" id="deadlineModal" tabindex="-1" aria-labelledby="deadlineModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-header bg-danger text-white">
                <h5 class="modal-title" id="deadlineModalLabel">Enrollment Closed</h5>
                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                Enrollment for the selected school year closed on
                <strong><?= esc($deadlineObj->format('m-d-Y')) ?></strong>.
                You can still request a withdrawal (if applicable), but new enrollments are no longer accepted.
                <br><br>
                If you believe this is an error, please contact the school office.
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">OK</button>
            </div>
        </div>
    </div>
</div>

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

<?= $this->section('scripts') ?>
<script>
    document.addEventListener("DOMContentLoaded", function() {
        // Values from PHP
        const deadlinePassed = <?= $deadlinePassed ? 'true' : 'false' ?>;
        const enrollmentDeadline = new Date("<?= esc($deadlineISO) ?>");

        // Bootstrap Modal
        const modalEl = document.getElementById('deadlineModal');
        const deadlineModal = modalEl ? new bootstrap.Modal(modalEl) : null;
        const showDeadlineModal = () => {
            if (deadlineModal) deadlineModal.show();
        };

        // The enrollment form
        const form = document.querySelector("form[action*='enroll_classes_handler']");

        // 1) Block clicking "enroll" checkboxes after deadline
        document.querySelectorAll("input[name='enroll[]']").forEach(cb => {
            cb.addEventListener("click", function(e) {
                if (deadlinePassed) {
                    e.preventDefault();
                    e.stopImmediatePropagation();
                    this.checked = false;
                    showDeadlineModal();
                }
            });
        });

        // 2) Prevent submission if any enroll[] is checked after deadline
        if (form) {
            form.addEventListener("submit", function(e) {
                const anyBoxesChecked = form.querySelectorAll("input[name='enroll[]']:checked").length > 0;
                const anyWithdrawChecked = form.querySelectorAll("input[name='withdraw[]']:checked").length > 0;

                if (!anyBoxesChecked && !anyWithdrawChecked) {
                    e.preventDefault();
                    alert("Please select at least one student to enroll or withdraw before saving.");
                    return;
                }

                if (deadlinePassed && anyBoxesChecked) {
                    e.preventDefault();
                    showDeadlineModal();
                    return;
                }

                // 3) If only withdrawals are selected, ask for a quick confirmation
                if (!anyBoxesChecked && anyWithdrawChecked) {
                    const ok = confirm('Confirm withdrawal request for the selected student(s)?');
                    if (!ok) {
                        e.preventDefault();
                        return;
                    }
                }
            });
        }
    });
</script>
<?= $this->endSection() ?>
