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

<?php if (isset($selectedProgram)): ?>
    <!-- MODE DETAIL PROGRAM: Tampilkan Navigasi Fitur Horizontal -->
    <?= $this->include('guru/tahfidz/_subnav') ?>

    <!-- Baris Ringkasan Pencapaian (Metrik Program) -->
    <div class="row g-3 mb-4">
        <div class="col-md-4">
            <div class="card border-0 shadow-sm">
                <div class="card-body d-flex align-items-center gap-3">
                    <div class="rounded bg-primary bg-opacity-10 text-primary d-flex align-items-center justify-content-center" style="width:48px; height:48px; font-size: 1.4rem;">
                        👥
                    </div>
                    <div>
                        <h6 class="text-muted small mb-0">Total Siswa</h6>
                        <h4 class="fw-bold mb-0"><?= count($students) ?></h4>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="card border-0 shadow-sm">
                <div class="card-body d-flex align-items-center gap-3">
                    <div class="rounded bg-success bg-opacity-10 text-success d-flex align-items-center justify-content-center" style="width:48px; height:48px; font-size: 1.4rem;">
                        📅
                    </div>
                    <div>
                        <h6 class="text-muted small mb-0">Setor Hari Ini</h6>
                        <h4 class="fw-bold mb-0"><?= count($todaySetor) ?></h4>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="card border-0 shadow-sm">
                <div class="card-body d-flex align-items-center gap-3">
                    <div class="rounded <?= count($blumSetor) > 0 ? 'bg-danger bg-opacity-10 text-danger' : 'bg-light text-secondary' ?> d-flex align-items-center justify-content-center" style="width:48px; height:48px; font-size: 1.4rem;">
                        ⚠️
                    </div>
                    <div>
                        <h6 class="text-muted small mb-0">Belum Setor >7 Hari</h6>
                        <h4 class="fw-bold mb-0 text-<?= count($blumSetor) > 0 ? 'danger' : 'dark' ?>"><?= count($blumSetor) ?></h4>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Monitoring Progres Siswa -->
    <div class="card border-0 shadow-sm">
        <div class="card-header bg-white border-0 py-3 d-flex align-items-center justify-content-between">
            <h6 class="fw-bold mb-0 text-success">
                <i class="bi bi-person-lines-fill me-2"></i>Monitoring Hafalan Siswa
            </h6>
        </div>
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-hover align-middle mb-0">
                    <thead class="bg-light" style="font-size: 0.8rem; text-transform: uppercase;">
                        <tr>
                            <th class="px-3" style="width: 50px;">No</th>
                            <th>Nama Siswa / NIS</th>
                            <th>Kelas</th>
                            <th class="text-center">Total Surah</th>
                            <th class="text-center">Persentase</th>
                            <th class="text-center">Aksi Cepat</th>
                        </tr>
                    </thead>
                    <tbody style="font-size: 0.85rem;">
                        <?php if (empty($students)): ?>
                            <tr>
                                <td colspan="6" class="text-center text-muted py-4">Belum ada siswa terdaftar di program ini.</td>
                            </tr>
                        <?php else: ?>
                            <?php 
                                $no = 1; 
                                foreach ($students as $s): 
                                    // Ambil progress dari rekapHafalan
                                    $hafalCount = 0;
                                    foreach ($rekapHafalan as $rh) {
                                        if ($rh['id'] == $s['id']) {
                                            $hafalCount = $rh['total_hafal'];
                                            break;
                                        }
                                    }
                                    $pct = round(($hafalCount / 114) * 100, 1);
                                    $barColor = $pct > 60 ? 'bg-success' : ($pct > 20 ? 'bg-info' : 'bg-warning');
                            ?>
                            <tr>
                                <td class="text-center text-muted px-3"><?= $no++ ?></td>
                                <td>
                                    <div class="fw-bold"><?= esc($s['full_name']) ?></div>
                                    <div class="text-muted small">NIS: <?= esc($s['nis']) ?></div>
                                </td>
                                <td><?= esc($s['classroom_name'] ?? '-') ?></td>
                                <td class="text-center fw-semibold"><?= $hafalCount ?> / 114 Surah</td>
                                <td style="min-width: 150px;">
                                    <div class="d-flex align-items-center gap-2">
                                        <div class="progress flex-grow-1" style="height: 6px;">
                                            <div class="progress-bar <?= $barColor ?>" style="width: <?= $pct ?>%"></div>
                                        </div>
                                        <span class="fw-semibold text-muted" style="font-size: 0.75rem;"><?= $pct ?>%</span>
                                    </div>
                                </td>
                                <td class="text-center">
                                    <div class="d-flex align-items-center justify-content-center gap-1">
                                        <a href="/guru/tahfidz/setoran?program_id=<?= $selectedProgram['id'] ?>&student_id=<?= $s['id'] ?>" 
                                           class="btn btn-xs btn-success py-1 px-2" title="Input Setoran">
                                            <i class="bi bi-pencil-square"></i>
                                        </a>
                                        <a href="/guru/tahfidz/hafalan?program_id=<?= $selectedProgram['id'] ?>&student_id=<?= $s['id'] ?>" 
                                           class="btn btn-xs btn-outline-primary py-1 px-2" title="Ceklis Hafalan">
                                            <i class="bi bi-check2-square"></i>
                                        </a>
                                        <a href="/guru/tahfidz/progress?program_id=<?= $selectedProgram['id'] ?>&student_id=<?= $s['id'] ?>" 
                                           class="btn btn-xs btn-outline-secondary py-1 px-2" title="Progress Juz">
                                            <i class="bi bi-bar-chart-steps"></i>
                                        </a>
                                    </div>
                                </td>
                            </tr>
                            <?php endforeach; ?>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>

<?php else: ?>
    <!-- MODE OVERVIEW UTAMA: Tampilkan Banner & Kartu-Kartu Program -->
    <div class="row g-3 mb-4">
        <div class="col-12">
            <!-- Header Banner -->
            <div class="card border-0 shadow-sm" style="background: linear-gradient(135deg, #1a472a 0%, #2d6a4f 60%, #40916c 100%); min-height: 130px;">
                <div class="card-body d-flex align-items-center gap-4 p-4">
                    <div class="rounded-circle d-flex align-items-center justify-content-center flex-shrink-0"
                         style="width:70px; height:70px; background:rgba(255,255,255,0.15); font-size:2rem;">
                        📖
                    </div>
                    <div class="text-white">
                        <h4 class="mb-1 fw-bold">Tahfidz Al-Qur'an</h4>
                        <p class="mb-0 opacity-75">
                            <?= esc($teacher['name']) ?> &bull;
                            <?= $activePeriod ? esc($activePeriod['year_name'] . ' — Semester ' . $activePeriod['semester']) : 'Belum ada periode aktif' ?>
                        </p>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <?php if (empty($programStats)): ?>
        <div class="alert alert-warning d-flex align-items-center gap-2 shadow-sm">
            <i class="bi bi-info-circle fs-5"></i>
            <div>Anda belum ditugaskan sebagai pengampu program tahfidz manapun pada periode ini. Hubungi administrator.</div>
        </div>
    <?php else: ?>
        <!-- Program Cards -->
        <div class="row g-3">
            <?php foreach ($programStats as $ps): ?>
            <?php
                $p    = $ps['program'];
                $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">
                    <div class="card-header bg-<?= $badge ?> bg-opacity-10 border-0 py-3">
                        <div class="d-flex align-items-center justify-content-between">
                            <div class="d-flex align-items-center gap-2">
                                <span class="fs-4"><?= $icon ?></span>
                                <div>
                                    <div class="fw-bold"><?= $typeLabel ?></div>
                                    <span class="text-muted small" style="font-size:0.75rem;"><?= esc($p['name']) ?></span>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="card-body">
                        <div class="row g-2 text-center mb-3">
                            <div class="col-4">
                                <div class="p-2 rounded bg-light">
                                    <div class="fs-4 fw-bold text-dark"><?= $ps['total_students'] ?></div>
                                    <div class="text-muted" style="font-size:0.72rem;">Siswa</div>
                                </div>
                            </div>
                            <div class="col-4">
                                <div class="p-2 rounded bg-success bg-opacity-10">
                                    <div class="fs-4 fw-bold text-success"><?= $ps['today_setoran'] ?></div>
                                    <div class="text-muted" style="font-size:0.72rem;">Setor Hari Ini</div>
                                </div>
                            </div>
                            <div class="col-4">
                                <div class="p-2 rounded <?= $ps['blum_setor_7d'] > 0 ? 'bg-danger bg-opacity-10' : 'bg-light' ?>">
                                    <div class="fs-4 fw-bold <?= $ps['blum_setor_7d'] > 0 ? 'text-danger' : 'text-secondary' ?>"><?= $ps['blum_setor_7d'] ?></div>
                                    <div class="text-muted" style="font-size:0.72rem;">Blm Setor 7hr</div>
                                </div>
                            </div>
                        </div>
                        <div class="d-flex flex-column gap-2">
                            <a href="/guru/tahfidz?program_id=<?= $p['id'] ?>" class="btn btn-sm btn-success w-100">
                                <i class="bi bi-folder-symlink me-1"></i> Buka Program
                            </a>
                        </div>
                    </div>
                </div>
            </div>
            <?php endforeach; ?>
        </div>
    <?php endif; ?>
<?php endif; ?>

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