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

<!-- Header -->
<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-speedometer2 text-success me-2"></i>Dashboard Tahfidz Al-Qur'an
        </h5>
        <small class="text-muted">
            Monitoring semua program tahfidz &bull;
            <?= esc($activePeriod['year_name'] ?? '') ?> — Semester <?= $activePeriod['semester'] ?? '' ?>
        </small>
    </div>
    <div class="d-flex gap-2">
        <a href="/admin/tahfidz/export-recap" target="_blank" class="btn btn-sm btn-white text-dark border shadow-sm" style="background-color: #fff;">
            <i class="bi bi-printer me-1"></i>Cetak Laporan Eksekutif
        </a>
        <a href="/admin/tahfidz" class="btn btn-sm btn-outline-secondary">
            <i class="bi bi-diagram-3 me-1"></i>Kelola Program
        </a>
    </div>
</div>

<?php if (empty($dashData)): ?>
<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 aktif untuk periode ini.<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: ?>

<!-- Tabs Navigation -->
<div class="col-12 mb-3">
    <ul class="nav nav-pills gap-2" id="tahfidzDashboardTabs" role="tablist">
        <li class="nav-item" role="presentation">
            <button class="nav-link active fw-bold px-4 py-2 border-0" id="overview-tab" data-bs-toggle="tab" data-bs-target="#overview-pane" type="button" role="tab" aria-controls="overview-pane" aria-selected="true" style="border-radius: 8px;">
                <i class="bi bi-speedometer2 me-1.5"></i>Overview & Program
            </button>
        </li>
        <li class="nav-item" role="presentation">
            <button class="nav-link fw-bold px-4 py-2 border-0" id="executive-tab" data-bs-toggle="tab" data-bs-target="#executive-pane" type="button" role="tab" aria-controls="executive-pane" aria-selected="false" style="border-radius: 8px;">
                <i class="bi bi-shield-check me-1.5"></i>Laporan Eksekutif Pengawasan
            </button>
        </li>
    </ul>
</div>

<div class="tab-content" id="tahfidzDashboardTabsContent">
    <!-- TAB 1: OVERVIEW PANE -->
    <div class="tab-pane fade show active" id="overview-pane" role="tabpanel" aria-labelledby="overview-tab">
        <!-- Ringkasan Global -->
        <?php
        $totalProg    = count($dashData);
        $totalSiswa   = array_sum(array_column($dashData, 'total_students'));
        $totalSetor   = array_sum(array_column($dashData, 'setoran_minggu'));
        $totalHafalAll= array_sum(array_column($dashData, 'total_hafal_all'));
        ?>
        <div class="row g-3 mb-4">
            <div class="col-6 col-md-3">
                <div class="card border-0 shadow-sm text-center py-3">
                    <div class="fs-2 fw-bold text-success"><?= $totalProg ?></div>
                    <div class="text-muted small"><i class="bi bi-diagram-3 me-1"></i>Program Aktif</div>
                </div>
            </div>
            <div class="col-6 col-md-3">
                <div class="card border-0 shadow-sm text-center py-3">
                    <div class="fs-2 fw-bold text-primary"><?= $totalSiswa ?></div>
                    <div class="text-muted small"><i class="bi bi-people me-1"></i>Total Siswa</div>
                </div>
            </div>
            <div class="col-6 col-md-3">
                <div class="card border-0 shadow-sm text-center py-3">
                    <div class="fs-2 fw-bold text-info"><?= $totalSetor ?></div>
                    <div class="text-muted small"><i class="bi bi-journal-check me-1"></i>Setoran Minggu Ini</div>
                </div>
            </div>
            <div class="col-6 col-md-3">
                <div class="card border-0 shadow-sm text-center py-3">
                    <div class="fs-2 fw-bold text-warning"><?= $totalHafalAll ?></div>
                    <div class="text-muted small"><i class="bi bi-trophy me-1"></i>Total Hafalan</div>
                </div>
            </div>
        </div>

        <!-- Kartu per Program -->
        <div class="row g-3">
            <?php foreach ($dashData as $d): ?>
            <?php
                $p         = $d['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-xl-4">
                <div class="card border-0 shadow-sm h-100">
                    <!-- Program Header -->
                    <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>
                                <div class="fw-bold"><?= esc($p['name']) ?></div>
                                <div class="d-flex gap-1">
                                    <span class="badge bg-<?= $badge ?> bg-opacity-20 text-<?= $badge ?>" style="font-size:0.7rem;">
                                        <?= $typeLabel ?>
                                    </span>
                                    <span class="badge bg-secondary bg-opacity-15 text-dark" style="font-size:0.7rem;">
                                        <i class="bi bi-person-badge me-1"></i><?= $d['total_teachers'] ?> guru
                                    </span>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="card-body">
                        <!-- Statistik Grid -->
                        <div class="row g-2 text-center mb-3">
                            <div class="col-4">
                                <div class="rounded p-2 bg-light">
                                    <div class="fw-bold fs-5"><?= $d['total_students'] ?></div>
                                    <div class="text-muted" style="font-size:0.68rem;">Siswa</div>
                                </div>
                            </div>
                            <div class="col-4">
                                <div class="rounded p-2 bg-info bg-opacity-10">
                                    <div class="fw-bold fs-5 text-info"><?= $d['setoran_minggu'] ?></div>
                                    <div class="text-muted" style="font-size:0.68rem;">Setor Minggu</div>
                                </div>
                            </div>
                            <div class="col-4">
                                <div class="rounded p-2 bg-warning bg-opacity-10">
                                    <div class="fw-bold fs-5 text-warning"><?= $d['total_hafal_all'] ?></div>
                                    <div class="text-muted" style="font-size:0.68rem;">Total Hafal</div>
                                </div>
                            </div>
                        </div>

                        <!-- Top 5 Hafalan -->
                        <?php if (!empty($d['top_hafalan'])): ?>
                        <div class="mb-2">
                            <div class="text-muted small fw-semibold mb-2">
                                <i class="bi bi-trophy text-warning me-1"></i>Top Hafalan
                            </div>
                            <?php foreach ($d['top_hafalan'] as $i => $h): ?>
                            <?php
                                $pct   = $h['total_hafal'] ? round(($h['total_hafal'] / 114) * 100) : 0;
                                $medal = match($i) { 0 => '🥇', 1 => '🥈', 2 => '🥉', default => '' };
                            ?>
                            <div class="d-flex align-items-center gap-2 mb-1">
                                <span style="font-size:0.85rem; width:20px;"><?= $medal ?: ($i + 1) . '.' ?></span>
                                <div class="flex-grow-1 min-width-0">
                                    <div class="d-flex justify-content-between align-items-center">
                                        <span class="small text-truncate fw-semibold" style="max-width:140px;">
                                            <?= esc($h['full_name']) ?>
                                        </span>
                                        <span class="small text-success fw-bold"><?= $h['total_hafal'] ?>/114</span>
                                    </div>
                                    <div class="progress" style="height:5px;">
                                        <div class="progress-bar bg-success" style="width:<?= $pct ?>%"></div>
                                    </div>
                                </div>
                            </div>
                            <?php endforeach; ?>
                        </div>
                        <?php else: ?>
                        <div class="text-center text-muted py-2 small">
                            <i class="bi bi-trophy opacity-25 d-block fs-4"></i>
                            Belum ada data hafalan
                        </div>
                        <?php endif; ?>
                    </div>

                    <div class="card-footer border-0 bg-transparent pb-3 pt-0 px-3">
                        <div class="d-flex gap-2">
                            <a href="/admin/tahfidz/programs/<?= $p['id'] ?>/students"
                               class="btn btn-sm btn-outline-<?= $badge ?> 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>
                        </div>
                    </div>
                </div>
            </div>
            <?php endforeach; ?>
        </div>
    </div>

    <!-- TAB 2: LAPORAN EKSEKUTIF PANE -->
    <div class="tab-pane fade" id="executive-pane" role="tabpanel" aria-labelledby="executive-tab">
        <div class="row g-4">
            <!-- Peringkat Keaktifan Kelas -->
            <div class="col-md-4">
                <div class="card border-0 shadow-sm h-100" style="border-radius: 12px;">
                    <div class="card-header bg-white border-0 pt-4 px-4 pb-0">
                        <h6 class="fw-bold text-dark mb-1"><i class="bi bi-bar-chart-line-fill text-success me-2"></i>Keaktifan Setoran Kelas</h6>
                        <p class="text-muted small mb-0">Rangking kelas berdasarkan akumulasi frekuensi setoran.</p>
                    </div>
                    <div class="card-body px-4 pb-4">
                        <?php if (empty($kelasKeaktifan)): ?>
                            <div class="text-center py-5 text-muted small">
                                <i class="bi bi-bar-chart fs-2 opacity-25 d-block mb-1"></i>Belum ada riwayat setoran kelas.
                            </div>
                        <?php else: ?>
                            <div class="list-group list-group-flush mt-2">
                                <?php foreach ($kelasKeaktifan as $idx => $kk): ?>
                                    <?php $medal = match($idx) { 0 => '🥇', 1 => '🥈', 2 => '🥉', default => '' }; ?>
                                    <div class="list-group-item d-flex justify-content-between align-items-center px-0 py-2.5">
                                        <div class="d-flex align-items-center gap-2">
                                            <span style="width: 24px;" class="small"><?= $medal ?: ($idx + 1) . '.' ?></span>
                                            <strong class="text-dark"><?= esc($kk['classroom_name']) ?></strong>
                                        </div>
                                        <span class="badge bg-success bg-opacity-10 text-success rounded-pill px-3 py-1.5 small fw-semibold">
                                            <?= $kk['total_setoran'] ?> Setoran
                                        </span>
                                    </div>
                                <?php endforeach; ?>
                            </div>
                        <?php endif; ?>
                    </div>
                </div>
            </div>

            <!-- Daftar Siswa Butuh Perhatian Khusus -->
            <div class="col-md-8">
                <div class="card border-0 shadow-sm h-100" style="border-radius: 12px;">
                    <div class="card-header bg-white border-0 pt-4 px-4 pb-0">
                        <h6 class="fw-bold text-danger mb-1"><i class="bi bi-exclamation-triangle-fill text-danger me-2"></i>Deteksi Dini: Siswa Butuh Perhatian Khusus</h6>
                        <p class="text-muted small mb-0">Daftar siswa aktif yang **tidak menyetor hafalan sama sekali dalam 14 hari terakhir**.</p>
                    </div>
                    <div class="card-body p-0 pt-3">
                        <?php if (empty($criticalStudents)): ?>
                            <div class="text-center py-5 text-muted small">
                                <i class="bi bi-check-circle fs-2 text-success opacity-50 d-block mb-1"></i>Luar biasa! Semua siswa aktif menyetor hafalan dalam 14 hari terakhir.
                            </div>
                        <?php else: ?>
                            <div class="table-responsive">
                                <table class="table table-hover align-middle mb-0 text-dark" style="font-size: 0.88rem;">
                                    <thead class="table-light">
                                        <tr>
                                            <th class="ps-4">Siswa (Kelas)</th>
                                            <th class="text-center">Hari Tanpa Setoran</th>
                                            <th>Surah Terakhir</th>
                                            <th class="pe-4" style="width: 45%;">Catatan Kendala Guru Pembimbing</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php foreach ($criticalStudents as $cs): ?>
                                            <?php 
                                            $days = 0;
                                            if ($cs['last_setoran_date']) {
                                                $lastDate = new \DateTime($cs['last_setoran_date']);
                                                $today = new \DateTime();
                                                $days = $today->diff($lastDate)->days;
                                            } else {
                                                $days = '∞'; // Belum pernah setor sama sekali
                                            }
                                            ?>
                                            <tr>
                                                <td class="ps-4">
                                                    <strong class="text-dark d-block"><?= esc($cs['full_name']) ?></strong>
                                                    <small class="text-muted">Kelas: <?= esc($cs['classroom_name']) ?></small>
                                                </td>
                                                <td class="text-center">
                                                    <?php if ($days === '∞'): ?>
                                                        <span class="badge bg-danger rounded-pill px-3 py-1.5 fw-bold">Belum Pernah Setor</span>
                                                    <?php else: ?>
                                                        <span class="badge bg-warning text-dark rounded-pill px-3 py-1.5 fw-bold"><?= $days ?> Hari</span>
                                                    <?php endif; ?>
                                                </td>
                                                <td>
                                                    <span class="text-dark fw-semibold"><?= esc($cs['surah_name'] ?: 'Belum Ada') ?></span>
                                                    <small class="text-muted d-block" style="font-size:0.75rem;"><?= $cs['last_setoran_date'] ? date('d-m-Y', strtotime($cs['last_setoran_date'])) : '-' ?></small>
                                                </td>
                                                <td class="pe-4 py-3">
                                                    <?php if ($cs['catatan'] && trim($cs['catatan']) !== ''): ?>
                                                        <div class="p-2 bg-light border rounded small" style="line-height: 1.4; font-size: 0.8rem;">
                                                            <?= esc($cs['catatan']) ?>
                                                        </div>
                                                    <?php else: ?>
                                                        <span class="text-muted small"><i class="bi bi-clock me-1"></i>Belum ada catatan kendala dari guru</span>
                                                    <?php endif; ?>
                                                </td>
                                            </tr>
                                        <?php endforeach; ?>
                                    </tbody>
                                </table>
                            </div>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<?php endif; ?>

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