<?php helper(['letter', 'logo']); ?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <title>Rekap Bimbingan Konseling & Pengaduan</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            font-size: 11pt;
            color: #333;
            margin: 40px;
        }
        .header {
            text-align: center;
            margin-bottom: 30px;
            border-bottom: 2px solid #333;
            padding-bottom: 10px;
        }
        .header h2 {
            margin: 0;
            text-transform: uppercase;
        }
        .header p {
            margin: 5px 0 0 0;
            font-size: 10pt;
            color: #666;
        }
        .meta-table {
            width: 100%;
            margin-bottom: 20px;
            border-collapse: collapse;
        }
        .meta-table td {
            padding: 4px 0;
            vertical-align: top;
        }
        .meta-table td.label {
            width: 180px;
            font-weight: bold;
        }
        .report-table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 10px;
        }
        .report-table th, .report-table td {
            border: 1px solid #666;
            padding: 8px 10px;
            text-align: left;
            font-size: 9.5pt;
        }
        .report-table th {
            background-color: #f2f2f2;
            font-weight: bold;
            text-align: center;
        }
        .text-center {
            text-align: center !important;
        }
        .text-capitalize {
            text-transform: capitalize;
        }
        .badge {
            display: inline-block;
            padding: 3px 6px;
            font-size: 8pt;
            font-weight: bold;
            border-radius: 3px;
            text-transform: uppercase;
        }
        .badge-open {
            border: 1px solid #28a745;
            color: #28a745;
        }
        .badge-closed {
            border: 1px solid #6c757d;
            color: #6c757d;
        }
        .footer {
            margin-top: 50px;
            text-align: right;
        }
        .signature {
            display: inline-block;
            text-align: center;
            width: 250px;
        }
        .signature-space {
            height: 70px;
        }
        @media print {
            body {
                margin: 20px;
            }
            .no-print {
                display: none;
            }
        }
    </style>
</head>
<body>

    <div class="no-print" style="margin-bottom: 20px; padding: 10px; background-color: #e3f2fd; border: 1px solid #90caf9; border-radius: 5px;">
        <button onclick="window.print()" style="padding: 8px 15px; font-weight: bold; background-color: #0d6efd; color: white; border: none; border-radius: 4px; cursor: pointer;">
            🖨️ Cetak Halaman Ini
        </button>
        <span style="margin-left: 15px; font-size: 9.5pt; color: #555;">Gunakan menu cetak browser Anda untuk menyimpan sebagai PDF.</span>
    </div>

    <?= view('letter/_kop', ['letterSettings' => $schoolSettings, 'schoolSettings' => $schoolSettings]) ?>

    <div style="text-align: center; margin-bottom: 25px;">
        <h3 style="margin: 0; text-transform: uppercase; font-size: 12pt; font-weight: bold; text-decoration: underline;">LAPORAN REKAPITULASI BIMBINGAN KONSELING & PENGADUAN GURU</h3>
    </div>

    <table class="meta-table">
        <tr>
            <td class="label">Nama Guru Penerima</td>
            <td>: <?= esc($teacher['name']) ?></td>
        </tr>
        <tr>
            <td class="label">Periode Akademik</td>
            <td>: <?= esc($period ? ($period['year_name'] . ' - ' . $period['semester']) : 'Semua Periode') ?></td>
        </tr>
        <tr>
            <td class="label">Kriteria Cetak</td>
            <td>: Tipe: <span class="text-capitalize"><?= $type ?: 'Semua' ?></span>, Status: <span class="text-capitalize"><?= $status ?: 'Semua' ?></span></td>
        </tr>
        <tr>
            <td class="label">Tanggal Cetak</td>
            <td>: <?= date('d F Y, H:i') ?></td>
        </tr>
    </table>

    <table class="report-table">
        <thead>
            <tr>
                <th style="width: 5%">No</th>
                <th style="width: 15%">Tanggal</th>
                <th style="width: 15%">Nama Siswa</th>
                <th style="width: 10%">Tipe / Kategori</th>
                <th style="width: 25%">Topik / Subjek</th>
                <th style="width: 20%">Catatan Kasus / Resolusi</th>
                <th style="width: 10%">Status</th>
            </tr>
        </thead>
        <tbody>
            <?php if (empty($tickets)): ?>
                <tr>
                    <td colspan="7" class="text-center" style="padding: 20px; color: #888;">Tidak ada data bimbingan atau pengaduan.</td>
                </tr>
            <?php else: ?>
                <?php $no = 1; foreach ($tickets as $t): ?>
                    <tr>
                        <td class="text-center"><?= $no++ ?></td>
                        <td class="text-center"><?= date('d-m-Y H:i', strtotime($t['created_at'])) ?></td>
                        <td>
                            <?php if ((int)$t['is_anonymous'] === 1): ?>
                                <em>Siswa Anonim (Rahasia)</em>
                            <?php else: ?>
                                <?= esc($t['student_real_name']) ?> <br>
                                <small style="color: #666;">Kelas: <?= esc($t['classroom_name'] ?: '-') ?></small>
                            <?php endif; ?>
                        </td>
                        <td class="text-center">
                            <span style="font-weight: bold; font-size: 8.5pt;" class="text-capitalize"><?= esc($t['type']) ?></span> <br>
                            <small style="color: #555;"><?= esc(get_counseling_category_label($t['category'])) ?></small>
                        </td>
                        <td><?= esc($t['subject']) ?></td>
                        <td>
                            <?php if ($t['internal_notes']): ?>
                                <?= nl2br(esc($t['internal_notes'])) ?>
                            <?php else: ?>
                                <em style="color: #999;">Belum ada catatan resolusi.</em>
                            <?php endif; ?>
                        </td>
                        <td class="text-center">
                            <span class="badge <?= $t['status'] === 'open' ? 'badge-open' : 'badge-closed' ?>">
                                <?= $t['status'] === 'open' ? 'Aktif' : 'Selesai' ?>
                            </span>
                        </td>
                    </tr>
                <?php endforeach; ?>
            <?php endif; ?>
        </tbody>
    </table>

    <?php
    $kota = !empty($schoolSettings['letter_city']) ? $schoolSettings['letter_city'] : (!empty($schoolSettings['city']) ? $schoolSettings['city'] : 'Wonogiri');
    $isMadrasah = (isset($schoolSettings['school_type']) && strtolower($schoolSettings['school_type']) === 'madrasah') || (isset($schoolSettings['school_name']) && stripos($schoolSettings['school_name'], 'madrasah') !== false) || (isset($schoolSettings['school_name']) && stripos($schoolSettings['school_name'], 'man') !== false) || (isset($schoolSettings['school_name']) && stripos($schoolSettings['school_name'], 'mts') !== false) || (isset($schoolSettings['school_name']) && stripos($schoolSettings['school_name'], 'ma') !== false) || (isset($schoolSettings['school_name']) && stripos($schoolSettings['school_name'], 'mi') !== false);
    $principalTitle = $isMadrasah ? 'Kepala Madrasah' : 'Kepala Sekolah';
    ?>
    <table class="footer-sign" style="width: 100%; margin-top: 50px; border-collapse: collapse;">
        <tr>
            <td style="width: 50%; text-align: center; font-size: 11pt; vertical-align: top;">
                <p style="margin-bottom: 5px;">Mengetahui,</p>
                <p style="font-weight: bold; margin-top: 0;"><?= $principalTitle ?></p>
                <div class="signature-space" style="height: 70px;"></div>
                <p style="text-decoration: underline; font-weight: bold; margin-bottom: 0;"><?= esc($schoolSettings['principal_name'] ?? '___________________________') ?></p>
                <p style="font-size: 9.5pt; color: #555; margin-top: 4px;">NIP. <?= esc($schoolSettings['principal_nip'] ?? '-') ?></p>
            </td>
            <td style="width: 50%; text-align: center; font-size: 11pt; vertical-align: top;">
                <p style="margin-bottom: 5px;"><?= esc($kota) ?>, <?= format_tanggal_indonesia(date('Y-m-d')) ?></p>
                <p style="font-weight: bold; margin-top: 0;">Guru,</p>
                <div class="signature-space" style="height: 70px;"></div>
                <p style="text-decoration: underline; font-weight: bold; margin-bottom: 0;"><?= esc(format_person_name($teacher['name'])) ?></p>
                <p style="font-size: 9.5pt; color: #555; margin-top: 4px;"><?= esc(teacher_id_label($teacher['status_kepegawaian'] ?? null)) ?>. <?= esc(teacher_id_display($teacher)) ?></p>
            </td>
        </tr>
    </table>

    <script>
        // Auto trigger print dialogue
        window.onload = function() {
            window.print();
        }
    </script>
</body>
</html>
