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

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

// 1. Ambil info mapel, kelas, dan 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 daftar siswa
$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>Lingkup Materi - <?= htmlspecialchars($info['nama_mapel'] ?? '') ?></title>
    <style>
        body { font-family: 'Times New Roman', serif; font-size: 11px; }
        table { width: 100%; border-collapse: collapse; margin-top: 10px; }
        table, th, td { border: 1px solid #000; padding: 3px; text-align: center; }
        .nama { text-align: left; padding-left: 5px; width: 150px; }
        @media print { .no-print { display: none; } @page { size: landscape; margin: 1cm; } }
    </style>
</head>
<body>
    <div style="text-align:center;">
        <h3>DAFTAR LINGKUP MATERI - <?= strtoupper(htmlspecialchars($info['nama_mapel'] ?? '-')) ?></h3>
        <p style="margin: 2px 0;">Kelas: <?= htmlspecialchars($info['kelas'] ?? '-') ?></p>
        <p style="margin: 2px 0; font-weight:bold;">Semester: <?= $semester_teks ?> | Tahun Pelajaran: <?= $tahun_teks ?></p>
    </div>

    <table>
        <thead>
            <tr>
                <th rowspan="2">No</th>
                <th rowspan="2" class="nama">Nama Siswa</th>
                <?php for($i=1; $i<=6; $i++): ?>
                    <th colspan="5">Lingkup Materi <?= $i ?></th>
                <?php endfor; ?>
            </tr>
            <tr>
                <?php for($i=1; $i<=30; $i++): ?>
                    <th>TP<?= ($i-1)%5 + 1 ?></th>
                <?php endfor; ?>
            </tr>
        </thead>
        <tbody>
            <?php $no = 1; while($s = mysqli_fetch_array($q_s)): ?>
                <tr>
                    <td><?= $no++ ?></td>
                    <td class="nama"><?= htmlspecialchars($s['nama']) ?></td>
                    <?php for($i=1; $i<=30; $i++): ?>
                        <td>&nbsp;</td>
                    <?php endfor; ?>
                </tr>
            <?php endwhile; ?>
        </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 class="no-print" style="margin-top:20px;">
        <button onclick="window.print()">Cetak Halaman Ini</button>
        <button onclick="window.location.href='input_nilai.php?id_tugas=<?= urlencode($id_t) ?>'">Tutup</button>
    </div>
</body>
</html>