<?= $this->extend('layout/main_layout') ?>

<?= $this->section('content') ?>
<div class="container-fluid">
    <div class="row">
        <!-- Sidebar -->
        <nav class="col-md-3 col-lg-2 d-md-block bg-light sidebar">
            <div class="sidebar-sticky pt-3">
                <ul class="nav flex-column">
                    <li class="nav-item">
                        <a class="nav-link active" href="#">
                            <i class="bi bi-house-door"></i>
                            Dashboard
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="/parent/attendance">
                            <i class="bi bi-calendar-check"></i>
                            Attendance
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="/parent/register_kid_form">
                            <i class="bi bi-person-plus"></i>
                            Register Kids
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="/parent/enroll_classes">
                            <i class="bi bi-pencil-square"></i>
                            Enroll in Classes
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="/parent/payment">
                            <i class="bi bi-credit-card"></i>
                            Payment
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="/parent/messages">
                            <i class="bi bi-chat-dots"></i>
                            Messages
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="/parent/support">
                            <i class="bi bi-life-preserver"></i>
                            Support
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="calendar">
                            <i class="bi bi-calendar"></i>
                            Calendar
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="/parent/contact">
                            <i class="bi bi-telephone"></i>
                            Contact Us
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="/parent/scores">
                            <i class="bi bi-clipboard-data"></i>
                            Grades
                        </a>
                    </li>
                </ul>
            </div>
        </nav>

        <!-- Main Content -->
        <main class="col-md-9 ml-sm-auto col-lg-10 px-md-4">
            <div class="pt-3 pb-2 mb-3 border-bottom" style="margin-left: -2in;">
                <h2>Enroll in Classes</h2>
                <h5 class="h5 mt-2">Enroll your kids in the available classes for the upcoming term.</h5>
            </div>

            <?php if (session()->getFlashdata('success')): ?>
                <div class="alert alert-success"><?= session()->getFlashdata('success') ?></div>
            <?php endif; ?>
            <?php if (session()->getFlashdata('error')): ?>
                <div class="alert alert-danger"><?= session()->getFlashdata('error') ?></div>
            <?php endif; ?>

            <div class="table-responsive">
                <form action="<?= base_url('/parent/enroll_classes_handler') ?>" method="post">
                    <table class="table table-striped table-sm">
                        <thead>
                            <tr>
                                <th>Select</th>
                                <th>First Name</th>
                                <th>Last Name</th>
                                <th>Age</th>
                                <th>Gender</th>
                                <th>Grade</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php if (!empty($students)): ?>
                                <?php foreach ($students as $student): ?>
                                    <tr>
                                        <td>
                                            <input type="checkbox" name="enroll[]" value="<?= $student['student_id'] ?>">
                                        </td>
                                        <td><?= $student['firstname'] ?></td>
                                        <td><?= $student['lastname'] ?></td>
                                        <td><?= $student['age'] ?></td>
                                        <td><?= $student['gender'] ?></td>
                                        <td><?= $student['grade'] ?></td>
                                    </tr>
                                <?php endforeach; ?>
                            <?php else: ?>
                                <tr>
                                    <td colspan="6">No students found. Please register your kids first.</td>
                                </tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                    <div class="form-group">
                        <label for="class_section_id">Select Class</label>
                        <select class="form-control" id="class_section_id" name="class_section_id" required>
                            <option value="">Select Class</option>
                            <?php foreach ($classes as $class): ?>
                                <option value="<?= $class['class_section_id'] ?>"><?= $class['class_name'] ?>
                                    (<?= $class['schedule'] ?>)</option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <button type="submit" class="btn btn-primary">Enroll Selected Students</button>
                </form>
            </div>
        </main>
    </div>
</div>
<?= $this->endSection() ?>