<?php
require_once '../config/koneksi.php';
if (!function_exists('e')) { function e($value) { return htmlspecialchars((string)$value, ENT_QUOTES, 'UTF-8'); } }
$kelas = $_GET['kelas'] ?? '';
$tahun = $_GET['tahun'] ?? '';
$p = $conn->query('SELECT * FROM profil_sekolah LIMIT 1')->fetch_assoc();
$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++;}
        }
        $rows[]=['siswa'=>$s,'nilai'=>$nilai,'jumlah'=>$jumlah,'rata'=>$isi?($jumlah/$isi):0];
    }
    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']));
}
?>
<!doctype html><html lang="id"><head><meta charset="utf-8"><title>Cetak Rekap Nilai</title><style>
@page{size:A4 landscape;margin:12mm}body{font-family:Arial,sans-serif;color:#111}.toolbar{margin-bottom:15px}.btn{padding:8px 14px;border:0;border-radius:6px;background:#16a34a;color:white;font-weight:bold;cursor:pointer}.kop{text-align:center;border-bottom:3px double #111;padding-bottom:8px;margin-bottom:12px}.kop h2,.kop h3{margin:2px 0}.meta{margin:10px 0;font-size:13px}table{border-collapse:collapse;width:100%;font-size:11px}th,td{border:1px solid #333;padding:5px;text-align:center}td.nama{text-align:left}th{background:#e5e7eb}.ttd{width:280px;float:right;text-align:center;margin-top:25px;font-size:12px}.space{height:60px}@media print{.toolbar{display:none}body{-webkit-print-color-adjust:exact;print-color-adjust:exact}}
</style></head><body><div class="toolbar"><button class="btn" onclick="window.print()">Cetak</button></div>
<div class="kop"><h2>REKAP NILAI HASIL UJIAN</h2><h3><?= e($p['nama_sekolah'] ?? '') ?></h3><div><?= e($p['alamat'] ?? '') ?></div></div>
<div class="meta"><b>Kelas:</b> <?= e($kelas) ?> &nbsp;&nbsp; <b>Tahun Pelajaran:</b> <?= e($tahun) ?></div>
<table><thead><tr><th>No</th><th>No Ujian</th><th>Nama Siswa</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 class="nama"><?= 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 class="ttd"><p>Kepala Sekolah</p><div class="space"></div><b><u><?= e($p['kepala_sekolah'] ?? '') ?></u></b><br>NIP. <?= e($p['nip_kepsek'] ?? '') ?></div>
</body></html>
