<?= $this->extend('layout/management_layout') ?>
<?= $this->section('content') ?>
<div class="container-fluid">
    <div class="wrapper">
        <div class="content">
            <br>
            <h2 class="text-center mt-4 mb-3">School Calendar</h2>
            <?php
                $addParams = array_filter([
                    'school_year' => $schoolYear ?? '',
                    'semester'    => $semester ?? '',
                ], fn($value) => $value !== '');
                $addQuery = $addParams ? ('?' . http_build_query($addParams)) : '';
            ?>
            <div class="d-flex gap-2 mb-3 justify-content-end">
                <a href="/administrator/calendar_add_event<?= $addQuery ?>" class="btn btn-success">Add New Event</a>
            </div>
            <br>

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

            <?php
                $emailStatus = session()->getFlashdata('email_status');
                $emailStatusClass = session()->getFlashdata('email_status_class') ?? 'info';
            ?>
            <?php if ($emailStatus): ?>
                <div class="alert alert-<?= esc($emailStatusClass) ?>">
                    <?= esc($emailStatus) ?>
                </div>
            <?php endif; ?>

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

            <div class="table-responsive mb-3">
                <table id="myTable" class="display">
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>Title</th>
                            <th>Type</th>
                            <th>Description</th>
                            <th>Date</th>
                            <th>Notify Parent</th>
                            <th>Notify Teacher</th>
                            <th>Notify Admin</th>
                            <th>No School</th>
                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php if (!empty($events)): ?>
                            <?php if ($emailStatus): ?>
                                <tr class="table-warning">
                                    <td colspan="10" class="text-center small">
                                        <?= esc($emailStatus) ?>
                                    </td>
                                </tr>
                            <?php endif; ?>
                            <?php foreach ($events as $event): ?>
                            <tr>
                                    <td><?= esc($event['id']) ?></td>
                                    <td><?= esc($event['title']) ?></td>
                                    <td><?= esc($event['event_type'] ?? '—') ?></td>
                                    <td><?= esc($event['description']) ?></td>
                                    <td><?= esc(!empty($event['date']) ? date('m-d-Y', strtotime($event['date'])) : '') ?></td>
                                    <td>
                                        <input type="checkbox" disabled <?= $event['notify_parent'] ? 'checked' : '' ?>>
                                    </td>
                                    <td>
                                        <input type="checkbox" disabled <?= $event['notify_teacher'] ? 'checked' : '' ?>>
                                    </td>
                                    <td>
                                        <input type="checkbox" disabled <?= $event['notify_admin'] ? 'checked' : '' ?>>
                                    </td>
                                    <td>
                                        <input type="checkbox" disabled <?= !empty($event['no_school']) ? 'checked' : '' ?>>
                                    </td>
                                    <td>
                                        <a href="/administrator/calendar_edit/<?= $event['id'] ?>" class="btn btn-primary btn-sm">Edit</a>
                                        <form action="/administrator/calendar_delete/<?= $event['id'] ?>" method="post" style="display:inline;">
                                            <?= csrf_field() ?>
                                            <button type="submit" class="btn btn-danger btn-sm"
                                                onclick="return confirm('Are you sure you want to delete this event?')">Delete</button>
                                        </form>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                        <?php else: ?>
                            <tr>
                                <td colspan="10" class="text-center">No events found</td>
                            </tr>
                        <?php endif; ?>
                    </tbody>

                </table>
            </div>

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

<?= $this->section('scripts') ?>
<script>
    $(document).ready(function() {
        $('#myTable').DataTable();
    });
</script>
<?= $this->endSection() ?>
