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

<div class="container-fluid">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h1 class="h3 mb-0 text-gray-800 fw-bold"><?= $title ?></h1>
            <p class="text-muted small mb-0">Kelola dan input jadwal pelajaran sekolah secara manual per-kelas.</p>
        </div>
        <a href="/manual-schedule/create" class="btn btn-primary fw-bold shadow-sm">
            <i class="bi bi-plus-circle me-1"></i> Buat Jadwal Manual Baru
        </a>
    </div>

    <!-- Alerts -->
    <?php if (session()->getFlashdata('success')) : ?>
        <div class="alert alert-success alert-dismissible fade show border-0 shadow-sm mb-4" role="alert">
            <div class="d-flex align-items-center gap-2">
                <i class="bi bi-check-circle-fill fs-5"></i>
                <div><?= session()->getFlashdata('success') ?></div>
            </div>
            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
        </div>
    <?php endif; ?>

    <?php if (session()->getFlashdata('error')) : ?>
        <div class="alert alert-danger alert-dismissible fade show border-0 shadow-sm mb-4" role="alert">
            <div class="d-flex align-items-center gap-2">
                <i class="bi bi-exclamation-triangle-fill fs-5"></i>
                <div><?= session()->getFlashdata('error') ?></div>
            </div>
            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
        </div>
    <?php endif; ?>

    <!-- Schedule List Table -->
    <div class="card shadow-sm border-0 rounded-3">
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-hover align-middle mb-0" style="min-width: 800px;">
                    <thead class="table-light text-uppercase font-monospace" style="font-size: 0.75rem; letter-spacing: 0.5px;">
                        <tr>
                            <th class="ps-4 py-3 text-muted">Nama Jadwal</th>
                            <th class="py-3 text-muted">Periode Akademik</th>
                            <th class="py-3 text-muted">Hari Aktif</th>
                            <th class="py-3 text-muted text-center">Status</th>
                            <th class="py-3 text-muted text-center">Total Entri</th>
                            <th class="pe-4 py-3 text-muted text-end">Aksi</th>
                        </tr>
                    </thead>
                    <tbody class="border-top-0">
                        <?php if (empty($schedules)) : ?>
                            <tr>
                                <td colspan="6" class="text-center py-5 text-muted">
                                    <div class="py-3">
                                        <i class="bi bi-calendar-x fs-1 opacity-25 d-block mb-3"></i>
                                        <span class="fw-semibold">Belum ada template jadwal manual.</span>
                                        <p class="small text-muted mb-0">Klik tombol "Buat Jadwal Manual Baru" untuk memulai.</p>
                                    </div>
                                </td>
                            </tr>
                        <?php else : ?>
                            <?php foreach ($schedules as $s) : ?>
                                <tr>
                                    <td class="ps-4 py-3">
                                        <div class="fw-bold text-dark fs-6"><?= esc($s['name']) ?></div>
                                        <span class="text-muted small">ID: #<?= $s['id'] ?></span>
                                    </td>
                                    <td class="py-3">
                                        <div class="fw-semibold text-secondary small">T.A. <?= esc($s['year_name']) ?></div>
                                        <span class="badge bg-light text-dark border small mt-1">Semester <?= esc($s['period_name']) ?></span>
                                    </td>
                                    <td class="py-3">
                                        <?php
                                        $daysMap = [1 => 'Senin', 2 => 'Selasa', 3 => 'Rabu', 4 => 'Kamis', 5 => 'Jumat', 6 => 'Sabtu'];
                                        $activeDays = array_map(function($d) use ($daysMap) {
                                            return $daysMap[(int)$d] ?? '';
                                        }, explode(',', $s['active_days']));
                                        echo implode(', ', $activeDays);
                                        ?>
                                    </td>
                                    <td class="py-3 text-center">
                                        <?php if ($s['status'] === 'published') : ?>
                                            <span class="badge bg-success-subtle text-success px-3 py-2 rounded-pill fw-bold border border-success-subtle" style="font-size: 0.75rem;">
                                                <i class="bi bi-check2-circle me-1"></i> Terbit (Aktif)
                                            </span>
                                        <?php else : ?>
                                            <span class="badge bg-secondary-subtle text-secondary px-3 py-2 rounded-pill fw-bold border border-secondary-subtle" style="font-size: 0.75rem;">
                                                Draf
                                            </span>
                                        <?php endif; ?>
                                    </td>
                                    <td class="py-3 text-center fw-bold text-secondary small">
                                        <?= $s['total_entries'] ?> JP
                                    </td>
                                    <td class="pe-4 py-3 text-end">
                                        <div class="d-flex justify-content-end gap-2">
                                            <a href="/manual-schedule/by-class/<?= $s['id'] ?>" class="btn btn-sm btn-outline-info d-flex align-items-center gap-1 px-3 py-2 fw-semibold">
                                                <i class="bi bi-printer"></i> Cetak
                                            </a>
                                            <a href="/manual-schedule/edit/<?= $s['id'] ?>" class="btn btn-sm btn-primary d-flex align-items-center gap-1 shadow-sm px-3 py-2 fw-semibold">
                                                <i class="bi bi-pencil-square"></i> Input Jadwal
                                            </a>
                                            <?php if ($s['status'] !== 'published') : ?>
                                                <a href="/manual-schedule/publish/<?= $s['id'] ?>" class="btn btn-sm btn-outline-success d-flex align-items-center gap-1 px-3 py-2 fw-semibold" onclick="return confirm('Aktifkan jadwal manual ini? Jadwal manual lainnya pada periode ini akan dinonaktifkan.');">
                                                    <i class="bi bi-check-lg"></i> Aktifkan
                                                </a>
                                            <?php endif; ?>
                                            <a href="/manual-schedule/delete/<?= $s['id'] ?>" class="btn btn-sm btn-outline-danger d-flex align-items-center gap-1 px-2 py-2" onclick="return confirm('Hapus template jadwal manual ini beserta seluruh entri di dalamnya? Tindakan ini tidak dapat dibatalkan.');">
                                                <i class="bi bi-trash"></i>
                                            </a>
                                        </div>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<style>
.table th {
    background-color: #f8f9fa;
    border-bottom: 2px solid #eaedf1;
}
.table td {
    border-bottom: 1px solid #eaedf1;
}
.rounded-3 {
    border-radius: 0.5rem !important;
}
</style>

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