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

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

        <form action="/administrator/update/<?= $event['id']; ?>" method="post">
            <?= csrf_field(); ?>

            <div class="form-group mb-3">
                <label>Calendar Display:</label><br>
                <div class="form-check form-check-inline">
                    <input class="form-check-input" type="checkbox" name="notify_parent" value="1"
                        <?= $event['notify_parent'] ? 'checked' : '' ?>>
                    <label class="form-check-label">Parents</label>
                </div>
                <div class="form-check form-check-inline">
                    <input class="form-check-input" type="checkbox" name="notify_teacher" value="1"
                        <?= $event['notify_teacher'] ? 'checked' : '' ?>>
                    <label class="form-check-label">Teachers</label>
                </div>
                <div class="form-check form-check-inline">
                    <input class="form-check-input" type="checkbox" name="notify_admin" value="1"
                        <?= $event['notify_admin'] ? 'checked' : '' ?>>
                    <label class="form-check-label">Admins</label>
                </div>
            </div>

            <div class="form-group mb-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="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"
                        <?= !empty($event['no_school']) ? 'checked' : '' ?>>
                    <label class="form-check-label" for="no_school">No School Day</label>
                </div>
                <label for="title">Title:</label>
                <input type="text" id="title" name="title" class="form-control"
                    value="<?= esc($event['title']); ?>" required>
            </div>

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

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

            <div class="form-group mb-3">
                <label for="start">Event Date:</label>
                <input type="date" id="start" name="start" class="form-control"
                    value="<?= esc($event['date']); ?>" required>
            </div>
            <div class="row g-3 mb-3">
                <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($event['school_year'] ?? (function_exists('getSchoolYear')?getSchoolYear():'')) ?>" placeholder="e.g. 2025-2026">
                </div>
                <div class="col-md-6">
                    <label for="semester" class="form-label">Semester</label>
                    <?php $semVal = (string)($event['semester'] ?? (function_exists('getSemester')?getSemester():'')); ?>
                    <select id="semester" name="semester" class="form-select">
                        <option value="">—</option>
                        <option value="Fall"   <?= (strcasecmp($semVal,'Fall')===0?'selected':'') ?>>Fall</option>
                        <option value="Spring" <?= (strcasecmp($semVal,'Spring')===0?'selected':'') ?>>Spring</option>
                    </select>
                </div>
            </div>
<div class="d-flex gap-2 mb-3 justify-content-end">
    <button type="submit" class="btn btn-success">Update Event</button>
    <a href="/administrator/calendar_view" class="btn btn-secondary">Cancel</a>
</div>

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