<!DOCTYPE html>
<html lang="id">
<head>
    <?php helper('logo'); ?>
    <link rel="icon" type="image/png" href="<?= getLogo('sekolah') ?>">
<meta charset="UTF-8">
<title>Cetak Jadwal Asesmen - <?= 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 */
@page {
    size: A4 portrait;
    margin: 0;
}

.a4-sheet {
    width: 210mm;
    height: 297mm;
    background: white;
    position: relative;
    margin: 0 auto 20px;
    padding: 10mm 15mm;
    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: 12pt;
    font-weight: bold;
    text-transform: uppercase;
    margin-top: 2mm;
    margin-bottom: 4mm;
    text-decoration: underline;
    letter-spacing: 0.5px;
}

.event-details-bar {
    background: #f8fafc;
    border: 1px solid #e2e8f0;
    padding: 2.5mm 4mm;
    border-radius: 4px;
    font-size: 10pt;
    margin-bottom: 5mm;
    display: flex;
    justify-content: space-between;
    text-transform: uppercase;
}

/* Table */
.schedule-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 11pt;
    margin-bottom: 8mm;
}
.schedule-table th, .schedule-table td {
    border: 1px solid #000;
    padding: 3mm 4mm;
    vertical-align: middle;
}
.schedule-table th {
    text-align: center;
    font-weight: bold;
    background: #f1f5f9;
    text-transform: uppercase;
}
.schedule-table td.center {
    text-align: center;
}

/* Signature */
.footer-area {
    display: flex;
    justify-content: flex-end;
    margin-top: 10mm;
    font-size: 11pt;
}
.sig-box {
    text-align: center;
    width: 65mm;
    position: relative;
}
.sig-space {
    height: 15mm;
    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: avoid;
    }
    * {
        -webkit-print-color-adjust: exact;
        print-color-adjust: exact;
    }
}
</style>
</head>
<body>

<div class="preview-header">
    <div>
        <h2>📅 Cetak Jadwal Asesmen</h2>
        <p>Jadwal mata pelajaran ujian untuk siswa &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
$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'
];
?>
<div class="a4-sheet" style="margin-bottom: 20px;">
    <!-- Official Kop -->
    <?= view('letter/_kop', ['letterSettings' => $letterSettings, 'schoolSettings' => $schoolSettings]) ?>

    <div class="page-title">JADWAL ASESMEN</div>

    <div class="event-details-bar">
        <span><strong>ASESMEN:</strong> <?= esc($event['name']) ?></span>
        <span><strong>TA.</strong> <?= strtoupper(esc($activePeriod['academic_year_name'] ?? '2025/2026')) ?> (SEMESTER <?= strtoupper(esc($event['semester'] ?? 'GANJIL')) ?>)</span>
    </div>

    <!-- Table -->
    <table class="schedule-table">
        <thead>
            <tr>
                <th width="8%">No</th>
                <th width="32%">Hari & Tanggal</th>
                <th width="25%">Waktu / Jam</th>
                <th width="35%">Mata Pelajaran</th>
            </tr>
        </thead>
        <tbody>
            <?php if (empty($schedules)): ?>
                <tr>
                    <td colspan="4" class="text-center py-4 text-muted" style="font-style: italic;">
                        Belum ada data jadwal asesmen
                    </td>
                </tr>
            <?php else: ?>
                <?php $no = 1; foreach ($schedules as $sch): 
                    $subs = $sch['subjects'] ?: [];
                    if (empty($subs)) $subs = [['name' => '-', 'time' => '-']];
                    $rowCount = count($subs);
                ?>
                    <?php foreach ($subs as $idx => $sub): 
                        if (is_array($sub)) {
                            $subName = $sub['name'] ?? '';
                            $subTime = $sub['time'] ?? '';
                        } else {
                            $subName = $sub;
                            $subTime = '';
                        }
                        
                        $schTime = strtotime($sch['exam_date']);
                        $schDay = date('d', $schTime);
                        $schMonth = (int)date('m', $schTime);
                        $schYear = date('Y', $schTime);
                        $schIndoDate = $schDay . ' ' . $months[$schMonth] . ' ' . $schYear;
                    ?>
                    <tr>
                        <?php if ($idx === 0): ?>
                            <td class="center" rowspan="<?= $rowCount ?>"><?= $no++ ?></td>
                            <td rowspan="<?= $rowCount ?>">
                                <strong><?= esc($sch['exam_day']) ?></strong>
                                <div style="font-size: 9.5pt; color: #475569; margin-top: 0.5mm;"><?= esc($schIndoDate) ?></div>
                            </td>
                        <?php endif; ?>
                        <td class="center font-monospace" style="font-size: 10pt;"><?= esc($subTime ?: '-') ?></td>
                        <td><strong><?= esc($subName) ?></strong></td>
                    </tr>
                    <?php endforeach; ?>
                <?php endforeach; ?>
            <?php endif; ?>
        </tbody>
    </table>

    <!-- Footer Signature -->
    <div class="footer-area">
        <div class="sig-box">
            <?php
            $cityName = ucwords(strtolower($schoolSettings['city'] ?? 'Wonogiri'));
            $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 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">
                <?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: -6mm; 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>

</body>
</html>
