<?php require_once '../auth/cek_login.php';
require_role(['admin','guru']);
include 'header.php';
$kelas = $_GET['kelas'] ?? '';
$tahun = $_GET['tahun'] ?? ($profil_header['tahun_pelajaran'] ?? '2025/2026');
$kelasQ = $conn->query('SELECT DISTINCT kelas FROM siswa ORDER BY kelas ASC');
$mapels = [];
$qm = $conn->query('SELECT * FROM mapel ORDER BY urutan ASC, nama_mapel ASC');
while($m=$qm->fetch_assoc()) $mapels[]=$m;
$rows=[];
if($kelas!==''){
    $stmt=$conn->prepare('SELECT * FROM siswa WHERE kelas=? ORDER BY nama ASC');
    $stmt->bind_param('s',$kelas); $stmt->execute(); $siswa=$stmt->get_result();
    while($s=$siswa->fetch_assoc()){
        $nilai=[]; $jumlah=0; $isi=0;
        foreach($mapels as $m){
            $st=$conn->prepare('SELECT nilai FROM nilai WHERE siswa_id=? AND mapel_id=? AND tahun_pelajaran=? LIMIT 1');
            $st->bind_param('iis',$s['id'],$m['id'],$tahun); $st->execute();
            $n=$st->get_result()->fetch_assoc();
            $val=$n ? (float)$n['nilai'] : null;
            $nilai[$m['id']]=$val;
            if($val!==null){$jumlah += $val; $isi++;}
        }
        $rata=$isi>0 ? $jumlah/$isi : 0;
        $rows[]=['siswa'=>$s,'nilai'=>$nilai,'jumlah'=>$jumlah,'rata'=>$rata];
    }
    usort($rows, fn($a,$b)=>$b['rata']<=>$a['rata']);
    $rank=1; foreach($rows as &$r){$r['rank']=$rank++;} unset($r);
    usort($rows, fn($a,$b)=>strcmp($a['siswa']['nama'],$b['siswa']['nama']));
}
?>
<h3 class="fw-bold mb-3">Rekap Nilai Ujian</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" required><option value="">Pilih 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 name="tahun" class="form-control" value="<?= e($tahun) ?>"></div>
        <div class="col-md-2"><button class="btn btn-primary w-100">Tampilkan</button></div>
        <?php if($kelas!==''): ?><div class="col-md-2"><a target="_blank" class="btn btn-success w-100" href="../cetak/rekap_nilai.php?kelas=<?= urlencode($kelas) ?>&tahun=<?= urlencode($tahun) ?>">Cetak</a></div><?php endif; ?>
    </div>
</form>
<?php if($kelas!=='' && count($mapels)==0): ?><div class="alert alert-warning">Data mata pelajaran masih kosong. Tambahkan dulu di menu Mata Pelajaran.</div><?php endif; ?>
<?php if($kelas!==''): ?>
<div class="card p-3 table-responsive">
<table class="table table-bordered table-striped align-middle">
<thead><tr><th>No</th><th>No Ujian</th><th>Nama</th><?php foreach($mapels as $m): ?><th><?= e($m['nama_mapel']) ?></th><?php endforeach; ?><th>Jumlah</th><th>Rata-rata</th><th>Ranking</th></tr></thead>
<tbody><?php $no=1; foreach($rows as $r): ?><tr><td><?= $no++ ?></td><td><?= e($r['siswa']['no_ujian']) ?></td><td><?= e($r['siswa']['nama']) ?></td><?php foreach($mapels as $m): ?><td><?= $r['nilai'][$m['id']]===null ? '-' : e(number_format($r['nilai'][$m['id']],2,',','.')) ?></td><?php endforeach; ?><td><?= e(number_format($r['jumlah'],2,',','.')) ?></td><td><?= e(number_format($r['rata'],2,',','.')) ?></td><td><?= e($r['rank']) ?></td></tr><?php endforeach; ?></tbody>
</table>
</div>
<?php endif; ?>
<?php include 'footer.php'; ?>
