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

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

$profil = $conn->query('SELECT * FROM profil_sekolah LIMIT 1')->fetch_assoc();
$tahun = $_GET['tahun'] ?? ($profil['tahun_pelajaran'] ?? '2025/2026');
$kelas = $_GET['kelas'] ?? '';

$kelasQ = $conn->query('SELECT DISTINCT kelas FROM siswa ORDER BY kelas ASC');

$where = '';
$params = [];
$types = '';
if ($kelas !== '') {
    $where = 'WHERE kelas = ?';
    $params[] = $kelas;
    $types .= 's';
}

$stmt = $conn->prepare("SELECT * FROM siswa $where ORDER BY kelas ASC, nama ASC");
if ($params) { $stmt->bind_param($types, ...$params); }
$stmt->execute();
$siswa = $stmt->get_result();

include 'header.php';
?>
<h3 class="fw-bold mb-3">Cetak Nilai Siswa</h3>

<form method="get" class="card p-3 mb-3">
    <div class="row g-2 align-items-end">
        <div class="col-md-4">
            <label>Kelas</label>
            <select name="kelas" class="form-select">
                <option value="">Semua Kelas</option>
                <?php while ($k = $kelasQ->fetch_assoc()): ?>
                    <option value="<?= e($k['kelas']) ?>" <?= $kelas == $k['kelas'] ? 'selected' : '' ?>><?= e($k['kelas']) ?></option>
                <?php endwhile; ?>
            </select>
        </div>
        <div class="col-md-4">
            <label>Tahun Pelajaran</label>
            <input type="text" name="tahun" class="form-control" value="<?= e($tahun) ?>" required>
        </div>
        <div class="col-md-4">
            <button class="btn btn-primary w-100"><i class="fa-solid fa-search"></i> Tampilkan</button>
        </div>
    </div>
</form>

<div class="card p-3">
    <div class="d-flex flex-wrap gap-2 justify-content-between align-items-center mb-3">
        <div>
            <b>Daftar Siswa</b><br>
            <small class="text-muted">Cetak nilai per siswa atau cetak semua sesuai filter kelas.</small>
        </div>
        <a target="_blank" class="btn btn-success" href="../cetak/nilai.php?kelas=<?= urlencode($kelas) ?>&tahun=<?= urlencode($tahun) ?>">
            <i class="fa-solid fa-print"></i> Cetak Semua
        </a>
    </div>

    <div class="table-responsive">
        <table class="table table-bordered table-striped align-middle">
            <thead class="table-dark">
                <tr>
                    <th width="50">No</th>
                    <th>Nama</th>
                    <th>NISN</th>
                    <th>No Ujian</th>
                    <th>Kelas</th>
                    <th width="150">Aksi</th>
                </tr>
            </thead>
            <tbody>
                <?php $no = 1; while ($row = $siswa->fetch_assoc()): ?>
                    <tr>
                        <td><?= $no++ ?></td>
                        <td><?= e($row['nama'] ?? '-') ?></td>
                        <td><?= e($row['nisn'] ?? '-') ?></td>
                        <td><?= e($row['no_ujian'] ?? '-') ?></td>
                        <td><?= e($row['kelas'] ?? '-') ?></td>
                        <td>
                            <a target="_blank" class="btn btn-sm btn-primary" href="../cetak/nilai.php?id=<?= (int)$row['id'] ?>&tahun=<?= urlencode($tahun) ?>">
                                <i class="fa-solid fa-print"></i> Cetak
                            </a>
                        </td>
                    </tr>
                <?php endwhile; ?>
            </tbody>
        </table>
    </div>
</div>
<?php include 'footer.php'; ?>
