<?= $this->extend('layouts/main') ?>
<?= $this->section('content') ?>

<div class="container-fluid py-4">

    <div class="d-flex align-items-center gap-2 mb-4">
        <a href="/exam-event" class="btn btn-outline-secondary btn-sm">
            <i class="bi bi-arrow-left"></i>
        </a>
        <div>
            <h4 class="mb-0 fw-bold">Edit Event Ujian</h4>
            <small class="text-muted">Ubah informasi event ujian: <?= esc($event['name']) ?></small>
        </div>
    </div>

    <?php if (session()->getFlashdata('error')): ?>
        <div class="alert alert-danger alert-dismissible fade show">
            <?= session()->getFlashdata('error') ?>
            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
        </div>
    <?php endif; ?>

    <?php if (session()->getFlashdata('errors')): ?>
        <div class="alert alert-danger">
            <ul class="mb-0">
                <?php foreach (session()->getFlashdata('errors') as $err): ?>
                    <li><?= esc($err) ?></li>
                <?php endforeach; ?>
            </ul>
        </div>
    <?php endif; ?>

    <form action="/exam-event/update/<?= $event['id'] ?>" method="POST">
        <?= csrf_field() ?>

        <div class="row g-4 justify-content-center">
            <div class="col-lg-8">
                <div class="card shadow-sm border-0 mb-4">
                    <div class="card-header fw-semibold bg-white border-bottom py-3">
                        <i class="bi bi-pencil me-2 text-warning"></i>Informasi Ujian
                    </div>
                    <div class="card-body p-4">

                        <!-- Nama Event -->
                        <div class="mb-3">
                            <label class="form-label fw-bold">Nama Event Ujian <span class="text-danger">*</span></label>
                            <input type="text" name="name" class="form-control" value="<?= old('name', $event['name']) ?>" required>
                            <div class="form-text">Nama lengkap event ujian yang akan dicetak di Kop/Judul dokumen.</div>
                        </div>

                        <!-- Kode Singkat Event -->
                        <div class="mb-3">
                            <label class="form-label fw-bold">Kode Singkat Event <span class="text-danger">*</span></label>
                            <input type="text" name="event_code" class="form-control" value="<?= old('event_code', $event['event_code']) ?>" required>
                            <div class="form-text">Kode singkat yang digunakan khusus untuk penomoran peserta ujian (misal: ASTS).</div>
                        </div>

                        <!-- Periode Akademik -->
                        <div class="mb-3">
                            <label class="form-label fw-bold">Periode Akademik <span class="text-danger">*</span></label>
                            <select name="academic_period_id" class="form-select" required>
                                <option value="">-- Pilih Periode --</option>
                                <?php foreach ($periods as $p): ?>
                                    <option value="<?= $p['id'] ?>" <?= (old('academic_period_id', $event['academic_period_id']) == $p['id']) ? 'selected' : '' ?>>
                                        <?= esc($p['year_name']) ?> — Semester <?= esc($p['semester']) ?> <?= $p['is_active'] ? '(Aktif)' : '' ?>
                                    </option>
                                <?php endforeach; ?>
                            </select>
                        </div>

                        <!-- Tanggal Pelaksanaan -->
                        <div class="row g-3 mb-3">
                            <div class="col-md-6">
                                <label class="form-label fw-bold">Tanggal Mulai <span class="text-danger">*</span></label>
                                <input type="date" name="start_date" class="form-control" value="<?= old('start_date', $event['start_date']) ?>" required>
                            </div>
                            <div class="col-md-6">
                                <label class="form-label fw-bold">Tanggal Selesai <span class="text-danger">*</span></label>
                                <input type="date" name="end_date" class="form-control" value="<?= old('end_date', $event['end_date']) ?>" required>
                            </div>
                        </div>

                        <!-- Tingkat Jenjang Ujian (Multiple Checkboxes) -->
                        <div class="mb-3">
                            <label class="form-label fw-bold d-block">Tingkat Kelas yang Mengikuti Ujian <span class="text-danger">*</span></label>
                            <div class="border rounded p-3 bg-light bg-opacity-50">
                                <?php 
                                $checkedGrades = explode(',', $event['grade_levels']);
                                foreach ($grades as $g): 
                                    $isChecked = in_array($g['name'], $checkedGrades);
                                ?>
                                <div class="form-check form-check-inline me-4">
                                    <input class="form-check-input" type="checkbox" name="grade_levels[]" id="grade_<?= esc($g['name']) ?>" value="<?= esc($g['name']) ?>" <?= $isChecked ? 'checked' : '' ?>>
                                    <label class="form-check-label fw-semibold" for="grade_<?= esc($g['name']) ?>">Kelas <?= esc($g['name']) ?></label>
                                </div>
                                <?php endforeach; ?>
                            </div>
                            <div class="form-text">Tentukan tingkat mana saja yang akan dimasukkan ke dalam daftar alokasi peserta ujian.</div>
                        </div>

                    </div>
                </div>

                <div class="card shadow-sm border-0 mb-4">
                    <div class="card-header fw-semibold bg-white border-bottom py-3">
                        <i class="bi bi-file-earmark-text me-2 text-success"></i>Tata Tertib Ujian (Event-Specific)
                    </div>
                    <div class="card-body p-4">

                        <div class="mb-3">
                            <label class="form-label fw-bold">Tata Tertib Pengawas</label>
                            <textarea name="proctor_rules" class="form-control" rows="6" placeholder="Masukkan tata tertib pengawas per baris..."><?= old('proctor_rules', $event['proctor_rules'] ?: ($schoolSettings['proctor_rules'] ?? '')) ?></textarea>
                            <div class="form-text">Tuliskan setiap aturan pada baris baru (tekan Enter). Kosongkan untuk menggunakan default.</div>
                        </div>

                        <div class="mb-3">
                            <label class="form-label fw-bold">Tata Tertib Peserta</label>
                            <textarea name="student_rules" class="form-control" rows="6" placeholder="Masukkan tata tertib peserta per baris..."><?= old('student_rules', $event['student_rules'] ?: ($schoolSettings['student_rules'] ?? '')) ?></textarea>
                            <div class="form-text">Tuliskan setiap aturan pada baris baru (tekan Enter). Kosongkan untuk menggunakan default.</div>
                        </div>
                    </div>
                </div>

                <div class="d-flex justify-content-end gap-2">
                    <a href="/exam-event" class="btn btn-outline-secondary px-4">Batal</a>
                    <button type="submit" class="btn btn-primary px-4"><i class="bi bi-save me-1"></i>Simpan Perubahan</button>
                </div>
            </div>
        </div>
    </form>

</div>

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