<?php
/**
 * Template cetak: Laporan Kalender Pendidikan (list kegiatan per bulan)
 * Dirender via mPDF (orientasi Portrait, A4)
 *
 * Variabel yang tersedia:
 * $year           : data academic_years (array)
 * $school         : data school_settings (array)
 * $months         : array bulan Juli-Juni hasil cal_academic_year_months()
 * $eventsByMonth  : array kegiatan dikelompokkan per "Y-n"
 * $categories     : daftar kategori
 * $rekap          : rekap hari efektif per bulan
 */

?>
<style>
    body { font-family: 'DejaVu Sans', sans-serif; font-size: 10px; color: #222; }
    .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: 13px; font-weight: bold; text-transform: uppercase; }
    .kop-sub { font-size: 9px; }
    .doc-title { text-align: center; font-size: 13px; font-weight: bold; text-transform: uppercase; margin: 8px 0 2px; }
    .doc-subtitle { text-align: center; font-size: 11px; margin-bottom: 10px; }

    table.events { width: 100%; border-collapse: collapse; margin-bottom: 8px; }
    table.events th, table.events td { border: 1px solid #444; padding: 3px 5px; font-size: 9.5px; }
    table.events th { background: #e9ecef; text-align: center; }
    .month-title { background: #343a40; color: #fff; font-weight: bold; padding: 3px 6px; font-size: 10.5px; margin-top: 6px; }
    .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 #444; padding: 3px 5px; font-size: 9.5px; text-align: center; }
    table.rekap th { background: #e9ecef; }

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

    .ttd-table { width: 100%; margin-top: 25px; }
    .ttd-table td { font-size: 10px; text-align: center; vertical-align: top; }
</style>

<?= 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:8.5px;">' . esc($it['description']) . '</span>' : '' ?></td>
                    <td style="text-align:center;">
                        <span style="background:<?= esc($it['color']) ?>; color:#fff; padding:1px 4px; border-radius:3px;">
                            <?= esc($it['category_code']) ?>
                        </span>
                    </td>
                </tr>
            <?php endforeach; ?>
        <?php endif; ?>
        </tbody>
    </table>
<?php endforeach; ?>

<div style="page-break-inside: avoid;">
    <div class="month-title">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 (Minggu &amp; Kategori 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:8.5px; color:#555; margin-top:3px;">
        * <?= esc($settings['effective_day_note'] ?? 'Perhitungan hari libur mingguan menggunakan asumsi Senin–Sabtu hari sekolah, Minggu libur.') ?>
        (acuan: <?= esc($settings['source_reference'] ?? 'SK Dirjen Pendis No. 4261 Tahun 2025') ?>)
    </p>
</div>

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

<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>