<?php
session_start();
require_once '../koneksi.php';
require_once '../auth.php';
cekAdminAtauGuru();

$id_t = mysqli_real_escape_string($koneksi, $_GET['id_tugas'] ?? '');
$mode = $_GET['mode'] ?? '';

// 1. Ambil info mapel, kelas, guru, dan NIP guru
$info = mysqli_fetch_assoc(mysqli_query($koneksi, "SELECT m.nama_mapel, r.kelas, g.nama as nama_guru, g.nip as nip_guru FROM tugas_guru t JOIN master_mapel m ON t.id_mapel=m.id_mapel JOIN rombel r ON t.id_rombel=r.id_rombel JOIN master_guru g ON t.id_guru=g.id_guru WHERE t.id_tugas='$id_t'"));

// 2. Ambil data Kepala Sekolah
$q_kepsek = mysqli_query($koneksi, "SELECT kepala_sekolah, nip_kepala FROM profil_semester LIMIT 1");
$kepsek = mysqli_fetch_assoc($q_kepsek);

// 3. Ambil data Semester dan Tahun Pelajaran (Otomatis ambil yang Aktif)
$q_set = mysqli_query($koneksi, "
    SELECT p.id_semester, t.tahun_pelajaran 
    FROM profil_semester p
    JOIN tahun_pelajaran t ON t.status = 'Aktif'
    LIMIT 1
");
$set = mysqli_fetch_assoc($q_set);

$semester_teks = '-';
$tahun_teks = '-';

if ($set) {
    $semester_teks = ($set['id_semester'] == 1) ? 'Ganjil' : 'Genap';
    $tahun_teks = $set['tahun_pelajaran'];
}

// 4. Ambil data nilai
$daftar_tp = mysqli_fetch_all(mysqli_query($koneksi, "SELECT id, tp_kode FROM tujuan_pembelajaran WHERE id_tugas='$id_t'"), MYSQLI_ASSOC);
$n_data = []; $r_data = [];
if($mode !== 'blank') {
    $q_n = mysqli_query($koneksi, "SELECT id_siswa, id_tp, nilai, nilai_rapor FROM nilai_siswa WHERE id_tugas='$id_t'");
    while($r = mysqli_fetch_assoc($q_n)) {
        $key = ($r['id_tp'] == null || $r['id_tp'] == 0) ? 'sas' : $r['id_tp'];
        $n_data[$r['id_siswa']][$key] = $r['nilai'];
        $r_data[$r['id_siswa']] = $r['nilai_rapor'];
    }
}

$q_s = mysqli_query($koneksi, "SELECT s.id_siswa, s.nama FROM master_siswa s JOIN anggota_rombel ar ON s.id_siswa=ar.id_siswa WHERE ar.id_rombel=(SELECT id_rombel FROM tugas_guru WHERE id_tugas='$id_t') ORDER BY s.nama ASC");
?>

<!DOCTYPE html>
<html>
<head>
    <title>Cetak Nilai - <?= htmlspecialchars($info['nama_mapel'] ?? 'Nilai') ?></title>
    <style>
        body{font-family: 'Times New Roman', serif; font-size:12px;}
        table{width:100%; border-collapse:collapse; margin-top:10px;}
        table,th,td{border:1px solid #000; padding:5px; text-align:center;}
        .nama{text-align:left; padding-left:10px;}
        @media print{.no-print{display:none;} @page { margin: 1cm; }}
    </style>
</head>
<body>
    <div style="text-align:center;">
        <h2 style="margin-bottom:5px;">DAFTAR NILAI <?= strtoupper(htmlspecialchars($info['nama_mapel'] ?? '-')) ?></h2>
        <p style="margin: 2px 0;">Kelas: <?= htmlspecialchars($info['kelas'] ?? '-') ?> | Guru: <?= htmlspecialchars($info['nama_guru'] ?? '-') ?></p>
        <p style="margin: 2px 0; font-weight:bold;">Semester: <?= $semester_teks ?> | Tahun Pelajaran: <?= htmlspecialchars($tahun_teks) ?></p>
    </div>

    <table>
        <thead>
            <tr>
                <th>No</th>
                <th class='nama'>Nama Siswa</th>
                <?php foreach($daftar_tp as $tp) echo "<th>{$tp['tp_kode']}</th>"; ?>
                <th>SAS</th>
                <th>Rapor</th>
            </tr>
        </thead>
        <tbody>
            <?php $n=1; while($s=mysqli_fetch_array($q_s)){ echo "<tr><td>$n</td><td class='nama'>".htmlspecialchars($s['nama'])."</td>";
            foreach($daftar_tp as $tp) echo "<td>".($mode=='blank' ? '&nbsp;' : ($n_data[$s['id_siswa']][$tp['id']] ?? '-'))."</td>";
            echo "<td>".($mode=='blank' ? '&nbsp;' : ($n_data[$s['id_siswa']]['sas'] ?? '-'))."</td>";
            echo "<td>".($mode=='blank' ? '&nbsp;' : ($r_data[$s['id_siswa']] ?? '-'))."</td></tr>"; $n++; } ?>
        </tbody>
    </table>

    <div style="margin-top: 30px; width: 100%; display: flex; justify-content: space-between;">
        <div style="width: 350px; text-align: center;">
            <p>Guru Mata Pelajaran</p>
            <br><br><br><br>
            <p><strong><?= htmlspecialchars($info['nama_guru'] ?? '-') ?></strong></p>
            <p>NIP. <?= htmlspecialchars($info['nip_guru'] ?? '-') ?></p>
        </div>
        <div style="width: 350px; text-align: center;">
            <p>Mengetahui,</p>
            <p>Kepala Sekolah</p>
            <br><br><br><br>
            <p><strong><?= htmlspecialchars($kepsek['kepala_sekolah'] ?? '-') ?></strong></p>
            <p>NIP. <?= htmlspecialchars($kepsek['nip_kepala'] ?? '-') ?></p>
        </div>
    </div>

    <div style="margin-top: 20px;" class="no-print">
        <button onclick="window.print()">Cetak</button>
        <button onclick="window.location.href='cetak_nilai.php?id_tugas=<?= urlencode($id_t) ?>&mode=blank'">Cetak Blanko Kosong</button>
        <button onclick="window.close()">Tutup</button>
    </div>

    <script>if('<?=$mode?>'=='blank') window.onload=function(){window.print();}</script>
</body>
</html>