<?php require_once '../auth/cek_login.php';
require_role(['admin', 'guru']);
include 'header.php';

if (!function_exists('pastikan_kolom_siswa')) {
    function pastikan_kolom_siswa($conn, $kolom, $definisi)
    {
        $cek = $conn->query("SHOW COLUMNS FROM siswa LIKE '" . $conn->real_escape_string($kolom) . "'");
        if ($cek && $cek->num_rows == 0) {
            $conn->query("ALTER TABLE siswa ADD `$kolom` $definisi");
        }
    }
}
pastikan_kolom_siswa($conn, 'nama_orang_tua', 'VARCHAR(120) NULL');
$q = trim($_GET['q'] ?? '');
if ($q) {
    $like = '%' . $q . '%';
    // Menyesuaikan pencarian berdasarkan kolom 'ruang_ujian'
    $stmt = $conn->prepare('SELECT * FROM siswa WHERE nama LIKE ? OR kelas LIKE ? OR no_ujian LIKE ? OR tempat_lahir LIKE ? OR ruang_ujian LIKE ? OR nama_orang_tua LIKE ? ORDER BY kelas,nama');
    $stmt->bind_param('ssssss', $like, $like, $like, $like, $like, $like);
    $stmt->execute();
    $data = $stmt->get_result();
} else {
    $data = $conn->query('SELECT * FROM siswa ORDER BY kelas,nama');
}
?>
<div class="d-flex justify-content-between align-items-center mb-3">
    <h3 class="fw-bold">Data Siswa</h3><a href="tambah_siswa.php" class="btn btn-primary">+ Tambah Siswa</a>
</div>
<form class="row mb-3">
    <div class="col-md-6"><input name="q" value="<?= e($q) ?>" class="form-control" placeholder="Cari nama, kelas, ruang ujian, tempat lahir, atau nomor ujian"></div>
    <div class="col-auto"><button class="btn btn-outline-primary">Cari</button></div>
    <div class="col-auto"><a class="btn btn-success" target="_blank" href="../cetak/kartu_massal.php">Cetak Kartu</a></div>
    <div class="col-auto"><a class="btn btn-primary" target="_blank" href="../cetak/kartu_massal_potret.php">Cetak ID_Card</a></div>
</form>
<div class="card">
    <div class="table-responsive">
        <table class="table table-hover mb-0">
            <thead>
                <tr>
                    <th>No</th>
                    <th>Foto</th>
                    <th>No Ujian</th>
                    <th>Nama</th><th>Orang Tua Kandung</th>
                    <th>Kelas</th>
                    <th>Ruang Ujian</th> <!-- Judul Kolom Baru -->
                    <th>Tempat, Tanggal Lahir</th>
                    <th>Aksi</th>
                </tr>
            </thead>
            <tbody><?php $no = 1;
                    while ($r = $data->fetch_assoc()): ?><tr>
                        <td><?= $no++ ?></td>
                        <td>
                            <!-- Foto diperkecil menggunakan style inline & object-fit agar proporsional -->
                            <img class="thumb img-thumbnail" src="../assets/uploads/<?= e($r['foto'] ?: 'default.png') ?>" style="width: 50px; height: 50px; object-fit: cover;">
                        </td>
                        <td><?= e($r['no_ujian']) ?></td>
                        <td><?= e($r['nama']) ?></td><td><?= e($r['nama_orang_tua'] ?? '-') ?></td>
                        <td><?= e($r['kelas']) ?></td>
                        <td><?= e($r['ruang_ujian'] ?: '-') ?></td> <!-- Menampilkan Ruang Ujian sesuai kode tambah siswa -->
                        <td><?= e(trim(($r['tempat_lahir'] ?? '') . (!empty($r['tanggal_lahir']) ? ', ' . date('d-m-Y', strtotime($r['tanggal_lahir'])) : '')) ?: '-') ?></td>
                        <td><a class="btn btn-sm btn-info" target="_blank" href="../cetak/kartu.php?id=<?= $r['id'] ?>">Kartu</a> <a class="btn btn-sm btn-primary" target="_blank" href="../cetak/kartu_potret.php?id=<?= $r['id'] ?>">ID_Card</a> <a class="btn btn-sm btn-warning" href="edit_siswa.php?id=<?= $r['id'] ?>">Edit</a> <a onclick="return confirm('Hapus data ini?')" class="btn btn-sm btn-danger" href="hapus_siswa.php?id=<?= $r['id'] ?>">Hapus</a></td>
                    </tr><?php endwhile; ?></tbody>
        </table>
    </div>
</div>
<?php include 'footer.php'; ?>