<?php
include 'header.php';

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

$ruangQ = $conn->query("
    SELECT DISTINCT ruang_ujian 
    FROM siswa 
    WHERE ruang_ujian IS NOT NULL AND ruang_ujian != '' 
    ORDER BY ruang_ujian ASC
");

$mapelQ = $conn->query("
    SELECT * FROM mapel 
    ORDER BY urutan ASC, nama_mapel ASC
");
?>

<h3 class="fw-bold mb-3">Daftar Hadir Peserta Ujian</h3>

<form method="get" class="card p-3 mb-3">
    <div class="row g-2 align-items-end">

        <div class="col-md-3">
            <label>Ruang Ujian</label>
            <select name="ruang" class="form-select" required>
                <option value="">Pilih Ruang</option>
                <?php while ($r = $ruangQ->fetch_assoc()): ?>
                    <option value="<?= e($r['ruang_ujian']) ?>" <?= $ruang == $r['ruang_ujian'] ? 'selected' : '' ?>>
                        <?= e($r['ruang_ujian']) ?>
                    </option>
                <?php endwhile; ?>
            </select>
        </div>

        <div class="col-md-3">
            <label>Mata Pelajaran</label>
            <select name="mapel_id" class="form-select" required>
                <option value="">Pilih Mapel</option>
                <?php while ($m = $mapelQ->fetch_assoc()): ?>
                    <option value="<?= e($m['id']) ?>" <?= $mapel_id == $m['id'] ? 'selected' : '' ?>>
                        <?= e($m['nama_mapel']) ?>
                    </option>
                <?php endwhile; ?>
            </select>
        </div>

        <div class="col-md-2">
            <label>Tanggal</label>
            <input type="date" name="tanggal" class="form-control" value="<?= e($tanggal) ?>">
        </div>

        <div class="col-md-2">
            <label>Sesi / Jam</label>
            <input name="sesi" class="form-control" value="<?= e($sesi) ?>" placeholder="07.30 - 09.30">
        </div>

        <div class="col-md-2">
            <label>Tahun</label>
            <input name="tahun" class="form-control" value="<?= e($tahun) ?>">
        </div>

        <div class="col-md-12">
            <button class="btn btn-primary">
                Tampilkan
            </button>

            <?php if ($ruang !== '' && $mapel_id > 0): ?>
                <a
                    href="../cetak/daftar_hadir.php?ruang=<?= urlencode($ruang) ?>&mapel_id=<?= $mapel_id ?>&tanggal=<?= urlencode($tanggal) ?>&sesi=<?= urlencode($sesi) ?>&tahun=<?= urlencode($tahun) ?>"
                    target="_blank"
                    class="btn btn-success">
                    Cetak Daftar Hadir
                </a>
            <?php endif; ?>
        </div>

    </div>
</form>

<?php
if ($ruang !== '' && $mapel_id > 0):

    $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();
?>

    <div class="card p-3 table-responsive">
        <h5 class="fw-bold mb-2">Preview Daftar Hadir</h5>

        <table class="table table-bordered mb-3">
            <tr>
                <th width="180">Ruang Ujian</th>
                <td><?= e($ruang) ?></td>
            </tr>
            <tr>
                <th>Mata Pelajaran</th>
                <td><?= e($mapel['nama_mapel'] ?? '-') ?></td>
            </tr>
            <tr>
                <th>Tanggal</th>
                <td><?= e($tanggal) ?></td>
            </tr>
            <tr>
                <th>Sesi / Jam</th>
                <td><?= e($sesi) ?></td>
            </tr>
        </table>

        <table class="table table-bordered table-striped align-middle">
            <thead>
                <tr>
                    <th width="50">No</th>
                    <th>No Ujian</th>
                    <th>Nama Peserta</th>
                    <th>Kelas</th>
                    <th width="180">Tanda Tangan</th>
                    <th width="120">Keterangan</th>
                </tr>
            </thead>
            <tbody>
                <?php $no = 1;
                while ($s = $siswaQ->fetch_assoc()): ?>
                    <tr>
                        <td><?= $no ?></td>
                        <td><?= e($s['no_ujian'] ?? '') ?></td>
                        <td><?= e($s['nama'] ?? '') ?></td>
                        <td><?= e($s['kelas'] ?? '') ?></td>
                        <td><?= $no++ ?>.</td>
                        <td></td>
                    </tr>
                <?php endwhile; ?>
            </tbody>
        </table>
    </div>

<?php endif; ?>

<?php include 'footer.php'; ?>