<!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 portrait;
            margin: 15mm;
        }
        body {
            font-family: Arial, Helvetica, sans-serif;
            font-size: 11px;
        }
        .kop-table {
            width: 100%;
            border: none;
        }
        .kop-table td {
            border: none;
            vertical-align: middle;
        }
        .logo-kiri {
            width: 80px;
            text-align: left;
        }
        .logo-kanan {
            width: 80px;
            text-align: right;
        }
        .logo-tengah {
            text-align: center;
        }
        .logo-img {
            max-height: 70px;
            max-width: 70px;
        }
        .judul-kop {
            font-size: 14px;
            font-weight: bold;
            margin: 0;
            line-height: 1.3;
        }
        .subjudul-kop {
            font-size: 11px;
            margin: 2px 0;
        }
        .alamat-kop {
            font-size: 8px;
            margin-top: 3px;
        }
        hr {
            border: 0.5px solid #000;
            margin: 10px 0;
        }
        .judul-laporan {
            text-align: center;
            font-size: 14px;
            font-weight: bold;
            margin: 15px 0 5px;
        }
        .subjudul-laporan {
            text-align: center;
            font-size: 11px;
            margin-bottom: 15px;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 10px;
        }
        th, td {
            border: 1px solid #000;
            padding: 6px;
            vertical-align: middle;
        }
        th {
            background-color: #4472C4;
            color: #fff;
            text-align: center;
            font-weight: bold;
        }
        .text-center {
            text-align: center;
        }
        .total-row {
            background-color: #f2f2f2;
            font-weight: bold;
        }
        .footer-ttd {
            margin-top: 40px;
            width: 100%;
        }
        .footer-ttd td {
            border: none;
            text-align: center;
            vertical-align: top;
        }
        .keterangan {
            font-size: 8px;
            margin-top: 15px;
        }
        .page-footer {
            position: fixed;
            bottom: 0;
            width: 100%;
            text-align: center;
            font-size: 8px;
            color: #666;
        }
    </style>
</head>
<body>

<!-- KOP SURAT -->
<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>

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

<!-- TABEL DATA -->
<table>
    <thead>
        <tr>
            <th width="30">NO</th>
            <th width="80">NIS</th>
            <th>NAMA SISWA</th>
            <th width="30">L/P</th>
            <th width="50" style="background-color:#dc3545; color:white;">A</th>
            <th width="50" style="background-color:#17a2b8; color:white;">I</th>
            <th width="50" style="background-color:#ffc107; color:white;">S</th>
            <th width="50">TOTAL</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>
            <td class="text-center"><?= $siswa['alpha'] ?></td>
            <td class="text-center"><?= $siswa['izin'] ?></td>
            <td class="text-center"><?= $siswa['sakit'] ?></td>
            <td class="text-center"><?= $siswa['total'] ?></td>
        </tr>
        <?php endforeach; ?>
        
        <!-- BARIS TOTAL -->
        <tr class="total-row">
            <td colspan="4" class="text-center"><strong>TOTAL</strong></td>
            <td class="text-center"><strong><?= $grandTotalA ?></strong></td>
            <td class="text-center"><strong><?= $grandTotalI ?></strong></td>
            <td class="text-center"><strong><?= $grandTotalS ?></strong></td>
            <td class="text-center"><strong><?= $grandTotalA + $grandTotalI + $grandTotalS ?></strong></td>
        </tr>
    </tbody>
</table>

<!-- KETERANGAN -->
<div class="keterangan">
    <strong>Keterangan:</strong><br>
    A = Alpha (Tidak hadir tanpa keterangan)<br>
    I = Izin (Tidak hadir dengan keterangan)<br>
    S = Sakit (Tidak hadir karena sakit)<br>
    Total Hari Aktif: <?= $totalHariAktif ?? 0 ?> hari
</div>

<!-- TANDA TANGAN -->
<table class="footer-ttd" width="100%">
    <tr>
        <td width="50%" align="center" style="padding-top: 30px;">
            <strong>Mengetahui,</strong><br>
            Kepala Madrasah<br><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: 30px;">
            <?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><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>
