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

<div class="d-flex align-items-center gap-2 mb-3">
    <a href="/schedule-generator" class="btn btn-sm btn-outline-secondary">
        <i class="bi bi-arrow-left"></i>
    </a>
    <div>
        <h5 class="mb-0 fw-semibold"><i class="bi bi-plus-circle me-2 text-primary"></i>Buat Jadwal Baru</h5>
        <small class="text-muted">Isi informasi dasar jadwal sebelum generate</small>
    </div>
</div>

<div class="row justify-content-center">
    <div class="col-lg-7">
        <div class="card border-0 shadow-sm">
            <div class="card-body p-4">
                <form action="/schedule-generator/store" method="post">
                    <?= csrf_field() ?>

                    <div class="mb-3">
                        <label class="form-label fw-semibold">Nama Jadwal <span class="text-danger">*</span></label>
                        <input type="text" name="name" class="form-control"
                               placeholder="cth: Jadwal Semester Ganjil 2025/2026"
                               value="<?= old('name') ?>" required>
                        <div class="form-text">Nama deskriptif untuk membedakan antar jadwal.</div>
                    </div>

                    <div class="row g-3 mb-3">
                        <div class="col-md-6">
                            <label class="form-label fw-semibold">Tahun Ajaran <span class="text-danger">*</span></label>
                            <select name="academic_year_id" id="selYear" class="form-select" required>
                                <option value="">-- Pilih Tahun Ajaran --</option>
                                <?php foreach ($years as $y): ?>
                                <option value="<?= $y['id'] ?>"
                                    <?= old('academic_year_id') == $y['id'] ? 'selected' : '' ?>>
                                    <?= esc($y['name']) ?>
                                    <?php if ($y['is_active']): ?>(Aktif)<?php endif; ?>
                                </option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label fw-semibold">Semester <span class="text-danger">*</span></label>
                            <select name="academic_period_id" id="selPeriod" class="form-select" required>
                                <option value="">-- Pilih tahun dulu --</option>
                            </select>
                        </div>
                    </div>

                    <div class="mb-3">
                        <label class="form-label fw-semibold">Hari Aktif <span class="text-danger">*</span></label>
                        <div class="d-flex gap-2 flex-wrap">
                            <?php
                            $dayList  = [1=>'Senin',2=>'Selasa',3=>'Rabu',4=>'Kamis',5=>'Jumat',6=>'Sabtu'];
                            $oldDays  = old('active_days') ?? ['1','2','3','4','5'];
                            ?>
                            <?php foreach ($dayList as $num => $label): ?>
                            <div class="form-check form-check-inline">
                                <input class="form-check-input" type="checkbox"
                                       name="active_days[]" value="<?= $num ?>"
                                       id="day<?= $num ?>"
                                       <?= in_array((string)$num, (array)$oldDays) ? 'checked' : '' ?>>
                                <label class="form-check-label" for="day<?= $num ?>"><?= $label ?></label>
                            </div>
                            <?php endforeach; ?>
                        </div>
                        <div class="form-text">Centang hari-hari di mana pembelajaran berlangsung.</div>
                    </div>

                    <div class="mb-4">
                        <label class="form-label fw-semibold">Catatan</label>
                        <textarea name="notes" class="form-control" rows="2"
                                  placeholder="Opsional..."><?= old('notes') ?></textarea>
                    </div>

                    <div class="d-flex gap-2">
                        <button type="submit" class="btn btn-primary">
                            <i class="bi bi-check-lg me-1"></i>Simpan & Lanjutkan
                        </button>
                        <a href="/schedule-generator" class="btn btn-outline-secondary">Batal</a>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

<?= $this->endSection() ?>
<?= $this->section('scripts') ?>
<script>
// Load periods saat year dipilih
document.getElementById('selYear').addEventListener('change', function () {
    const yearId = this.value;
    const sel    = document.getElementById('selPeriod');
    sel.innerHTML = '<option value="">Memuat...</option>';

    if (!yearId) {
        sel.innerHTML = '<option value="">-- Pilih tahun dulu --</option>';
        return;
    }

    fetch('/schedule-generator/get-periods?year_id=' + yearId, {
        headers: { 'X-Requested-With': 'XMLHttpRequest' }
    })
    .then(r => r.json())
    .then(data => {
        if (!data.length) {
            sel.innerHTML = '<option value="">Tidak ada semester</option>';
            return;
        }
        sel.innerHTML = '<option value="">-- Pilih Semester --</option>';
        data.forEach(p => {
            sel.innerHTML += `<option value="${p.id}">${p.semester} (${p.year_name})</option>`;
        });
        // Restore old value jika ada
        const old = '<?= old('academic_period_id') ?>';
        if (old) sel.value = old;
    });
});

// Trigger jika ada old value
const oldYear = '<?= old('academic_year_id') ?>';
if (oldYear) {
    document.getElementById('selYear').value = oldYear;
    document.getElementById('selYear').dispatchEvent(new Event('change'));
}
</script>
<?= $this->endSection() ?>