<!DOCTYPE html>
<html>
<head>
    <?php helper(['logo', 'letter']); ?>
    <link rel="icon" type="image/png" href="<?= getLogo('sekolah') ?>">
    <title><?= esc($judul) ?> - <?= esc($kelas['name']) ?></title>
    <style>
        @page {
            size: A4 landscape;
            margin: 10mm 10mm 12mm 10mm;
        }
        body {
            font-family: Arial, Helvetica, sans-serif;
            font-size: 8pt;
            line-height: 1.3;
        }
        .kop-table {
            width: 100%;
            border: none;
            margin-bottom: 5px;
        }
        .kop-table td {
            border: none;
            vertical-align: middle;
            padding: 2px;
        }
        .logo-kiri {
            width: 70px;
            text-align: left;
        }
        .logo-kanan {
            width: 70px;
            text-align: right;
        }
        .logo-tengah {
            text-align: center;
        }
        .logo-img {
            max-height: 55px;
            max-width: 55px;
        }
        .judul-kop {
            font-size: 11pt;
            font-weight: bold;
            margin: 0;
            line-height: 1.2;
        }
        .alamat-kop {
            font-size: 7.5pt;
            margin-top: 2px;
        }
        hr {
            border: 0.5px solid #000;
            margin: 5px 0 10px 0;
        }
        .judul-laporan {
            text-align: center;
            font-size: 11pt;
            font-weight: bold;
            margin: 5px 0 2px;
        }
        .subjudul-laporan {
            text-align: center;
            font-size: 9.5pt;
            margin-bottom: 10px;
            font-weight: bold;
        }
        .table-info {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 10px;
            font-size: 8.5pt;
        }
        .table-info td {
            border: none;
            padding: 2px 0;
        }
        .table-recap {
            width: 100%;
            border-collapse: collapse;
            font-size: 8pt;
            margin-bottom: 15px;
        }
        .table-recap th, .table-recap td {
            border: 1px solid #000;
            padding: 3px 2px;
            text-align: center;
        }
        .table-recap th {
            background-color: #f2f2f2;
            font-weight: bold;
        }
        .cell-off {
            background-color: #ffe5e5 !important;
            color: #dc3545;
        }
        .cell-sakit {
            color: #0dcaf0;
            font-weight: bold;
        }
        .cell-izin {
            color: #ffc107;
            font-weight: bold;
        }
        .cell-alpha {
            color: #dc3545;
            font-weight: bold;
        }
        .cell-present {
            color: #999;
        }
        .total-row {
            background-color: #f2f2f2;
            font-weight: bold;
        }
        .footer-ttd {
            width: 100%;
            margin-top: 15px;
            font-size: 8.5pt;
        }
        .footer-ttd td {
            border: none;
            text-align: center;
            vertical-align: top;
            padding: 5px;
        }
        @media print {
            .no-print {
                display: none !important;
            }
            body {
                -webkit-print-color-adjust: exact;
                print-color-adjust: exact;
            }
        }
    </style>
</head>
<body>

<div class="no-print" style="margin: 10px 0; padding: 10px; background: #f5f5f5; text-align: center;">
    <button onclick="window.print()" class="btn-print" style="padding: 6px 15px; background: #007bff; color: white; border: none; border-radius: 4px; cursor: pointer;">
        Cetak Laporan
    </button>
    <button onclick="window.close()" class="btn-back" style="padding: 6px 15px; background: #6c757d; color: white; border: none; border-radius: 4px; cursor: pointer; margin-left: 10px;">
        Tutup Halaman
    </button>
</div>

<!-- KOP SURAT -->
<?= view('letter/_kop', ['letterSettings' => $schoolSettings, 'schoolSettings' => $schoolSettings]) ?>

<!-- JUDUL LAPORAN -->
<div class="judul-laporan"><?= esc($judul) ?></div>
<div class="subjudul-laporan"><?= esc($subjudul) ?></div>

<!-- INFORMASI KELAS & STATS -->
<table class="table-info">
    <tr>
        <td style="width: 12%; font-weight: bold;">Madrasah</td>
        <td style="width: 38%;">: MTs Negeri 1 Wonogiri</td>
        <td style="width: 20%; font-weight: bold;">Hari Efektif Belajar</td>
        <td style="width: 30%;">: <?= esc($totalHariAktif) ?> Hari</td>
    </tr>
    <tr>
        <td style="font-weight: bold;">Kelas</td>
        <td>: <?= esc($kelas['name']) ?> (<?= esc($kelas['level']) ?>)</td>
        <td style="font-weight: bold;">Guru BK</td>
        <td>: <?= esc(format_person_name($waliKelas['teacher_name'])) ?></td>
    </tr>
</table>

<!-- TABEL REKAP KEHADIRAN -->
<table class="table-recap">
    <thead>
        <tr>
            <th style="width: 30px;">NO</th>
            <th style="width: 70px;">NIS</th>
            <th style="text-align: left; padding-left: 5px; width: 140px;">NAMA SISWA</th>
            <th style="width: 30px;">L/P</th>
            <?php foreach ($days as $day): ?>
                <th style="width: 18px; <?= $day['is_off'] ? 'background-color: #ffe5e5; color: #dc3545;' : '' ?>" title="<?= esc($day['holiday_title'] ?: '') ?>">
                    <?= $day['day'] ?>
                </th>
            <?php endforeach; ?>
            <th style="width: 22px; color: #0dcaf0;">S</th>
            <th style="width: 22px; color: #ffc107;">I</th>
            <th style="width: 22px; color: #dc3545;">A</th>
            <th style="width: 25px;">H</th>
        </tr>
    </thead>
    <tbody>
        <?php $no = 1; foreach ($dataSiswa as $siswa): ?>
        <tr>
            <td><?= $no++ ?></td>
            <td style="font-family: Courier, monospace;"><?= esc($siswa['nis']) ?></td>
            <td style="text-align: left; padding-left: 5px; font-weight: bold; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 140px;"><?= esc(format_person_name($siswa['full_name'])) ?></td>
            <td><?= esc($siswa['gender']) ?></td>
            <?php foreach ($days as $day): ?>
                <?php 
                $status = $siswa['statuses'][$day['day']] ?? '.';
                $class = '';
                if ($status === 'S') $class = 'cell-sakit';
                elseif ($status === 'I') $class = 'cell-izin';
                elseif ($status === 'A') $class = 'cell-alpha';
                elseif ($status === '.') $class = 'cell-present';
                ?>
                <td class="<?= $class ?> <?= $day['is_off'] ? 'cell-off' : '' ?>">
                    <?= $status ?>
                </td>
            <?php endforeach; ?>
            <td style="font-weight: bold;"><?= $siswa['sakit'] ?: '-' ?></td>
            <td style="font-weight: bold;"><?= $siswa['izin'] ?: '-' ?></td>
            <td style="font-weight: bold; color: #dc3545;"><?= $siswa['alpha'] ?: '-' ?></td>
            <td style="font-weight: bold;"><?= $totalHariAktif - ($siswa['sakit'] + $siswa['izin'] + $siswa['alpha']) ?></td>
        </tr>
        <?php endforeach; ?>
        
        <!-- BARIS TOTAL -->
        <tr class="total-row">
            <td colspan="4" style="text-align: center;"><strong>TOTAL ABSEN KELAS</strong></td>
            <?php foreach ($days as $day): ?>
                <td class="<?= $day['is_off'] ? 'cell-off' : '' ?>">&nbsp;</td>
            <?php endforeach; ?>
            <td><strong><?= $grandTotalS ?></strong></td>
            <td><strong><?= $grandTotalI ?></strong></td>
            <td style="color: #dc3545;"><strong><?= $grandTotalA ?></strong></td>
            <td><strong>-</strong></td>
        </tr>
    </tbody>
</table>

<!-- TANDA TANGAN -->
<table class="footer-ttd" style="width: 100%; border-collapse: collapse;">
    <tr>
        <td style="width: 40%; text-align: center;">
            <strong>Mengetahui,</strong><br>
            Kepala Madrasah<br><br><br><br>
            <strong><u><?= esc(format_person_name($kepalaSekolah['name'])) ?></u></strong><br>
            NIP. <?= esc($kepalaSekolah['nip'] ?: '-') ?>
        </td>
        <td style="width: 20%;">&nbsp;</td>
        <td style="width: 40%; text-align: center;">
            <?php 
            $kota = !empty($kepalaSekolah['city']) ? $kepalaSekolah['city'] : 'Wonogiri';
            $lastDateOfMonth = date('Y-m-t', strtotime("$year-$month-01"));
            ?>
            <strong><?= $kota ?>, <?= format_tanggal_indonesia($lastDateOfMonth) ?></strong><br>
            <strong>Guru BK</strong><br><br><br><br>
            <strong><u><?= esc(format_person_name($waliKelas['teacher_name'])) ?></u></strong><br>
            <?= esc(guess_id_label($waliKelas['teacher_nip'] ?? null)) ?>. <?= esc($waliKelas['teacher_nip'] ?: '-') ?>
        </td>
    </tr>
</table>

</body>
</html>