<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <title>Jurnal Mengajar - Semua Kelas - <?= esc($teacher['name']) ?></title>
    <style>
        @page { size: landscape; margin: 15mm 12mm; }
        body { font-family: 'Times New Roman', Times, serif; font-size: 10pt; color: #000; margin: 0; padding: 0; }

        .title { text-align: center; margin-bottom: 12px; }
        .title h1 { font-size: 14pt; margin: 0 0 2px; text-transform: uppercase; font-weight: bold; }
        .title .divider { width: 80px; height: 2px; background: #000; margin: 6px auto; }

        .info { margin-bottom: 10px; font-size: 10pt; }
        .info table { width: 100%; border-collapse: collapse; }
        .info td { padding: 1px 4px; vertical-align: top; }
        .info .label { width: 100px; }

        .section-title { font-weight: bold; font-size: 11pt; margin: 16px 0 6px; border-bottom: 1px solid #000; padding-bottom: 2px; }

        table.jurnal { width: 100%; border-collapse: collapse; font-size: 9pt; margin-bottom: 6px; }
        table.jurnal th, table.jurnal td { border: 1px solid #000; padding: 3px 5px; vertical-align: top; text-align: left; }
        table.jurnal th { background: #f0f0f0; text-align: center; font-weight: bold; }
        table.jurnal td.tc { text-align: center; }

        .abs-s { color: #b45309; }
        .abs-i { color: #0369a1; }
        .abs-a { color: #b91c1c; }

        .summary { margin-top: 10px; padding: 6px 8px; border: 1px solid #000; font-size: 9pt; }

        .footer { margin-top: 24px; }
        .footer table { width: 100%; }
        .footer td { width: 50%; text-align: center; padding-top: 36px; font-size: 10pt; }
        .sign-line { border-top: 1px solid #000; width: 200px; margin: 0 auto; padding-top: 4px; font-size: 10pt; }
        .page-break { page-break-before: always; }
        .no-print { display: none; }
    </style>
</head>
<body>

<div class="title">
    <h1>JURNAL MENGAJAR</h1>
    <div class="divider"></div>
</div>

<div class="info">
    <table>
        <tr><td class="label">Guru</td><td>: <?= esc($teacher['name']) ?></td></tr>
        <tr><td class="label">Tahun Ajaran</td><td>: <?= esc($period['academic_year_name'] ?? $period['name'] ?? '—') ?></td></tr>
        <tr><td class="label">Semester</td><td>: <?= esc($period['semester'] ?? '—') ?></td></tr>
    </table>
</div>

<?php
$currentClass = null;
$currentSubject = null;
$totalHadirAll = 0;
$totalSiswaAll = 0;
$pageCount = 0;
$schoolType = $school['school_type'] ?? '';
$kepalaLabel = in_array($schoolType, ['SD','SMP','SMA','SMK']) ? 'Kepala Sekolah' : 'Kepala Madrasah';
?>

<?php foreach ($journals as $j):
    $absList = $absMap[$j['id']] ?? [];
    $totalHadirAll += (int)$j['total_present'];
    $totalSiswaAll += (int)$j['total_students'];

    $classKey = $j['classroom_id'] . '-' . $j['school_subject_id'];
    $classroomName = $j['classroom_name'] ?? 'Kelas';
    $subjectName = $j['subject_name'] ?? 'Mapel';

    if ($currentClass !== $classKey):
        if ($currentClass !== null): ?>
            </tbody></table>
        <?php endif;
        $currentClass = $classKey;
        $pageCount++; ?>
        <div class="section-title">Kelas: <?= esc($classroomName) ?> — Mata Pelajaran: <?= esc($subjectName) ?></div>
        <table class="jurnal">
            <thead>
                <tr>
                    <th style="width:28px;">No</th>
                    <th style="width:70px;">Tanggal</th>
                    <th style="width:36px;">Jam</th>
                    <th style="width:36px;">Pert.</th>
                    <th style="width:30%;">Materi Pokok</th>
                    <th style="width:32%;">Siswa Tidak Hadir (S/I/A)</th>
                    <th style="width:15%;">Catatan</th>
                </tr>
            </thead>
            <tbody>
    <?php endif; ?>
        <tr>
            <td class="tc"><?= $j['meeting_number'] ?></td>
            <td class="tc"><?= date('d/m/Y', strtotime($j['meeting_date'])) ?></td>
            <td class="tc"><?= $j['start_period'] ?>-<?= $j['end_period'] ?></td>
            <td class="tc"><?= $j['meeting_number'] ?></td>
            <td><?= esc($j['topic']) ?></td>
            <td>
                <?php if (!empty($absList)):
                    $sickN = []; $permitN = []; $absentN = [];
                    foreach ($absList as $a) {
                        if ($a['status'] === 'Sakit') $sickN[] = $a['full_name'];
                        elseif ($a['status'] === 'Izin') $permitN[] = $a['full_name'];
                        else $absentN[] = $a['full_name'];
                    } ?>
                    <?php if (!empty($sickN)): ?><span class="abs-s">S: <?= implode(', ', $sickN) ?></span><br><?php endif; ?>
                    <?php if (!empty($permitN)): ?><span class="abs-i">I: <?= implode(', ', $permitN) ?></span><br><?php endif; ?>
                    <?php if (!empty($absentN)): ?><span class="abs-a">A: <?= implode(', ', $absentN) ?></span><?php endif; ?>
                <?php else: ?>
                    <span style="color:#999;">—</span>
                <?php endif; ?>
            </td>
            <td><?= esc($j['notes']) ?></td>
        </tr>
<?php endforeach; ?>
        </tbody>
    </table>

<div class="summary">
    <strong>Ringkasan:</strong>
    Total pertemuan: <strong><?= count($journals) ?></strong>
    &nbsp;|&nbsp;
    Rata-rata kehadiran: <?= $totalSiswaAll > 0 ? round(($totalHadirAll / $totalSiswaAll) * 100, 1) : 0 ?>%
</div>

<div class="footer">
    <table>
        <tr>
            <td>
                Mengetahui,<br>
                <?= $kepalaLabel ?>,<br><br><br><br>
                <strong><?= esc($school['principal_name'] ?? '') ?></strong>
                <div style="font-size:9pt;">NIP. <?= esc($school['principal_nip'] ?? '') ?></div>
            </td>
            <td>
                <?= esc($school['city'] ?? '') ?>, ....................<br>
                Guru Mata Pelajaran,<br><br><br><br>
                <strong><?= esc($teacher['name'] ?? '') ?></strong>
                <div style="font-size:9pt;"><?= esc(teacher_id_label($teacher['status_kepegawaian'] ?? null)) ?>. <?= esc(teacher_id_display($teacher)) ?></div>
            </td>
        </tr>
    </table>
</div>

<script>window.print();</script>
</body>
</html>
