<?php 
helper(['letter', 'logo']); 
$letterSettings = $schoolSettings; // Ensure _kop.php works correctly
?>
<?= $this->extend('layouts/print') ?>
<?= $this->section('content') ?>

<style>
    @page {
        size: A4 landscape;
        margin: 1cm 1cm 1.2cm 1cm;
    }
    
    body {
        font-family: Arial, Helvetica, sans-serif;
        font-size: 8pt;
        line-height: 1.3;
    }

    #surat {
        width: 100%;
        max-width: 100%;
        box-sizing: border-box;
    }

    .table-recap {
        width: 100%;
        border-collapse: collapse;
        margin-bottom: 20px;
        font-size: 8pt;
    }

    .table-recap th, .table-recap td {
        border: 1px solid #000;
        padding: 4px 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;
    }

    /* Print optimization */
    @media print {
        .no-print {
            display: none !important;
        }
        body {
            -webkit-print-color-adjust: exact;
            print-color-adjust: exact;
        }
    }
</style>

<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;">
        <i class="bi bi-printer"></i> 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;">
        <i class="bi bi-x-lg"></i> Tutup Halaman
    </button>
</div>

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

    <!-- JUDUL LAPORAN -->
    <div style="text-align: center; margin-bottom: 15px;">
        <h2 style="font-size: 11pt; font-weight: bold; text-transform: uppercase; margin: 0 0 3px 0; letter-spacing: 0.5px;">
            LAPORAN REKAPITULASI PRESENSI
        </h2>
        <div style="font-size: 9.5pt; font-weight: bold; margin: 0; text-transform: uppercase;">
            BULAN: <?= esc($monthName) ?> <?= esc($year) ?>
        </div>
    </div>

    <!-- INFORMASI KELAS & STATS -->
    <table style="width: 100%; border-collapse: collapse; margin-bottom: 10px; font-size: 8.5pt;">
        <tr>
            <td style="width: 12%; padding: 2px 0; font-weight: bold;">Madrasah</td>
            <td style="width: 38%; padding: 2px 0;">: <?= esc($schoolSettings['school_name']) ?></td>
            <td style="width: 20%; padding: 2px 0; font-weight: bold;">Hari Efektif Belajar</td>
            <td style="width: 30%; padding: 2px 0;">: <?= esc($activeDays) ?> Hari</td>
        </tr>
        <tr>
            <td style="padding: 2px 0; font-weight: bold;">Kelas</td>
            <td style="padding: 2px 0;">: <?= esc($classroom['grade_name'] . ' ' . $classroom['classroom_name']) ?></td>
            <td style="padding: 2px 0; font-weight: bold;">Rata-rata Kehadiran</td>
            <td style="padding: 2px 0;">: <?= esc($averageRate) ?></td>
        </tr>
        <tr>
            <td style="padding: 2px 0; font-weight: bold;">Semester / TA</td>
            <td style="padding: 2px 0;">: <?= esc($activePeriod['semester']) ?> / <?= esc($activePeriod['year_name']) ?></td>
            <td style="padding: 2px 0; font-weight: bold;">Wali Kelas</td>
            <td style="padding: 2px 0;">: <?= esc(format_person_name($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 Lengkap 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: 25px; color: #0dcaf0;">S</th>
                <th style="width: 25px; color: #ffc107;">I</th>
                <th style="width: 25px; color: #dc3545;">A</th>
                <th style="width: 35px;">%</th>
            </tr>
        </thead>
        <tbody>
            <?php if (empty($recap)): ?>
                <tr>
                    <td colspan="<?= count($days) + 7 ?>" style="padding: 15px; text-align: center; color: #555;">
                        Tidak ada data siswa.
                    </td>
                </tr>
            <?php else: ?>
                <?php foreach ($recap as $index => $row): ?>
                    <tr>
                        <td><?= $index + 1 ?></td>
                        <td style="font-family: Courier, monospace;"><?= esc($row['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($row['full_name']) ?></td>
                        <td><?= esc($row['gender']) ?></td>
                        <?php foreach ($days as $day): ?>
                            <?php 
                            $status = $row['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;"><?= $row['sakit'] ?: '-' ?></td>
                        <td style="font-weight: bold;"><?= $row['izin'] ?: '-' ?></td>
                        <td style="font-weight: bold; color: #dc3545;"><?= $row['alpha'] ?: '-' ?></td>
                        <td><?= $row['attendance_rate'] ?>%</td>
                    </tr>
                <?php endforeach; ?>
            <?php endif; ?>
        </tbody>
    </table>

    <!-- TANDA TANGAN -->
    <table style="width: 100%; border-collapse: collapse; font-size: 8.5pt; page-break-inside: avoid; margin-top: 15px;">
        <tr>
            <td style="width: 40%; vertical-align: top; text-align: center;">
                <div style="margin-bottom: 45px;">
                    Mengetahui,<br>
                    Kepala <?= esc($schoolSettings['school_name']) ?>
                </div>
                <div style="font-weight: bold; text-decoration: underline; margin-bottom: 2px;">
                    <?= esc(format_person_name($schoolSettings['principal_name'])) ?>
                </div>
                <div>
                    NIP. <?= esc($schoolSettings['principal_nip'] ?: '-') ?>
                </div>
            </td>
            <td style="width: 20%;">&nbsp;</td>
            <td style="width: 40%; vertical-align: top; text-align: center;">
                <div style="margin-bottom: 45px;">
                    <?php 
                    $lastDateOfMonth = date('Y-m-t', strtotime("$year-$month-01"));
                    ?>
                    <?= esc($schoolSettings['letter_city'] ?: ($schoolSettings['city'] ?: 'Sleman')) ?>, <?= format_tanggal_indonesia($lastDateOfMonth) ?><br>
                    Wali Kelas <?= esc($classroom['grade_name'] . ' ' . $classroom['classroom_name']) ?>
                </div>
                <div style="font-weight: bold; text-decoration: underline; margin-bottom: 2px;">
                    <?= esc(format_person_name($teacher['name'])) ?>
                </div>
                <div>
                    <?= esc(teacher_id_label($teacher['status_kepegawaian'] ?? null)) ?>. <?= esc(teacher_id_display($teacher)) ?>
                </div>
            </td>
        </tr>
    </table>
</div>

<?= $this->endSection() ?>
