<!DOCTYPE html>
<html>
<head>
    <?php helper('logo'); ?>
    <link rel="icon" type="image/png" href="<?= getLogo('sekolah') ?>">
    <meta charset="UTF-8">
    <title><?= esc($judul) ?> - <?= esc($kelas['name']) ?></title>
    <style>
        @page {
            size: A4 landscape;
            margin: 10mm;
        }
        body {
            font-family: Arial, Helvetica, sans-serif;
            font-size: 9px;
        }
        .kop-table {
            width: 100%;
            border: none;
        }
        .kop-table td {
            border: none;
            vertical-align: middle;
        }
        .logo-kiri {
            width: 60px;
            text-align: left;
        }
        .logo-kanan {
            width: 60px;
            text-align: right;
        }
        .logo-tengah {
            text-align: center;
        }
        .logo-img {
            max-height: 50px;
            max-width: 50px;
        }
        .judul-kop {
            font-size: 11px;
            font-weight: bold;
            margin: 0;
            line-height: 1.3;
        }
        .subjudul-kop {
            font-size: 9px;
            margin: 2px 0;
        }
        .alamat-kop {
            font-size: 7px;
            margin-top: 3px;
        }
        hr {
            border: 0.5px solid #000;
            margin: 6px 0;
        }
        .judul-laporan {
            text-align: center;
            font-size: 12px;
            font-weight: bold;
            margin: 8px 0 3px;
        }
        .subjudul-laporan {
            text-align: center;
            font-size: 9px;
            margin-bottom: 8px;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 4px;
        }
        th, td {
            border: 1px solid #000;
            padding: 2px;
            vertical-align: middle;
            font-size: 7px;
        }
        th {
            background-color: #4472C4;
            color: #fff;
            text-align: center;
            font-weight: bold;
        }
        .text-center {
            text-align: center;
        }
        .day-off {
            background-color: #ffe5e5 !important;
        }
        .s { color: #0d6efd; font-weight: bold; }
        .i { color: #fd7e14; font-weight: bold; }
        .a { color: #dc3545; font-weight: bold; }
        .total-row {
            background-color: #f2f2f2;
            font-weight: bold;
        }
        .footer-ttd {
            margin-top: 20px;
            width: 100%;
        }
        .footer-ttd td {
            border: none;
            text-align: center;
            vertical-align: top;
        }
        .keterangan {
            font-size: 7px;
            margin-top: 8px;
        }
        .page-footer {
            position: fixed;
            bottom: 0;
            width: 100%;
            text-align: center;
            font-size: 7px;
            color: #666;
        }
    </style>
</head>
<body>

<table class="kop-table">
    <tr>
        <td class="logo-kiri">
            <img src="<?= getLogo('kemenag') ?>" class="logo-img" alt="Logo Kemenag">
        </td>
        <td class="logo-tengah">
            <div class="judul-kop">KEMENTERIAN AGAMA REPUBLIK INDONESIA</div>
            <div class="judul-kop">KANTOR KEMENTERIAN AGAMA KAB. WONOGIRI</div>
            <div class="judul-kop">MADRASAH TSANAWIYAH NEGERI 1 WONOGIRI</div>
            <div class="alamat-kop">
                Jl. Tandon Giriwono Wonogiri, Telp. (0273) 321839<br>
                email : mtsn1wonogiri@gmail.com
            </div>
        </td>
        <td class="logo-kanan">
            <img src="<?= getLogo('sekolah') ?>" class="logo-img" alt="Logo Sekolah">
        </td>
    </tr>
</table>
<hr>

<div class="judul-laporan"><?= esc($judul) ?></div>
<div class="subjudul-laporan">
    <?= esc($subjudul) ?> | Kelas: <?= esc($kelas['name']) ?> (<?= esc($kelas['level']) ?>)
</div>

<table>
    <thead>
        <tr>
            <th width="18">#</th>
            <th width="50">NIS</th>
            <th>Nama Siswa</th>
            <th width="16">L/P</th>
            <?php foreach ($days as $d): ?>
            <th width="14" class="<?= $d['is_off'] ? 'day-off' : '' ?>"><?= $d['day'] ?></th>
            <?php endforeach; ?>
            <th width="18" style="background:#0d6efd;">S</th>
            <th width="18" style="background:#fd7e14;">I</th>
            <th width="18" style="background:#dc3545;">A</th>
            <th width="18" style="background:#198754;">H</th>
        </tr>
    </thead>
    <tbody>
        <?php $no = 1; foreach ($dataSiswa as $siswa): ?>
        <tr>
            <td class="text-center"><?= $no++ ?></td>
            <td class="text-center"><?= esc($siswa['nis']) ?></td>
            <td><?= esc(format_person_name($siswa['full_name'])) ?></td>
            <td class="text-center"><?= esc($siswa['gender']) ?></td>
            <?php foreach ($days as $d): ?>
            <?php if ($d['is_off']): ?>
            <td class="day-off"></td>
            <?php else: ?>
            <?php $st = $siswa['statuses'][$d['day']] ?? ''; ?>
            <td class="text-center <?= $st === 'S' ? 's' : ($st === 'I' ? 'i' : ($st === 'A' ? 'a' : '')) ?>"><?= $st ?></td>
            <?php endif; ?>
            <?php endforeach; ?>
            <td class="text-center"><?= $siswa['sakit'] ?></td>
            <td class="text-center"><?= $siswa['izin'] ?></td>
            <td class="text-center"><?= $siswa['alpha'] ?></td>
            <td class="text-center"><?= $siswa['hadir'] ?></td>
        </tr>
        <?php endforeach; ?>
        <tr class="total-row">
            <td colspan="4" class="text-center"><strong>TOTAL</strong></td>
            <?php foreach ($days as $d): ?>
            <td class="<?= $d['is_off'] ? 'day-off' : '' ?>"></td>
            <?php endforeach; ?>
            <td class="text-center"><strong><?= $totalS ?></strong></td>
            <td class="text-center"><strong><?= $totalI ?></strong></td>
            <td class="text-center"><strong><?= $totalA ?></strong></td>
            <td class="text-center"><strong><?= $totalH ?></strong></td>
        </tr>
    </tbody>
</table>

<div class="keterangan">
    <strong>Keterangan:</strong>
    . = Hadir &nbsp;|&nbsp; S = Sakit &nbsp;|&nbsp; I = Izin &nbsp;|&nbsp; A = Alpha &nbsp;|&nbsp; <span style="background:#ffe5e5">&nbsp;&nbsp;</span> = Libur
</div>

<table class="footer-ttd" width="100%">
    <tr>
        <td width="50%" align="center" style="padding-top: 20px;">
            <strong>Mengetahui,</strong><br>
            Kepala Madrasah<br><br><br>
            <strong>
                <?php if (!empty($kepalaSekolah['name'])): ?>
                    <u><?= esc(format_person_name($kepalaSekolah['name'])) ?></u>
                <?php else: ?>
                    _________________________
                <?php endif; ?>
            </strong><br>
            NIP. <?= esc($kepalaSekolah['nip'] ?: '_________________________') ?>
        </td>
        <td width="50%" align="center" style="padding-top: 20px;">
            <?php 
            $kota = !empty($kepalaSekolah['city']) ? $kepalaSekolah['city'] : 'Wonogiri';
            $bulanIndonesia = [
                'January' => 'Januari', 'February' => 'Februari', 'March' => 'Maret',
                'April' => 'April', 'May' => 'Mei', 'June' => 'Juni',
                'July' => 'Juli', 'August' => 'Agustus', 'September' => 'September',
                'October' => 'Oktober', 'November' => 'November', 'December' => 'Desember'
            ];
            $tgl = date('d');
            $bln = $bulanIndonesia[date('F')];
            $thn = date('Y');
            $tanggalIndonesia = $kota . ', ' . $tgl . ' ' . $bln . ' ' . $thn;
            ?>
            <strong><?= $tanggalIndonesia ?></strong><br>
            <strong>Guru BK</strong><br><br><br>
            <strong>
                <?php if (!empty($waliKelas['teacher_name'])): ?>
                    <u><?= esc(format_person_name($waliKelas['teacher_name'])) ?></u>
                <?php else: ?>
                    _________________________
                <?php endif; ?>
            </strong><br>
            <?= esc(guess_id_label($waliKelas['teacher_nip'] ?? null)) ?>. <?= esc($waliKelas['teacher_nip'] ?: '_________________________') ?>
        </td>
    </tr>
</table>

<div class="page-footer">
    Dicetak pada: <?= date('d-m-Y H:i:s') ?>
</div>

<script>
    window.onload = function() {
        window.print();
    }
</script>

</body>
</html>