<?php
/**
 * Template cetak: Grid Kalender Pendidikan 1 Tahun - LANDSCAPE
 * Dirender via mPDF (A4, Landscape)
 */
?>
<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 5px; }
    .divider     { border-top: 2px solid #343a40; margin: 0 0 4px; }

    table.grid-outer { width: 100%; border-collapse: separate; border-spacing: 2px; table-layout: fixed; }
    table.grid-outer > tr > td { width: 25%; 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: 8.5px; padding: 2px 0; }

    table.mini-cal { width: 100%; border-collapse: collapse; table-layout: fixed; }
    table.mini-cal th {
        background: #f1f3f5; font-size: 7px; font-weight: bold;
        text-align: center; padding: 1.5px 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: 7.5px; height: 12px; 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: 4px; }
    table.legend td { border: 1px solid #aaa; padding: 1.5px 4px; font-size: 7.5px; vertical-align: middle; }
    .swatch { width: 10px; height: 10px; display: inline-block; border: 1px solid rgba(0,0,0,.2); vertical-align: middle; margin-right: 2px; }
    .leg-code { font-weight: bold; }

    .doc-footer { font-size: 7px; color: #888; margin-top: 3px; }
</style>

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

<table class="grid-outer">
<?php foreach (array_chunk($months, 4) 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 for ($i=count($rowMonths);$i<4;$i++) echo '<td></td>'; ?>
    </tr>
<?php endforeach; ?>
</table>

<table class="legend">
<?php foreach (array_chunk($categories, 4) as $row): ?>
    <tr>
    <?php foreach ($row as $c): ?>
        <td style="width:25%;">
            <span class="swatch" style="background:<?= esc($c['color']) ?>"></span>
            <span class="leg-code"><?= esc($c['code']) ?></span> — <?= esc($c['name']) ?>
        </td>
    <?php endforeach; ?>
    <?php for ($i=count($row);$i<4;$i++) echo '<td></td>'; ?>
    </tr>
<?php endforeach; ?>
</table>

<div class="doc-footer">Sumber acuan: <?= esc($settings['source_reference'] ?? '') ?></div>