<!DOCTYPE html>
<html lang="id">
<head>
    <?php helper('logo'); ?>
    <link rel="icon" type="image/png" href="<?= getLogo('sekolah') ?>">
<meta charset="UTF-8">
<title>Cetak Daftar Peserta Ruang</title>
<style>
* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    font-family: Arial, sans-serif;
    background: #e2e8f0;
    padding: 20px;
}

/* Preview Header */
.preview-header {
    background: #1e293b;
    color: white;
    padding: 12px 24px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-radius: 8px;
    margin-bottom: 20px;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
    position: sticky;
    top: 10px;
    z-index: 100;
}
.preview-header h2 {
    font-size: 1.1rem;
    margin: 0;
}
.preview-header p {
    font-size: 0.8rem;
    opacity: 0.8;
    margin-top: 4px;
}
.btn-back {
    background: #475569;
    color: white;
    border: none;
    padding: 8px 16px;
    border-radius: 4px;
    cursor: pointer;
    font-size: 0.8rem;
    margin-right: 8px;
}
.btn-print {
    background: #0ea5e9;
    color: white;
    border: none;
    padding: 8px 20px;
    border-radius: 4px;
    cursor: pointer;
    font-size: 0.8rem;
    font-weight: bold;
}

/* Page Setup: A4 Portrait (210mm x 297mm) */
@page {
    size: A4 portrait;
    margin: 0;
}

.a4-sheet {
    width: 210mm;
    height: 297mm;
    background: white;
    position: relative;
    page-break-after: always;
    margin: 0 auto 20px;
    padding: 10mm 15mm;
    box-shadow: 0 4px 10px rgba(0,0,0,0.15);
    display: flex;
    flex-direction: column;
}

.page-title {
    text-align: center;
    font-size: 13pt;
    font-weight: bold;
    text-transform: uppercase;
    margin-top: 1mm;
    margin-bottom: 3mm;
    text-decoration: underline;
    letter-spacing: 0.5px;
}

.room-badge {
    text-align: center;
    font-size: 16pt;
    font-weight: bold;
    background: #f1f5f9;
    border: 1.5px solid #000;
    padding: 1.5mm 0;
    margin-bottom: 3mm;
    text-transform: uppercase;
}

/* Table */
.att-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 8.5pt;
    margin-bottom: 4mm;
    font-family: Arial, sans-serif;
}
.att-table th, .att-table td {
    border: 1.2px solid #000;
    padding: 0.8mm 1.2mm;
    vertical-align: middle;
}
.att-table td {
    height: 8.2mm;
    line-height: 1.15;
}
.att-table th {
    text-align: center;
    font-weight: bold;
    background: #f8fafc;
    text-transform: uppercase;
}
.event-subtitle {
    text-align: center;
    font-size: 9.5pt;
    font-weight: bold;
    margin-bottom: 3mm;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}
.att-table td.center {
    text-align: center;
}
.nowrap {
    white-space: nowrap !important;
}
.class-header {
    font-size: 12pt;
    font-weight: bold;
    margin-top: 1mm;
    margin-bottom: 2mm;
    text-transform: uppercase;
    border-bottom: 1.5px solid #000;
    padding-bottom: 1mm;
}

/* Signature */
.footer-area {
    display: flex;
    justify-content: flex-end;
    margin-top: auto;
    font-size: 11pt;
}
.sig-box {
    text-align: center;
    width: 60mm;
    position: relative;
}
.sig-space {
    height: 10mm;
    position: relative;
}
.sig-name {
    font-weight: bold;
    text-decoration: underline;
}

/* Print Overrides */
@media print {
    body {
        background: white;
        padding: 0;
        margin: 0;
    }
    .preview-header {
        display: none !important;
    }
    .a4-sheet {
        margin: 0;
        box-shadow: none;
        page-break-after: always;
    }
    * {
        -webkit-print-color-adjust: exact;
        print-color-adjust: exact;
    }
}
</style>
</head>
<body>

<div class="preview-header">
    <div>
        <h2>📝 Cetak Daftar Peserta Ruang (Pintu)</h2>
        <p><?= count($rooms) ?> ruang &nbsp;·&nbsp; Kertas A4 Portrait</p>
    </div>
    <div>
        <button class="btn-back" onclick="window.history.back()">← Kembali</button>
        <button class="btn-print" onclick="window.print()">🖨 Cetak / Simpan PDF</button>
    </div>
</div>

<?php foreach ($rooms as $room): 
    $rStudents = $participantsByRoom[$room['id']] ?? [];
    
    // Group students by classroom
    $studentsByClass = [];
    foreach ($rStudents as $student) {
        $studentsByClass[$student['classroom_name']][] = $student;
    }
?>
<div class="a4-sheet" style="page-break-after: always; margin-bottom: 20px;">
    <!-- Official Kop -->
    <?= view('letter/_kop', ['letterSettings' => $letterSettings, 'schoolSettings' => $schoolSettings]) ?>
    
    <div class="page-title">DAFTAR PESERTA ASESMEN</div>
    <div class="event-subtitle">
        <strong>ASESMEN:</strong> <?= strtoupper(esc($event['name'])) ?> &nbsp;|&nbsp;
        <strong>TA:</strong> <?= strtoupper(esc($event['academic_year'] ?? '2025/2026')) ?>
    </div>
    
    <div class="room-badge">
        RUANG: <?= esc($room['name']) ?>
    </div>

    <?php if (count($studentsByClass) === 1): 
        $classNames = array_keys($studentsByClass);
        $className = $classNames[0];
        $cStudents = $studentsByClass[$className];
        $half = ceil(count($cStudents) / 2);
        $leftStudents = array_slice($cStudents, 0, $half);
        $rightStudents = array_slice($cStudents, $half);
    ?>
        <div class="class-header">KELAS <?= esc(strtoupper($className)) ?></div>
        <div style="display: flex; gap: 5mm; align-items: flex-start;">
            
            <!-- Left Table -->
            <table class="att-table" style="width: 50%; margin-bottom: 0;">
                <thead>
                    <tr>
                        <th width="8%">No</th>
                        <th width="32%" class="nowrap">No. Peserta</th>
                        <th width="60%">Nama Siswa</th>
                    </tr>
                </thead>
                <tbody>
                    <?php 
                    $idx = 1;
                    foreach ($leftStudents as $student): 
                    ?>
                        <tr>
                            <td class="center"><?= $idx++ ?></td>
                            <td class="center nowrap"><?= esc($student['exam_number']) ?></td>
                            <td><?= strtoupper(esc($student['full_name'])) ?></td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>

            <!-- Right Table -->
            <table class="att-table" style="width: 50%; margin-bottom: 0;">
                <thead>
                    <tr>
                        <th width="8%">No</th>
                        <th width="32%" class="nowrap">No. Peserta</th>
                        <th width="60%">Nama Siswa</th>
                    </tr>
                </thead>
                <tbody>
                    <?php 
                    $idx = $half + 1;
                    foreach ($rightStudents as $student): 
                    ?>
                        <tr>
                            <td class="center"><?= $idx++ ?></td>
                            <td class="center nowrap"><?= esc($student['exam_number']) ?></td>
                            <td><?= strtoupper(esc($student['full_name'])) ?></td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>

        </div>

    <?php elseif (count($studentsByClass) > 1): ?>
        
        <div style="display: flex; gap: 5mm; align-items: flex-start; flex-wrap: wrap;">
            <?php foreach ($studentsByClass as $className => $cStudents): ?>
                <div style="flex: 1; min-width: 45%;">
                    <div class="class-header">KELAS <?= esc(strtoupper($className)) ?></div>
                    <table class="att-table">
                        <thead>
                            <tr>
                                <th width="8%">No</th>
                                <th width="32%" class="nowrap">No. Peserta</th>
                                <th width="60%">Nama Siswa</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php 
                            $idx = 1;
                            foreach ($cStudents as $student): 
                            ?>
                                <tr>
                                    <td class="center"><?= $idx++ ?></td>
                                    <td class="center nowrap"><?= esc($student['exam_number']) ?></td>
                                    <td><?= strtoupper(esc($student['full_name'])) ?></td>
                                </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            <?php endforeach; ?>
        </div>

    <?php else: ?>
        <p class="center text-muted" style="font-style: italic; text-align: center; margin-top: 10mm;">
            Belum ada peserta di ruangan ini.
        </p>
    <?php endif; ?>

    <!-- Footer Signature -->
    <div class="footer-area" style="margin-top: 6mm;">
        <div class="sig-box">
            <?php
            $cityName = ucwords(strtolower($schoolSettings['city'] ?? 'Wonogiri'));
            $months = [
                1 => 'Januari', 2 => 'Februari', 3 => 'Maret', 4 => 'April', 5 => 'Mei', 6 => 'Juni',
                7 => 'Juli', 8 => 'Agustus', 9 => 'September', 10 => 'Oktober', 11 => 'November', 12 => 'Desember'
            ];
            $start_time = strtotime($event['start_date']);
            $day = date('d', $start_time);
            $month_num = (int)date('m', $start_time);
            $year = date('Y', $start_time);
            $indonesian_date = $day . ' ' . $months[$month_num] . ' ' . $year;
            ?>
            <div><?= esc($cityName) ?>, <?= esc($indonesian_date) ?></div>
            <div>
                <?php 
                $schoolName = $schoolSettings['school_name'] ?? '';
                if (stripos($schoolName, 'Madrasah') !== false || stripos($schoolName, 'MTs') !== false || stripos($schoolName, 'MA') !== false || stripos($schoolName, 'MI') !== false) {
                    echo 'Kepala Madrasah,';
                } else {
                    echo 'Kepala Sekolah,';
                }
                ?>
            </div>
            <div class="sig-space">
                <?php if (!empty($cardSetting['headmaster_signature']) && file_exists(FCPATH . 'uploads/card/' . $cardSetting['headmaster_signature'])): ?>
                    <img src="<?= base_url('uploads/card/' . $cardSetting['headmaster_signature']) ?>" alt="TTD" style="position: absolute; left: 50%; top: -5mm; transform: translateX(-50%); height: 100px; width: auto; z-index: 10;">
                <?php endif; ?>
            </div>
            <div class="sig-name"><?= esc($schoolSettings['principal_name'] ?? '') ?></div>
            <div>NIP. <?= esc($schoolSettings['principal_nip'] ?? '-') ?></div>
        </div>
    </div>
</div>
<?php endforeach; ?>

</body>
</html>
