<!DOCTYPE html>
<html lang="id">
<head>
    <?php helper('logo'); ?>
    <link rel="icon" type="image/png" href="<?= getLogo('sekolah') ?>">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Preview Laporan Kalender Pendidikan <?= esc($year['name']) ?></title>
    <style>
        /* ===== TOOLBAR (tidak ikut cetak) ===== */
        #print-toolbar {
            position: fixed; top: 0; left: 0; right: 0; z-index: 999;
            background: #1e2a3a; color: #fff;
            padding: 10px 20px;
            display: flex; align-items: center; gap: 12px;
            font-family: sans-serif; font-size: 14px;
            box-shadow: 0 2px 8px rgba(0,0,0,.3);
        }
        #print-toolbar .toolbar-title { flex: 1; font-weight: 600; }
        #print-toolbar a  { color: #adc8ff; text-decoration: none; font-size: 13px; }
        #print-toolbar a:hover { text-decoration: underline; }
        .btn-print {
            background: #198754; color: #fff; border: none;
            padding: 7px 18px; border-radius: 6px; cursor: pointer;
            font-size: 14px; font-weight: 600;
        }
        .btn-print:hover { background: #157347; }
        .btn-pdf {
            background: #dc3545; color: #fff; border: none;
            padding: 7px 18px; border-radius: 6px; cursor: pointer;
            font-size: 14px; font-weight: 600; text-decoration: none;
        }
        .btn-pdf:hover { background: #bb2d3b; color: #fff; }

        /* ===== KONTEN ===== */
        body {
            font-family: 'Segoe UI', Arial, sans-serif;
            font-size: 11px; color: #222;
            background: #e8ecf0;
            margin: 0; padding: 72px 0 20px;
        }
        .page-wrap {
            width: 210mm; min-height: 297mm;
            margin: 20px auto; background: #fff;
            padding: 15mm;
            box-shadow: 0 2px 12px rgba(0,0,0,.15);
        }
        .kop-table { width: 100%; border-bottom: 2px solid #000; padding-bottom: 6px; margin-bottom: 10px; }
        .kop-table td { vertical-align: middle; }
        .kop-title { font-size: 14px; font-weight: bold; text-transform: uppercase; text-align: center; }
        .kop-sub { font-size: 10px; text-align: center; }
        .doc-title { text-align: center; font-size: 14px; font-weight: bold; text-transform: uppercase; margin: 8px 0 2px; }
        .doc-subtitle { text-align: center; font-size: 12px; margin-bottom: 10px; }

        .month-title {
            background: #343a40; color: #fff; font-weight: bold;
            padding: 4px 8px; font-size: 11px; margin-top: 8px;
        }
        table.events { width: 100%; border-collapse: collapse; margin-bottom: 6px; }
        table.events th, table.events td { border: 1px solid #888; padding: 3px 5px; font-size: 10px; }
        table.events th { background: #e9ecef; text-align: center; }
        .empty-row td { text-align: center; color: #888; font-style: italic; }

        table.rekap { width: 100%; border-collapse: collapse; margin-top: 4px; }
        table.rekap th, table.rekap td { border: 1px solid #888; padding: 3px 5px; font-size: 10px; text-align: center; }
        table.rekap th { background: #e9ecef; }

        table.legend { width: 100%; border-collapse: collapse; margin-top: 6px; }
        table.legend td { border: 1px solid #888; padding: 2px 6px; font-size: 10px; vertical-align: middle; }
        .legend-color { width: 14px; height: 14px; display: inline-block; border: 1px solid #999; vertical-align: middle; margin-right: 3px; }

        .ttd-table { width: 100%; margin-top: 25px; }
        .ttd-table td { font-size: 11px; text-align: center; vertical-align: top; }

        /* ===== PRINT ===== */
        @media print {
            #print-toolbar { display: none !important; }
            body { background: #fff; padding: 0; }
            .page-wrap { box-shadow: none; margin: 0; padding: 12mm; width: auto; }
        }
    </style>
</head>
<body>

<!-- TOOLBAR -->
<div id="print-toolbar">
    <div class="toolbar-title">
        <i>👁</i> Preview — Laporan Kalender Pendidikan <?= esc($year['name']) ?>
    </div>
    <?php
    $backUrl = in_array(session()->get('user_role'), ['superadmin', 'admin'])
        ? "/academic-calendar?academic_year_id=" . $year['id']
        : "/guru/dashboard";
    ?>
    <a href="<?= $backUrl ?>">← Kembali</a>
    <a class="btn-pdf" href="/academic-calendar/print-report/<?= $year['id'] ?>" target="_blank">
        ⬇ Unduh PDF
    </a>
    <button class="btn-print" onclick="window.print()">🖨 Cetak</button>
</div>

<div class="page-wrap">
    <?= view('letter/_kop', ['letterSettings' => $letterSettings, 'schoolSettings' => $schoolSettings]) ?>

    <div class="doc-title">Kalender Pendidikan</div>
    <div class="doc-subtitle">Tahun Ajaran <?= esc($year['name']) ?></div>

    <?php foreach ($months as $m): ?>
        <?php
            $key   = $m['year'] . '-' . $m['month'];
            $items = $eventsByMonth[$key] ?? [];
        ?>
        <div class="month-title"><?= cal_indo_month($m['month']) . ' ' . $m['year'] ?></div>
        <table class="events">
            <thead>
                <tr>
                    <th style="width:25px;">No</th>
                    <th style="width:140px;">Tanggal</th>
                    <th>Uraian Kegiatan</th>
                    <th style="width:70px;">Kategori</th>
                </tr>
            </thead>
            <tbody>
            <?php if (empty($items)): ?>
                <tr class="empty-row"><td colspan="4">Tidak ada kegiatan khusus pada bulan ini</td></tr>
            <?php else: ?>
                <?php $no = 1; foreach ($items as $it): ?>
                    <?php
                        $tglMulai   = date('d-m-Y', strtotime($it['start_date']));
                        $tglSelesai = date('d-m-Y', strtotime($it['end_date']));
                        $tanggal    = ($tglMulai === $tglSelesai) ? $tglMulai : ($tglMulai . ' s.d. ' . $tglSelesai);
                    ?>
                    <tr>
                        <td style="text-align:center;"><?= $no++ ?></td>
                        <td><?= $tanggal ?></td>
                        <td><?= esc($it['title']) ?><?= !empty($it['description']) ? '<br><span style="color:#666;font-size:9px;">' . esc($it['description']) . '</span>' : '' ?></td>
                        <td style="text-align:center;">
                            <span style="background:<?= esc($it['color']) ?>;color:#fff;padding:1px 5px;border-radius:3px;font-size:9px;">
                                <?= esc($it['category_code']) ?>
                            </span>
                        </td>
                    </tr>
                <?php endforeach; ?>
            <?php endif; ?>
            </tbody>
        </table>
    <?php endforeach; ?>

    <!-- REKAP -->
    <div class="month-title" style="margin-top:10px;">Rekapitulasi Hari Efektif Belajar</div>
    <table class="rekap">
        <thead>
            <tr>
                <th style="width:30px;">No</th>
                <th>Bulan</th>
                <th>Jumlah Hari</th>
                <th>Hari Libur</th>
                <th>Hari Efektif</th>
            </tr>
        </thead>
        <tbody>
        <?php $totalHari = $totalLibur = $totalEfektif = 0;
              foreach ($rekap as $i => $r):
                  $totalHari += $r['total']; $totalLibur += $r['libur']; $totalEfektif += $r['efektif']; ?>
            <tr>
                <td><?= $i + 1 ?></td>
                <td style="text-align:left;"><?= esc($r['label']) ?></td>
                <td><?= $r['total'] ?></td>
                <td><?= $r['libur'] ?></td>
                <td><?= $r['efektif'] ?></td>
            </tr>
        <?php endforeach; ?>
            <tr style="font-weight:bold;background:#f1f1f1;">
                <td colspan="2">Jumlah</td>
                <td><?= $totalHari ?></td>
                <td><?= $totalLibur ?></td>
                <td><?= $totalEfektif ?></td>
            </tr>
        </tbody>
    </table>
    <p style="font-size:9px;color:#555;margin-top:3px;">
        * <?= esc($settings['effective_day_note'] ?? '') ?>
        (acuan: <?= esc($settings['source_reference'] ?? '') ?>)
    </p>

    <!-- LEGENDA -->
    <div class="month-title" style="margin-top:6px;">Keterangan Kategori Kegiatan</div>
    <table class="legend">
        <?php foreach (array_chunk($categories, 2) as $row): ?>
            <tr>
            <?php foreach ($row as $c): ?>
                <td style="width:25%;"><span class="legend-color" style="background:<?= esc($c['color']) ?>"></span> <strong><?= esc($c['code']) ?></strong> — <?= esc($c['name']) ?></td>
            <?php endforeach; ?>
            <?php if (count($row) < 2): ?><td></td><?php endif; ?>
            </tr>
        <?php endforeach; ?>
    </table>

    <!-- TTD -->
    <table class="ttd-table">
        <tr>
            <td style="width:50%;"></td>
            <td style="width:50%;">
                <?= esc($school['letter_city'] ?? $school['city'] ?? '') ?>, <?= date('d F Y') ?><br>
                Kepala <?= esc($school['school_name'] ?? 'Madrasah') ?>
                <br><br><br><br>
                <strong><?= esc($school['principal_name'] ?? '...........................') ?></strong><br>
                NIP. <?= esc($school['principal_nip'] ?? '-') ?>
            </td>
        </tr>
    </table>
</div>
</body>
</html>