<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <title>Laporan Eksekutif Capaian Tahfidz</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            color: #333;
            font-size: 11pt;
            line-height: 1.4;
            margin: 20px;
        }
        .header {
            text-align: center;
            border-bottom: 2px double #333;
            padding-bottom: 12px;
            margin-bottom: 20px;
        }
        .header h2 {
            margin: 0;
            font-size: 16pt;
            text-transform: uppercase;
        }
        .header p {
            margin: 4px 0 0 0;
            font-size: 10pt;
            color: #666;
        }
        .meta-table {
            width: 100%;
            margin-bottom: 20px;
            font-size: 10pt;
        }
        .meta-table td {
            padding: 3px 0;
        }
        .section-title {
            font-size: 12pt;
            font-weight: bold;
            margin-top: 25px;
            margin-bottom: 8px;
            border-bottom: 1px solid #ddd;
            padding-bottom: 4px;
            color: #000;
        }
        table.data-table {
            width: 100%;
            border-collapse: collapse;
            font-size: 9.5pt;
            margin-bottom: 20px;
        }
        table.data-table th, table.data-table td {
            border: 1px solid #666;
            padding: 6px 8px;
            text-align: left;
        }
        table.data-table th {
            background-color: #f2f2f2;
            font-weight: bold;
        }
        .text-center {
            text-align: center;
        }
        .badge {
            display: inline-block;
            padding: 2px 6px;
            font-size: 8pt;
            font-weight: bold;
            border-radius: 4px;
            border: 1px solid #ccc;
        }
        .badge-warning {
            background-color: #fff3cd;
            color: #856404;
            border-color: #ffeeba;
        }
        .badge-danger {
            background-color: #f8d7da;
            color: #721c24;
            border-color: #f5c6cb;
        }
        @media print {
            body { margin: 0; }
            .no-print { display: none; }
            .page-break { page-break-before: always; }
        }
    </style>
</head>
<body>

    <!-- Floating Action Button for print -->
    <div class="no-print" style="margin-bottom: 20px; text-align: right;">
        <button onclick="window.print();" style="padding: 8px 16px; font-weight: bold; background: #28a745; color: #fff; border: none; border-radius: 4px; cursor: pointer;">
            🖨️ Cetak Halaman Ini
        </button>
    </div>

    <!-- Kop Surat -->
    <div class="header">
        <h2>Laporan Eksekutif Capaian Tahfidz Al-Qur'an</h2>
        <p>Sistem Informasi Manajemen Akademik &amp; KBM Madrasah</p>
    </div>

    <table class="meta-table">
        <tr>
            <td style="width: 15%;"><strong>Tahun Pelajaran</strong></td>
            <td style="width: 35%;">: <?= esc($activePeriod['year_name'] ?? '-') ?></td>
            <td style="width: 15%;"><strong>Tanggal Cetak</strong></td>
            <td style="width: 35%;">: <?= date('d-m-Y H:i') ?></td>
        </tr>
        <tr>
            <td><strong>Semester</strong></td>
            <td>: Sem. <?= esc($activePeriod['semester'] ?? '-') ?></td>
            <td><strong>Dicetak Oleh</strong></td>
            <td>: <?= esc(session('user_name')) ?> (<?= esc(ucfirst(session('user_role'))) ?>)</td>
        </tr>
    </table>

    <!-- SECTION 1: RINGKASAN PROGRAM -->
    <div class="section-title">I. Ringkasan Kinerja Program Tahfidz</div>
    <table class="data-table">
        <thead>
            <tr>
                <th style="width: 5%;">No</th>
                <th>Nama Program Tahfidz</th>
                <th style="width: 15%;">Tipe Program</th>
                <th style="width: 12%;" class="text-center">Total Murid</th>
                <th style="width: 12%;" class="text-center">Total Pengajar</th>
                <th style="width: 15%;" class="text-center">Setoran Minggu Ini</th>
                <th style="width: 15%;" class="text-center">Total Hafalan (Sura)</th>
            </tr>
        </thead>
        <tbody>
            <?php $no = 1; foreach ($dashData as $d): ?>
                <?php 
                $typeLabel = match($d['program']['type']) { 'pk' => 'Program Khusus', 'boarding' => 'Boarding', default => 'KBM Pagi' };
                ?>
                <tr>
                    <td class="text-center"><?= $no++ ?></td>
                    <td><strong><?= esc($d['program']['name']) ?></strong></td>
                    <td><?= $typeLabel ?></td>
                    <td class="text-center"><?= $d['total_students'] ?> Siswa</td>
                    <td class="text-center"><?= $d['total_teachers'] ?> Guru</td>
                    <td class="text-center"><?= $d['setoran_minggu'] ?></td>
                    <td class="text-center"><?= $d['total_hafal_all'] ?> kali</td>
                </tr>
            <?php endforeach; ?>
        </tbody>
    </table>

    <!-- SECTION 2: KEAKTIFAN KELAS -->
    <div class="section-title">II. Rangking Keaktifan Setoran Kelas</div>
    <table class="data-table" style="max-width: 500px;">
        <thead>
            <tr>
                <th style="width: 15%;" class="text-center">Peringkat</th>
                <th>Nama Kelas</th>
                <th style="width: 35%;" class="text-center">Akumulasi Frekuensi Setoran</th>
            </tr>
        </thead>
        <tbody>
            <?php if (empty($kelasKeaktifan)): ?>
                <tr>
                    <td colspan="3" class="text-center">Belum ada riwayat setoran masuk.</td>
                </tr>
            <?php else: ?>
                <?php foreach ($kelasKeaktifan as $idx => $kk): ?>
                    <tr>
                        <td class="text-center"><?= $idx + 1 ?></td>
                        <td><strong><?= esc($kk['classroom_name']) ?></strong></td>
                        <td class="text-center"><?= $kk['total_setoran'] ?> Kali Setoran</td>
                    </tr>
                <?php endforeach; ?>
            <?php endif; ?>
        </tbody>
    </table>

    <div class="page-break"></div>

    <!-- SECTION 3: SISWA BUTUH PERHATIAN KHUSUS -->
    <div class="section-title" style="margin-top: 0;">III. Deteksi Dini: Siswa Butuh Perhatian Khusus (Tidak Setor &ge; 14 Hari)</div>
    <p style="font-size: 8.5pt; color: #555; margin-bottom: 12px; font-style: italic;">
        Daftar berikut menyaring siswa aktif yang belum melakukan setoran hafalan sama sekali dalam 14 hari terakhir untuk bahan evaluasi bersama pengajar tahfidz.
    </p>
    <table class="data-table">
        <thead>
            <tr>
                <th style="width: 5%;">No</th>
                <th style="width: 25%;">Nama Siswa (Kelas)</th>
                <th style="width: 15%;" class="text-center">Lama Vakum</th>
                <th style="width: 20%;">Hafalan Terakhir (Tgl)</th>
                <th>Catatan Kendala Belajar &amp; Keterangan Guru Pembimbing</th>
            </tr>
        </thead>
        <tbody>
            <?php if (empty($criticalStudents)): ?>
                <tr>
                    <td colspan="5" class="text-center py-4" style="color: green; font-weight: bold;">
                        Luar biasa! Seluruh siswa aktif terpantau rajin menyetor hafalan dalam 14 hari terakhir.
                    </td>
                </tr>
            <?php else: ?>
                <?php $no = 1; 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 . ' Hari';
                    } else {
                        $days = 'Belum Pernah';
                    }
                    ?>
                    <tr>
                        <td class="text-center"><?= $no++ ?></td>
                        <td>
                            <strong><?= esc($cs['full_name']) ?></strong>
                            <div style="font-size: 8pt; color: #555;">Kelas: <?= esc($cs['classroom_name']) ?></div>
                        </td>
                        <td class="text-center">
                            <span class="badge <?= $days === 'Belum Pernah' ? 'badge-danger' : 'badge-warning' ?>">
                                <?= $days ?>
                            </span>
                        </td>
                        <td>
                            <strong><?= esc($cs['surah_name'] ?: '-') ?></strong>
                            <div style="font-size: 8pt; color: #555;"><?= $cs['last_setoran_date'] ? date('d-m-Y', strtotime($cs['last_setoran_date'])) : '-' ?></div>
                        </td>
                        <td style="font-size: 8.5pt;">
                            <?= esc($cs['catatan'] ?: 'Belum ada catatan kendala dilaporkan oleh guru pengampu.') ?>
                        </td>
                    </tr>
                <?php endforeach; ?>
            <?php endif; ?>
        </tbody>
    </table>

    <!-- Tanda Tangan Lap -->
    <div style="margin-top: 50px; text-align: right; width: 100%;">
        <div style="display: inline-block; text-align: center; width: 250px;">
            <p>Mengetahui,</p>
            <p style="font-weight: bold; margin-bottom: 60px;">Kepala Madrasah</p>
            <p style="text-decoration: underline; font-weight: bold; margin-bottom: 0;"><?= esc(session('user_name')) ?></p>
            <p style="font-size: 9pt; color: #666; margin-top: 2px;">NIP. __________________</p>
        </div>
    </div>

</body>
</html>
