<?php
/**
 * Template cetak: Grid Kalender Pendidikan 1 Tahun - PORTRAIT
 * Dirender via mPDF (A4, Portrait) — 6 bulan per halaman (2 kolom x 3 baris)
 */
?>
<style>
    body { font-family: 'DejaVu Sans', sans-serif; font-size: 9px; color: #222; }

    .doc-school  { text-align: center; font-size: 11px; font-weight: bold; text-transform: uppercase; margin: 0; }
    .doc-title   { text-align: center; font-size: 13px; font-weight: bold; text-transform: uppercase; margin: 2px 0 0; }
    .doc-sub     { text-align: center; font-size: 10px; margin: 1px 0 4px; }
    .divider     { border-top: 2px solid #343a40; margin: 0 0 4px; }
    .sem-label   { text-align: center; font-size: 9px; font-weight: bold; color: #555; margin: 0 0 3px; }

    table.grid-outer { width: 100%; border-collapse: separate; border-spacing: 2px; table-layout: fixed; }
    table.grid-outer > tr > td { width: 50%; vertical-align: top; padding: 0; }

    .month-box { border: 1px solid #999; }
    .month-name { background: #343a40; color: #fff; text-align: center; font-weight: bold; font-size: 10px; padding: 3px 0; }

    table.mini-cal { width: 100%; border-collapse: collapse; table-layout: fixed; }
    table.mini-cal th {
        background: #f1f3f5; font-size: 7.5px; font-weight: bold;
        text-align: center; padding: 2px 0; border-bottom: 1px solid #ddd; color: #444;
    }
    table.mini-cal th.dow-min { color: #c00; }
    table.mini-cal td {
        text-align: center; font-size: 8px; height: 14px; padding: 0;
        border-bottom: 1px solid #eee;
    }
    table.mini-cal td.dow-min  { color: #c00; }
    table.mini-cal td.blank    { background: #fafafa; }
    table.mini-cal td.fill     { background: #f5f5f5; }

    table.legend { width: 100%; border-collapse: collapse; margin-top: 5px; }
    table.legend td { border: 1px solid #aaa; padding: 2px 5px; font-size: 8px; vertical-align: middle; }
    .swatch { width: 10px; height: 10px; display: inline-block; border: 1px solid rgba(0,0,0,.2); vertical-align: middle; margin-right: 3px; }
    .leg-code { font-weight: bold; }

    .doc-footer { font-size: 7.5px; color: #888; margin-top: 3px; }
    .page-break { page-break-before: always; }
</style>

<?php $monthChunks = array_chunk($months, 6); ?>

<?php foreach ($monthChunks as $pageIdx => $monthGroup): ?>
<div class="<?= $pageIdx > 0 ? 'page-break' : '' ?>">

    <?php if ($pageIdx === 0): ?>
        <?= view('letter/_kop', ['letterSettings' => $letterSettings, 'schoolSettings' => $schoolSettings]) ?>
        <div class="doc-title">Kalender Pendidikan</div>
        <div class="doc-sub">Tahun Ajaran <?= esc($year['name']) ?></div>
    <?php endif; ?>

    <div class="sem-label"><?= $pageIdx === 0 ? 'Semester Ganjil' : 'Semester Genap' ?></div>

    <table class="grid-outer">
    <?php foreach (array_chunk($monthGroup, 2) as $rowMonths): ?>
        <tr>
        <?php foreach ($rowMonths as $m): ?>
            <td>
                <div class="month-box">
                    <div class="month-name"><?= cal_indo_month($m['month']) . ' ' . $m['year'] ?></div>
                    <table class="mini-cal">
                        <tr>
                            <?php foreach ([0,1,2,3,4,5,6] as $dow): ?>
                                <th class="<?= $dow===0 ? 'dow-min' : '' ?>"><?= cal_indo_day_short($dow) ?></th>
                            <?php endforeach; ?>
                        </tr>
                        <?php
                            $weeks = cal_month_matrix($m['year'], $m['month']);
                            foreach ($weeks as $week):
                        ?>
                            <tr>
                            <?php foreach ($week as $idx => $cell): ?>
                                <?php
                                    if ($cell['date'] === null) {
                                        echo '<td class="blank"></td>'; continue;
                                    }
                                    $info  = $dayMap[$cell['date']] ?? null;
                                    $style = ''; $cls = '';
                                    if ($info) {
                                        $style = 'background:' . esc($info['color']) . ';';
                                        $style .= (int)$info['is_libur'] === 1 ? 'color:#fff;font-weight:bold;' : 'color:#000;';
                                    } elseif (($cell['col'] ?? $idx) === 0) {
                                        $cls = 'dow-min';
                                    }
                                ?>
                                <td class="<?= $cls ?>" style="<?= $style ?>"><?= $cell['day'] ?></td>
                            <?php endforeach; ?>
                            </tr>
                        <?php endforeach; ?>
                        <?php for ($er = count($weeks); $er < 6; $er++): ?>
                            <tr><?php for ($ec=0;$ec<7;$ec++) echo '<td class="fill"></td>'; ?></tr>
                        <?php endfor; ?>
                    </table>
                </div>
            </td>
        <?php endforeach; ?>
        <?php if (count($rowMonths) < 2): ?><td></td><?php endif; ?>
        </tr>
    <?php endforeach; ?>
    </table>

    <?php if ($pageIdx === count($monthChunks) - 1): ?>
        <table class="legend">
        <?php foreach (array_chunk($categories, 2) as $row): ?>
            <tr>
            <?php foreach ($row as $c): ?>
                <td style="width:50%;">
                    <span class="swatch" style="background:<?= esc($c['color']) ?>"></span>
                    <span class="leg-code"><?= esc($c['code']) ?></span> — <?= esc($c['name']) ?>
                </td>
            <?php endforeach; ?>
            <?php if (count($row) < 2): ?><td></td><?php endif; ?>
            </tr>
        <?php endforeach; ?>
        </table>
        <div class="doc-footer">Sumber acuan: <?= esc($settings['source_reference'] ?? '') ?></div>
    <?php endif; ?>

</div>
<?php endforeach; ?>