<?php
require_once '../config/koneksi.php';

if (!function_exists('e')) {
    function e($value)
    {
        return htmlspecialchars((string)$value, ENT_QUOTES, 'UTF-8');
    }
}

function tgl_indo($tanggal)
{
    if (!$tanggal) return '-';

    $bulan = [
        1 => 'Januari',
        'Februari',
        'Maret',
        'April',
        'Mei',
        'Juni',
        'Juli',
        'Agustus',
        'September',
        'Oktober',
        'November',
        'Desember'
    ];

    $pecah = explode('-', $tanggal);
    if (count($pecah) != 3) return $tanggal;

    return (int)$pecah[2] . ' ' . $bulan[(int)$pecah[1]] . ' ' . $pecah[0];
}

$ruang = $_GET['ruang'] ?? '';
$mapel_id = (int)($_GET['mapel_id'] ?? 0);
$tanggal = $_GET['tanggal'] ?? date('Y-m-d');
$sesi = $_GET['sesi'] ?? '';
$tahun = $_GET['tahun'] ?? '2025/2026';

$profil = $conn->query("SELECT * FROM profil_sekolah LIMIT 1")->fetch_assoc();

$stmtMapel = $conn->prepare("SELECT * FROM mapel WHERE id=? LIMIT 1");
$stmtMapel->bind_param('i', $mapel_id);
$stmtMapel->execute();
$mapel = $stmtMapel->get_result()->fetch_assoc();

$stmt = $conn->prepare("
    SELECT * FROM siswa 
    WHERE ruang_ujian = ? 
    ORDER BY no_ujian ASC, nama ASC
");
$stmt->bind_param('s', $ruang);
$stmt->execute();
$siswaQ = $stmt->get_result();

$logo = $profil['logo'] ?? 'logo.png';
$logoKabupaten = $profil['logo_kabupaten'] ?? '';
$namaKecamatan = $profil['nama_kecamatan'] ?? '';
$namaKabupaten = $profil['nama_kabupaten'] ?? '';
?>

<!doctype html>
<html lang="id">

<head>
    <meta charset="utf-8">
    <title>Cetak Daftar Hadir</title>

    <style>
        @page {
            size: A4 portrait;
            margin: 12mm;
        }

        body {
            font-family: Arial, sans-serif;
            font-size: 12px;
            color: #000;
        }

        .kop {
            display: flex;
            align-items: center;
            border-bottom: 3px double #000;
            padding-bottom: 8px;
            margin-bottom: 12px;
        }

        .kop-logo-wrap {
            display: flex;
            gap: 8px;
            margin-right: 15px;
        }

        .kop-logo-wrap img {
            width: 70px;
            height: 70px;
            object-fit: contain;
        }

        .kop-text {
            text-align: center;
            flex: 1;
        }

        .kop-text h3,
        .kop-text h4,
        .kop-text p {
            margin: 2px 0;
        }

        .judul {
            text-align: center;
            margin: 14px 0;
        }

        .judul h3 {
            margin: 0;
            text-decoration: underline;
        }

        .info {
            width: 100%;
            margin-bottom: 12px;
        }

        .info td {
            padding: 3px;
        }

        table.data {
            width: 100%;
            border-collapse: collapse;
        }

        table.data th,
        table.data td {
            border: 1px solid #000;
            padding: 6px;
            vertical-align: middle;
        }

        table.data th {
            text-align: center;
            background: #eee;
        }

        .ttd {
            margin-top: 25px;
            width: 100%;
        }

        .ttd td {
            width: 50%;
            text-align: center;
            vertical-align: top;
        }

        .space {
            height: 65px;
        }

        .btn-print {
            margin-bottom: 15px;
            padding: 8px 14px;
            background: #198754;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }

        @media print {
            .btn-print {
                display: none;
            }
        }
    </style>
</head>

<body>

    <button onclick="window.print()" class="btn-print">Cetak</button>

    <div class="kop">
        <div class="kop-logo-wrap">
            <?php if (!empty($logoKabupaten)) : ?>
                <img src="../assets/uploads/<?= e($logoKabupaten) ?>" alt="Logo Kabupaten/Kota">
            <?php endif; ?>
            <img src="../assets/uploads/<?= e($logo) ?>" alt="Logo Sekolah">
        </div>
        <div class="kop-text">
            <h4>PEMERINTAH <?= e(!empty($namaKabupaten) ? strtoupper($namaKabupaten) : "KABUPATEN/KOTA") ?></h4>
            <h4>DINAS PENDIDIKAN DAN KEBUDAYAAN</h4>
            <h3><?= e($profil['nama_sekolah'] ?? '') ?></h3>
            <?php if (!empty($namaKecamatan)) : ?><p>Kecamatan <?= e($namaKecamatan) ?></p><?php endif; ?>
            <p><?= e($profil['alamat'] ?? '') ?></p>
        </div>
    </div>

    <div class="judul">
        <h3>DAFTAR HADIR PESERTA UJIAN</h3>
        <p>Tahun Pelajaran <?= e($tahun) ?></p>
    </div>

    <table class="info">
        <tr>
            <td width="140">Mata Pelajaran</td>
            <td width="10">:</td>
            <td><?= e($mapel['nama_mapel'] ?? '-') ?></td>

            <td width="100">Ruang</td>
            <td width="10">:</td>
            <td><?= e($ruang) ?></td>
        </tr>
        <tr>
            <td>Hari / Tanggal</td>
            <td>:</td>
            <td><?= e(tgl_indo($tanggal)) ?></td>

            <td>Sesi / Jam</td>
            <td>:</td>
            <td><?= e($sesi) ?></td>
        </tr>
    </table>

    <table class="data">
        <thead>
            <tr>
                <th width="35">No</th>
                <th width="110">No Ujian</th>
                <th>Nama Peserta</th>
                <th width="70">Kelas</th>
                <th width="130">Tanda Tangan</th>
                <th width="90">Ket.</th>
            </tr>
        </thead>
        <tbody>
            <?php $no = 1;
            while ($s = $siswaQ->fetch_assoc()): ?>
                <tr>
                    <td align="center"><?= $no ?></td>
                    <td><?= e($s['no_ujian'] ?? '') ?></td>
                    <td><?= e($s['nama'] ?? '') ?></td>
                    <td align="center"><?= e($s['kelas'] ?? '') ?></td>
                    <td><?= $no++ ?>.</td>
                    <td></td>
                </tr>
            <?php endwhile; ?>

            <?php if ($no == 1): ?>
                <tr>
                    <td colspan="6" align="center">Tidak ada data siswa pada ruang ini.</td>
                </tr>
            <?php endif; ?>
        </tbody>
    </table>

    <table class="ttd">
        <tr>
            <td>
                Pengawas Ruang<br>
                <div class="space"></div>
                <b>________________________</b>
            </td>
            <td>
                Kepala Sekolah<br>
                <div class="space"></div>
                <b><u><?= e($profil['kepala_sekolah'] ?? '') ?></u></b><br>
                NIP. <?= e($profil['nip_kepsek'] ?? '') ?>
            </td>
        </tr>
    </table>

</body>

</html>