<?php
$namaSekolah = !empty($p['nama_sekolah']) ? $p['nama_sekolah'] : 'SDN 3 Mekar Sari';
$alamatSekolah = !empty($p['alamat']) ? $p['alamat'] : 'Jln Suropati Lekong Pulut Mekar Sari';

$tahun = !empty($s['tahun_pelajaran'])
    ? $s['tahun_pelajaran']
    : (!empty($p['tahun_pelajaran']) ? $p['tahun_pelajaran'] : '2025/2026');

$logo = !empty($p['logo']) ? $p['logo'] : 'logo.png';
$logoKabupaten = !empty($p['logo_kabupaten']) ? $p['logo_kabupaten'] : '';
$namaKecamatan = !empty($p['nama_kecamatan']) ? $p['nama_kecamatan'] : '';
$namaKabupaten = !empty($p['nama_kabupaten']) ? $p['nama_kabupaten'] : '';
$foto = !empty($s['foto']) ? $s['foto'] : 'default.png';

$noPeserta = !empty($s['no_ujian']) ? $s['no_ujian'] : '-';

$tempatLahir = !empty($s['tempat_lahir']) ? $s['tempat_lahir'] : '';
$tglLahir = !empty($s['tanggal_lahir']) ? $s['tanggal_lahir'] : '';

$ttl = trim($tempatLahir . ($tglLahir ? ', ' . date('d-m-Y', strtotime($tglLahir)) : ''));
if ($ttl === '') {
    $ttl = '-';
}

$kepsek = !empty($p['kepala_sekolah']) ? $p['kepala_sekolah'] : (!empty($p['nama_kepala_sekolah']) ? $p['nama_kepala_sekolah'] : (!empty($p['nama_kepsek']) ? $p['nama_kepsek'] : (!empty($p['kepsek']) ? $p['kepsek'] : '................................')));

$nipKepsek = !empty($p['nip_kepsek']) ? $p['nip_kepsek'] : (!empty($p['nip_kepala_sekolah']) ? $p['nip_kepala_sekolah'] : (!empty($p['nip']) ? $p['nip'] : '................................'));

$barcodeSeed = preg_replace('/[^0-9]/', '', $noPeserta);
if ($barcodeSeed === '') {
    $barcodeSeed = '25030001';
}

$bars = str_split(str_pad(substr($barcodeSeed, 0, 12), 12, '1234567890'));
?>

<div class="exam-card-v2">
    <div class="corner corner-left"></div>
    <div class="corner corner-right"></div>
    <div class="dot dot-left"></div>
    <div class="dot dot-right"></div>

    <div class="left-panel">
        <div class="logo-row-v2">
            <?php if ($logoKabupaten !== '') : ?>
                <div class="logo-box-v2"><img src="../assets/uploads/<?= e($logoKabupaten) ?>" class="school-logo-v2" alt="Logo Kabupaten/Kota"></div>
            <?php endif; ?>
            <div class="logo-box-v2"><img src="../assets/uploads/<?= e($logo) ?>" class="school-logo-v2" alt="Logo Sekolah"></div>
        </div>

        <div class="photo-box">
            <img src="../assets/uploads/<?= e($foto) ?>" class="student-photo-v2" alt="Foto Siswa">
            <div class="photo-label">PESERTA</div>

            <div class="number-box">
                <small>No. Peserta</small>
                <strong><?= e($noPeserta) ?></strong>
            </div>
        </div>

        <div class="barcode" aria-label="Barcode">
            <?php foreach ($bars as $b): ?>
                <span style="width:<?= 2 + ((int)$b % 4) ?>px"></span>
            <?php endforeach; ?>

            <?php foreach (array_reverse($bars) as $b): ?>
                <span style="width:<?= 1 + ((int)$b % 5) ?>px"></span>
            <?php endforeach; ?>
        </div>
    </div>

    <div class="main-panel">
        <div class="card-header-v2">
            <h2>KARTU UJIAN</h2>
            <h1><?= e($namaSekolah) ?></h1>
            <?php if ($namaKecamatan || $namaKabupaten) : ?>
                <p><?= e(trim(($namaKecamatan ? "Kec. " . $namaKecamatan : "") . ($namaKabupaten ? " - Kab/Kota " . $namaKabupaten : ""), " -")) ?></p>
            <?php endif; ?>
            <p><?= e($alamatSekolah) ?></p>
            <div class="divider"><span></span></div>
            <div class="year-ribbon">TAHUN PELAJARAN <?= e($tahun) ?></div>
        </div>

        <div class="student-info-v2">
            <div class="row-info">
                <label>Nama Lengkap</label>
                <span>:</span>
                <b><?= e($s['nama'] ?? '-') ?></b>
            </div>

            <div class="row-info">
                <label>NISN</label>
                <span>:</span>
                <b><?= e($s['nisn'] ?? '-') ?></b>
            </div>

            <div class="row-info">
                <label>Kelas</label>
                <span>:</span>
                <b><?= e($s['kelas'] ?? '-') ?></b>
            </div>

            <div class="row-info">
                <label>Tempat, Tanggal Lahir</label>
                <span>:</span>
                <b><?= e($ttl) ?></b>
            </div>
        </div>

        <div class="bottom-area">
            <div class="note-box">
                <div>CATATAN PENGAWAS</div>
            </div>

            <div class="signature-box">
                <p>Mengetahui,<br>Kepala Sekolah</p>
                <div class="signature-space"></div>
                <strong><?= e($kepsek) ?></strong>
                <small>NIP. <?= e($nipKepsek) ?></small>
            </div>

            <div class="signature-box">
                <p>Panitia Ujian Sekolah</p>
                <div class="signature-space"></div>
                <strong>................................</strong>
                <small>NIP. ................................</small>
            </div>
        </div>
    </div>

    <div class="warning-strip">
        ⚠ KARTU INI WAJIB DIBAWA SETIAP MENGIKUTI UJIAN SEKOLAH
    </div>
</div>