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

<div class="d-flex align-items-center justify-content-between mb-4 flex-wrap gap-2">
    <div>
        <h5 class="fw-bold mb-0"><i class="bi bi-book-half text-success me-2"></i>Program Tahfidz Al-Qur'an</h5>
        <small class="text-muted">Kelola program, guru pengampu, dan siswa per periode</small>
    </div>
    <div class="d-flex gap-2">
        <a href="/admin/tahfidz/dashboard" class="btn btn-sm btn-outline-success">
            <i class="bi bi-speedometer2 me-1"></i> Dashboard
        </a>
        <a href="/admin/tahfidz/create" class="btn btn-sm btn-success">
            <i class="bi bi-plus-lg me-1"></i> Buat Program
        </a>
    </div>
</div>

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

<!-- Filter Periode -->
<div class="card border-0 shadow-sm mb-3">
    <div class="card-body py-2 d-flex align-items-center gap-3">
        <span class="text-muted small fw-semibold">Filter Periode:</span>
        <form class="d-flex align-items-center gap-2" method="GET">
            <select name="period_id" class="form-select form-select-sm" style="max-width:280px;" onchange="this.form.submit()">
                <option value="">-- Semua Periode --</option>
                <?php foreach ($periods as $period): ?>
                <option value="<?= $period['id'] ?>" <?= $selectedPeriodId == $period['id'] ? 'selected' : '' ?>>
                    <?= esc($period['year_name']) ?> — Semester <?= $period['semester'] ?>
                </option>
                <?php endforeach; ?>
            </select>
        </form>
        <?php if ($activePeriod): ?>
        <span class="badge bg-success bg-opacity-15 text-success small">
            Aktif: <?= esc($activePeriod['year_name']) ?> — <?= $activePeriod['semester'] ?>
        </span>
        <?php endif; ?>
    </div>
</div>

<!-- Daftar Program -->
<?php if (empty($programs)): ?>
<div class="card border-0 shadow-sm">
    <div class="card-body text-center py-5 text-muted">
        <i class="bi bi-book fs-1 d-block mb-2 opacity-25"></i>
        Belum ada program tahfidz.<br>
        <a href="/admin/tahfidz/create" class="btn btn-success mt-3">
            <i class="bi bi-plus me-1"></i> Buat Program Pertama
        </a>
    </div>
</div>
<?php else: ?>
<div class="row g-3">
    <?php foreach ($programs as $p): ?>
    <?php
        $icon = match($p['type']) { 'pk' => '🏫', 'boarding' => '🌙', default => '☀️' };
        $badge = match($p['type']) { 'pk' => 'warning', 'boarding' => 'primary', default => 'success' };
        $typeLabel = match($p['type']) { 'pk' => 'Program Khusus', 'boarding' => 'Boarding', default => 'KBM Pagi' };
    ?>
    <div class="col-md-6 col-lg-4">
        <div class="card border-0 shadow-sm h-100 <?= !$p['is_active'] ? 'opacity-50' : '' ?>">
            <div class="card-header border-0 py-3 bg-<?= $badge ?> bg-opacity-10">
                <div class="d-flex align-items-center gap-2">
                    <span class="fs-3"><?= $icon ?></span>
                    <div class="flex-grow-1 min-width-0">
                        <div class="fw-bold text-truncate"><?= esc($p['name']) ?></div>
                        <div class="d-flex align-items-center gap-1 flex-wrap">
                            <span class="badge bg-<?= $badge ?> bg-opacity-20 text-<?= $badge ?>" style="font-size:0.7rem;"><?= $typeLabel ?></span>
                            <?php if (!$p['is_active']): ?>
                            <span class="badge bg-secondary" style="font-size:0.7rem;">Non-aktif</span>
                            <?php endif; ?>
                        </div>
                    </div>
                </div>
            </div>
            <div class="card-body">
                <div class="text-muted small mb-3">
                    <i class="bi bi-calendar3 me-1"></i>
                    <?= esc($p['year_name']) ?> — Semester <?= $p['semester'] ?>
                </div>
                <div class="row g-2 text-center mb-3">
                    <div class="col-6">
                        <div class="rounded p-2 bg-light">
                            <div class="fw-bold"><?= $p['teacher_count'] ?></div>
                            <div class="text-muted" style="font-size:0.72rem;"><i class="bi bi-person-badge me-1"></i>Guru</div>
                        </div>
                    </div>
                    <div class="col-6">
                        <div class="rounded p-2 bg-light">
                            <div class="fw-bold"><?= $p['student_count'] ?></div>
                            <div class="text-muted" style="font-size:0.72rem;"><i class="bi bi-people me-1"></i>Siswa</div>
                        </div>
                    </div>
                </div>
                <?php if ($p['description']): ?>
                <p class="text-muted small mb-3 fst-italic"><?= esc($p['description']) ?></p>
                <?php endif; ?>
            </div>
            <div class="card-footer border-0 bg-transparent pt-0 pb-3 px-3">
                <div class="d-flex flex-wrap gap-2">
                    <a href="/admin/tahfidz/programs/<?= $p['id'] ?>/students"
                       class="btn btn-sm btn-success flex-grow-1">
                        <i class="bi bi-people me-1"></i>Kelola Siswa
                    </a>
                    <a href="/admin/tahfidz/<?= $p['id'] ?>/edit"
                       class="btn btn-sm btn-outline-secondary">
                        <i class="bi bi-pencil"></i>
                    </a>
                    <button class="btn btn-sm btn-outline-danger btn-delete"
                            data-id="<?= $p['id'] ?>" data-name="<?= esc($p['name']) ?>">
                        <i class="bi bi-trash3"></i>
                    </button>
                </div>
            </div>
        </div>
    </div>
    <?php endforeach; ?>
</div>
<?php endif; ?>

<!-- Modal Konfirmasi Hapus -->
<div class="modal fade" id="deleteModal" tabindex="-1">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content border-0 shadow">
            <div class="modal-header border-0">
                <h5 class="modal-title"><i class="bi bi-trash3 text-danger me-2"></i>Hapus Program</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                Yakin ingin menghapus program <strong id="deleteName"></strong>?<br>
                <small class="text-danger">Semua data setoran dan hafalan pada program ini akan ikut terhapus.</small>
            </div>
            <div class="modal-footer border-0">
                <button class="btn btn-secondary" data-bs-dismiss="modal">Batal</button>
                <form id="deleteForm" method="POST">
                    <input type="hidden" name="<?= csrf_token() ?>" value="<?= csrf_hash() ?>">
                    <button class="btn btn-danger">Ya, Hapus</button>
                </form>
            </div>
        </div>
    </div>
</div>

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

<?= $this->section('scripts') ?>
<script>
document.querySelectorAll('.btn-delete').forEach(btn => {
    btn.addEventListener('click', function () {
        document.getElementById('deleteName').textContent = this.dataset.name;
        document.getElementById('deleteForm').action = `/admin/tahfidz/${this.dataset.id}/delete`;
        new bootstrap.Modal(document.getElementById('deleteModal')).show();
    });
});
</script>
<?= $this->endSection() ?>
