<!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 Hadir Pengawas - <?= esc($event['name']) ?></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 10mm;
    box-shadow: 0 4px 10px rgba(0,0,0,0.15);
    display: flex;
    flex-direction: column;
}

/* Title & Info */
.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;
}

.info-table {
    width: 100%;
    margin-bottom: 4mm;
    font-size: 9.5pt;
    line-height: 1.35;
    border-collapse: collapse;
}
.info-table td {
    padding: 0.8mm 0;
    vertical-align: top;
}

/* Table */
.att-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 10.5pt;
    margin-bottom: 4mm;
}
.att-table th, .att-table td {
    border: 1.2px solid #000;
    padding: 1.5mm 2mm;
    vertical-align: middle;
}
.att-table th {
    text-align: center;
    font-weight: bold;
    background: #f8fafc;
    text-transform: uppercase;
}
.att-table td.center {
    text-align: center;
}
.sig-cell {
    position: relative;
    height: 10.5mm;
    vertical-align: middle;
}
.sig-num {
    position: absolute;
    left: 1.5mm;
    top: 50%;
    transform: translateY(-50%);
    font-size: 8pt;
    color: #475569;
}

/* Signature */
.footer-signatures {
    display: flex;
    justify-content: flex-end;
    margin-top: auto;
    font-size: 11pt;
    padding-bottom: 5mm;
}
.sig-box {
    text-align: center;
    width: 65mm;
    position: relative;
}
.sig-space {
    height: 12mm;
    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 Hadir Pengawas</h2>
        <p><?= count($sessions) ?> sesi ujian &nbsp;·&nbsp; Kertas A4 Portrait (Margin 1cm)</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 ($sessions as $sess): 
    $totalPages = count($sess['pages']);
    $globalIdx = 1;
    
    foreach ($sess['pages'] as $pIdx => $pageProctors):
        $isFirstPage = ($pIdx === 0);
        $isLastPage = ($pIdx === $totalPages - 1);
?>
<div class="a4-sheet">
    <?php if ($isFirstPage): ?>
        <!-- Official Kop -->
        <?= view('letter/_kop', ['letterSettings' => $letterSettings, 'schoolSettings' => $schoolSettings]) ?>
        
        <div class="page-title">DAFTAR HADIR PENGAWAS ASESMEN</div>
        
        <!-- Info Ujian -->
        <table class="info-table">
            <tr>
                <td width="18%"><strong>ASESMEN</strong></td>
                <td width="2%">:</td>
                <td width="40%"><?= strtoupper(esc($event['name'])) ?></td>
                
                <td width="18%"><strong>HARI / TANGGAL</strong></td>
                <td width="2%">:</td>
                <td width="22%"><?= strtoupper(esc($sess['exam_day'])) ?>, <?= date('d/m/Y', strtotime($sess['exam_date'])) ?></td>
            </tr>
            <tr>
                <td><strong>TAHUN AJARAN</strong></td>
                <td>:</td>
                <td><?= strtoupper(esc($letterSettings['academic_year'] ?? $event['academic_year'] ?? '2025/2026')) ?></td>
                
                <td><strong>WAKTU / SESI</strong></td>
                <td>:</td>
                <td><?= esc($sess['subject_time'] ?: '-') ?></td>
            </tr>
            <tr>
                <td><strong>SEMESTER</strong></td>
                <td>:</td>
                <td><?= strtoupper(esc($event['semester'] ?? 'GANJIL')) ?></td>
                
                <td><strong>MATA PELAJARAN</strong></td>
                <td>:</td>
                <td><?= strtoupper(esc($sess['subject_name'])) ?></td>
            </tr>
        </table>
    <?php else: ?>
        <!-- Simplified header for page 2+ of the same session -->
        <div style="display: flex; justify-content: space-between; align-items: center; border-bottom: 1.5px solid #000; padding-bottom: 1.5mm; margin-bottom: 4mm; font-size: 8pt; font-weight: bold; text-transform: uppercase;">
            <span>Daftar Hadir Pengawas — <?= esc($event['name']) ?></span>
            <span><?= esc($sess['subject_name']) ?> — Halaman <?= ($pIdx + 1) ?> dari <?= $totalPages ?></span>
        </div>
    <?php endif; ?>

    <!-- Table Daftar Hadir -->
    <table class="att-table">
        <thead>
            <tr>
                <th width="8%">No</th>
                <th width="28%">Ruang</th>
                <th width="44%">Nama Pengawas</th>
                <th width="20%">Tanda Tangan</th>
            </tr>
        </thead>
        <tbody>
            <?php if (empty($pageProctors)): ?>
                <tr>
                    <td colspan="4" class="center py-5 text-muted" style="font-style: italic;">
                        Belum ada tugas pengawas di sesi ini
                    </td>
                </tr>
            <?php else: ?>
                <?php foreach ($pageProctors as $pws): ?>
                    <tr>
                        <td class="center"><?= $globalIdx ?></td>
                        <td class="center fw-bold text-uppercase"><?= esc($pws['room_name']) ?></td>
                        <td><strong><?= esc($pws['teacher_name']) ?></strong></td>
                        <td class="sig-cell">
                            <?php if ($globalIdx % 2 !== 0): ?>
                                <span class="sig-num"><?= $globalIdx ?>.</span>
                                <span style="padding-left: 5mm;">.......................</span>
                            <?php else: ?>
                                <span class="sig-num" style="left: 14mm;"><?= $globalIdx ?>.</span>
                                <span style="padding-left: 17mm;">.......................</span>
                            <?php endif; ?>
                        </td>
                    </tr>
                <?php $globalIdx++; endforeach; ?>
            <?php endif; ?>
        </tbody>
    </table>

    <?php if ($isLastPage): ?>
        <!-- Footer TTD Kepala Sekolah/Madrasah (Only on the last page of the session) -->
        <div class="footer-signatures">
            <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($sess['exam_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 style="margin-bottom: 2mm;">
                    <?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" style="height: 18mm;"></div>
                
                <div class="sig-name" style="font-weight: bold; text-decoration: underline;"><?= esc($schoolSettings['principal_name'] ?? '') ?></div>
                <div>NIP. <?= esc($schoolSettings['principal_nip'] ?? '-') ?></div>
            </div>
        </div>
    <?php endif; ?>
</div>
<?php endforeach; ?>
<?php endforeach; ?>

</body>
</html>
