<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?= esc($title) ?></title>
    <style>
        body {
            font-family: 'Times New Roman', Times, serif;
            font-size: 11pt;
            color: #000;
            margin: 0;
            padding: 0;
            background-color: #fff;
        }
        
        /* Landscape Print Layout */
        @page {
            size: A4 landscape;
            margin: 15mm;
        }

        .no-print {
            background-color: #e9ecef;
            padding: 10px;
            text-align: center;
            border-bottom: 1px solid #dee2e6;
            margin-bottom: 20px;
        }
        .btn-print {
            padding: 8px 20px;
            font-weight: bold;
            background-color: #0d6efd;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }
        .btn-close {
            padding: 8px 20px;
            font-weight: bold;
            background-color: #6c757d;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            margin-left: 10px;
        }

        @media print {
            .no-print {
                display: none;
            }
            .page-break {
                page-break-before: always;
            }
            body {
                background: none;
                margin: 0;
            }
        }

        .journal-page {
            width: 100%;
            margin-bottom: 30px;
        }

        .title-section {
            text-align: center;
            margin: 15px 0;
        }
        .title-section h3 {
            margin: 0 0 5px 0;
            text-transform: uppercase;
            font-size: 14pt;
            font-weight: bold;
        }
        .title-section p {
            margin: 0;
            font-size: 11pt;
        }

        .info-table {
            width: 100%;
            margin-bottom: 15px;
            border-collapse: collapse;
        }
        .info-table td {
            padding: 4px 0;
            vertical-align: top;
        }

        .data-table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 25px;
        }
        .data-table th, .data-table td {
            border: 1px solid #000;
            padding: 8px;
            font-size: 10pt;
        }
        .data-table th {
            background-color: #f2f2f2;
            text-align: center;
            font-weight: bold;
        }
        .text-center {
            text-align: center;
        }

        .signature-table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }
        .signature-table td {
            vertical-align: top;
            text-align: center;
            width: 50%;
            font-size: 11pt;
        }
    </style>
</head>
<body>

<div class="no-print">
    <button onclick="window.print()" class="btn-print">Cetak Jurnal</button>
    <button onclick="window.close()" class="btn-close">Tutup</button>
</div>

<?php 
$isFirst = true;
if (empty($groupedJournals)): 
?>
<div style="text-align: center; margin-top: 100px;">
    <h2>TIDAK ADA DATA KBM</h2>
    <p>Belum ada jurnal mengajar yang diisi untuk kelas <?= esc($classroom['name']) ?> pada bulan <?= esc($monthName) ?> <?= esc($year) ?>.</p>
</div>
<?php 
else:
    foreach ($groupedJournals as $date => $dayJournals):
        // Page break for subsequent pages
        $pageBreakClass = $isFirst ? '' : 'page-break';
        $isFirst = false;
        
        // Load custom school city or default to Wonogiri
        $kota = !empty($schoolSettings['letter_city']) ? $schoolSettings['letter_city'] : (!empty($schoolSettings['city']) ? $schoolSettings['city'] : 'Wonogiri');
?>
<div class="journal-page <?= $pageBreakClass ?>">

    <!-- Title Section -->
    <div class="title-section">
        <h3>JURNAL MENGAJAR HARIAN KELAS</h3>
        <p>Bulan: <?= esc($monthName) ?> <?= esc($year) ?></p>
    </div>

    <!-- Info Section -->
    <table class="info-table">
        <tr>
            <td style="width: 12%;"><strong>Kelas</strong></td>
            <td style="width: 38%;">: <?= esc($classroom['name']) ?></td>
            <td style="width: 15%;"><strong>Hari / Tanggal</strong></td>
            <td style="width: 35%;">: <?= format_tanggal_indonesia($date) ?></td>
        </tr>
        <tr>
            <td><strong>Semester</strong></td>
            <td>: <?= esc($activePeriod['semester']) ?></td>
            <td><strong>Tahun Ajaran</strong></td>
            <td>: <?= esc($activePeriod['academic_year_name']) ?></td>
        </tr>
    </table>

    <!-- Main KBM Table -->
    <table class="data-table">
        <thead>
            <tr>
                <th style="width: 8%;">Jam Ke-</th>
                <th style="width: 18%;">Mata Pelajaran</th>
                <th style="width: 20%;">Nama Guru</th>
                <th style="width: 25%;">Materi / Topik Pembelajaran</th>
                <th style="width: 15%;">Absensi Siswa (Tidak Hadir)</th>
                <th style="width: 14%;">Catatan / Keterangan</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($dayJournals as $j): ?>
            <tr>
                <td class="text-center">
                    Jam <?= esc($j['start_period']) ?> - <?= esc($j['end_period']) ?>
                </td>
                <td><?= esc($j['subject_name']) ?></td>
                <td><?= esc(format_person_name($j['teacher_name'])) ?></td>
                <td><?= esc($j['topic']) ?></td>
                <td>
                    <?php 
                    if (empty($j['absentees'])): 
                        echo '<span style="color: green; font-style: italic;">Hadir Semua</span>';
                    else:
                        $absList = [];
                        foreach ($j['absentees'] as $abs) {
                            $keteranganSingkat = substr($abs['status'], 0, 1); // S / I / A
                            $absList[] = esc($abs['full_name']) . ' (' . $keteranganSingkat . ')';
                        }
                        echo implode('<br>', $absList);
                    endif;
                    ?>
                </td>
                <td><?= esc($j['notes'] ?: '-') ?></td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>

    <!-- Signature Section -->
    <table class="signature-table" style="width: 100%;">
        <tr>
            <td style="width: 60%;">&nbsp;</td>
            <td style="width: 40%; text-align: center;">
                <?= esc($kota) ?>, <?= format_tanggal_indonesia($date) ?><br>
                Wali Kelas<br><br><br><br>
                <strong><u><?= $homeroom ? esc(format_person_name($homeroom['teacher_name'])) : '_____________________' ?></u></strong><br>
                <?= $homeroom ? esc(guess_id_label($homeroom['teacher_nip'] ?? null)) . '. ' . esc($homeroom['teacher_nip'] ?: '-') : '-' ?>
            </td>
        </tr>
    </table>
</div>
<?php 
    endforeach;
endif; 
?>

</body>
</html>
