<?= $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">Add Event</h2>

        <?php if (isset($validation)): ?>
            <div class="alert alert-danger">
                <?= $validation->listErrors(); ?>
            </div>
        <?php endif; ?>

        <?php
        $request = service('request');
        $prefilledSchoolYear = set_value('school_year');
        if ($prefilledSchoolYear === '') {
            $prefilledSchoolYear = $request->getGet('school_year') ?: (function_exists('getSchoolYear') ? (string) getSchoolYear() : '');
        }
        $prefilledSemester = set_value('semester');
        if ($prefilledSemester === '') {
            $prefilledSemester = $request->getGet('semester') ?: (function_exists('getSemester') ? (string) getSemester() : '');
        }
        ?>
        <form action="/administrator/calendar_add_event" method="post">
            <?= csrf_field() ?>

            <div class="form-group mb-3">
                <div class="form-check form-switch mb-2">
                    <input class="form-check-input" type="checkbox" id="no_school" name="no_school" value="1"
                        <?= set_checkbox('no_school', '1') ?>>
                    <label class="form-check-label" for="no_school">No School Day</label>
                </div>
                <label for="title">Title<span class="text-danger">*</span></label>
                <input type="text" id="title" name="title" class="form-control"
                    value="<?= set_value('title'); ?>" required>
            </div>

            <div class="form-group mb-3">
                <label for="description">Description:</label>
                <textarea id="description" name="description" class="form-control"><?= set_value('description'); ?></textarea>
            </div>

            <div class="form-group mb-3">
                <label for="event_type">Event Type:</label>
                <select id="event_type" name="event_type" class="form-select"
                    <?= empty($eventTypes) ? 'disabled' : '' ?>>
                    <option value="">— Select event type —</option>
                    <?php foreach ($eventTypes ?? [] as $type): ?>
                        <option value="<?= esc($type) ?>" <?= set_select('event_type', $type) ?>>
                            <?= esc($type) ?>
                        </option>
                    <?php endforeach; ?>
                </select>
            </div>

            <div class="form-group mb-3">
                <label for="start">Event Date <span class="text-danger">*</span></label>
                <input type="date" id="start" name="start" class="form-control"
                    value="<?= set_value('start'); ?>" required>
            </div>

            <div class="form-group mt-3">
                <label>Calendar Display:</label><br>
                <div class="form-check form-check-inline">
                    <input class="form-check-input" type="checkbox" id="notify_parent" name="notify_parent" value="1"
                        <?= set_checkbox('notify_parent', '1') ?>>
                    <label class="form-check-label" for="notify_parent">Parents</label>
                </div>
                <div class="form-check form-check-inline">
                    <input class="form-check-input" type="checkbox" id="notify_teacher" name="notify_teacher" value="1"
                        <?= set_checkbox('notify_teacher', '1') ?>>
                    <label class="form-check-label" for="notify_teacher">Teachers</label>
                </div>
                <div class="form-check form-check-inline">
                    <input class="form-check-input" type="checkbox" id="notify_admin" name="notify_admin" value="1"
                        <?= set_checkbox('notify_admin', '1') ?>>
                    <label class="form-check-label" for="notify_admin">Admins</label>
                </div>
            </div>

            <div class="form-group mt-3">
                <label>Email notifications (optional):</label><br>
                <div class="form-check form-check-inline">
                    <input class="form-check-input" type="checkbox" id="send_email_parent" name="send_email_parent" value="1"
                        <?= set_checkbox('send_email_parent', '1') ?>>
                    <label class="form-check-label" for="send_email_parent">Send to Parents</label>
                </div>
                <div class="form-check form-check-inline">
                    <input class="form-check-input" type="checkbox" id="send_email_teacher" name="send_email_teacher" value="1"
                        <?= set_checkbox('send_email_teacher', '1') ?>>
                    <label class="form-check-label" for="send_email_teacher">Send to Teachers</label>
                </div>
                <div class="form-check form-check-inline">
                    <input class="form-check-input" type="checkbox" id="send_email_admin" name="send_email_admin" value="1"
                        <?= set_checkbox('send_email_admin', '1') ?>>
                    <label class="form-check-label" for="send_email_admin">Send to Admins</label>
                </div>
                <small class="form-text text-muted">Emails are dispatched immediately upon saving the event.</small>
            </div>

            <div class="row g-3 mt-2">
                <div class="col-md-6">
                <label for="school_year" class="form-label">School Year</label>
                <input type="text" id="school_year" name="school_year" class="form-control" value="<?= esc($prefilledSchoolYear) ?>" placeholder="e.g. 2025-2026">
            </div>
            <div class="col-md-6">
                <label for="semester" class="form-label">Semester</label>
                <select id="semester" name="semester" class="form-select">
                    <option value="">—</option>
                    <option value="Fall"   <?= (strcasecmp($prefilledSemester, 'Fall') === 0 ? 'selected' : '') ?>>Fall</option>
                    <option value="Spring" <?= (strcasecmp($prefilledSemester, 'Spring') === 0 ? 'selected' : '') ?>>Spring</option>
                </select>
            </div>
            </div>

            <br>
            <div class="d-flex gap-2 mb-3 justify-content-end">
                <button type="submit" class="btn btn-primary">Add Event</button>
                <a href="/administrator/calendar_view" class="btn btn-secondary">Cancel</a>
            </div>

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