<?= $this->extend('layout/management_layout') ?>
<?= $this->section('content') ?>
<div class="container-fluid">
    <div class="wrapper">
        <div class="content"></div>
                <h2 class="text-center mt-4 mb-3">Classes Lists</h2>

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

                <?php if (!empty($classSections)): ?>
                <div id="classAccordion">
                    <?php foreach ($classSections as $index => $classSection): ?>
                    <div class="card">
                        <div class="card-header" id="heading<?= $index ?>">
                            <h2 class="mb-0">
                                <button class="btn btn-link w-100 text-start" type="button"
                                    data-bs-toggle="collapse"
                                    data-bs-target="#collapse<?= $index ?>"
                                    aria-expanded="<?= $index === 0 ? 'true' : 'false' ?>"
                                    aria-controls="collapse<?= $index ?>">
                                    Grade : <?= esc($classSection['class_section_name']) ?>
                                </button>
                            </h2>
                        </div>

                        <div id="collapse<?= $index ?>" class="collapse" aria-labelledby="heading<?= $index ?>" data-bs-parent="#classAccordion">
                            <div class="card-body">
                                <h5>Main Teacher: 
    <?= !empty($classSection['main_teachers']) 
        ? esc(implode(', ', $classSection['main_teachers'])) 
        : 'None' ?>
</h5>

<h5>Teacher-Assistant: 
    <?= !empty($classSection['teacher_assistants']) 
        ? esc(implode(', ', $classSection['teacher_assistants'])) 
        : 'None' ?>
</h5>

                                <?php $displaySemester = $selectedSemester ?? ''; ?>
                                <?php if ($displaySemester === '' && isset($classSection['semester'])): ?>
                                    <?php $displaySemester = $classSection['semester']; ?>
                                <?php endif; ?>
                                <h5>Semester: <?= esc($displaySemester ?: 'N/A') ?></h5>
                                <h5>School Year: <?= esc($classSection['school_year']) ?></h5>
                                <p><?= esc($classSection['description']) ?></p>

                                <?php if (!empty($classSection['students'])): ?>
                                <table id="myTable<?= $index ?>" class="display">
                                    <thead>
                                        <tr>
                                            <th>School ID</th>
                                            <th>First Name</th>
                                            <th>Last Name</th>
                                            <th>Age</th>
                                            <th>Gender</th>
                                            <th>Photo Consent</th>
                                            <th>Tuition Paid</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php foreach ($classSection['students'] as $student): ?>
                                        <tr>
                                            <td><?= esc($student['school_id']) ?></td>
                                            <td>
                                                <?php $sid = (int)($student['id'] ?? 0); ?>
                                                <?php if ($sid): ?>
                                                  <a href="#" class="text-decoration-none" data-family-student-id="<?= $sid ?>">
                                                    <?= esc($student['firstname']) ?>
                                                  </a>
                                                <?php else: ?>
                                                  <?= esc($student['firstname']) ?>
                                                <?php endif; ?>
                                            </td>
                                            <td>
                                                <?php if (!empty($sid)): ?>
                                                  <a href="#" class="text-decoration-none" data-family-student-id="<?= $sid ?>">
                                                    <?= esc($student['lastname']) ?>
                                                  </a>
                                                <?php else: ?>
                                                  <?= esc($student['lastname']) ?>
                                                <?php endif; ?>
                                            </td>
                                            <td><?= esc($student['age']) ?></td>
                                            <td><?= esc($student['gender']) ?></td>
                                            <td><?= esc($student['photo_consent']) ?></td>
                                            <td><?= esc($student['tuition_paid']) ?></td>
                                        </tr>
                                        <?php endforeach; ?>
                                    </tbody>
                                </table>
                                <?php else: ?>
                                <p>No students registered in this class section.</p>
                                <?php endif; ?>

                            </div>
                        </div>
                    </div>
                    <?php endforeach; ?>
                </div>
                <?php else: ?>
                <p>No class sections available.</p>
                <?php endif; ?>
            </div>
        </div>
    </div>
</div>
<?= $this->endSection() ?>

<?= $this->section('scripts') ?>
<script>
    $(document).ready(function() {
        <?php foreach ($classSections as $index => $section): ?>
            $('#myTable<?= $index ?>').DataTable({
                "paging": true,          // enable pagination
                "lengthChange": true,    // allow changing number of rows per page
                "pageLength": 50,        // default rows per page
                "info": true,            // show "Showing X to Y of Z entries"
                "language": {
                    "info": "Showing _START_ to _END_ of _TOTAL_ entries (Page _PAGE_ of _PAGES_)",
                    "infoEmpty": "No entries to show",
                    "infoFiltered": "(filtered from _MAX_ total entries)",
                    "lengthMenu": "Show _MENU_ entries per page"
                }
            });
        <?php endforeach; ?>
    });
</script>


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