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

// 1. Ambil ID Tugas dari URL
$id_t = isset($_GET['id_tugas']) ? mysqli_real_escape_string($koneksi, $_GET['id_tugas']) : '';

if (empty($id_t)) {
    die("ID Tugas tidak valid.");
}

// 2. Query Data Utama (Mapel, Kelas, Guru)
$query_info = 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'");
$info = mysqli_fetch_assoc($query_info);

// 3. Query Data Kepala Sekolah
$q_kepsek = mysqli_query($koneksi, "SELECT kepala_sekolah, nip_kepala FROM profil_semester LIMIT 1");
$kepsek = mysqli_fetch_assoc($q_kepsek);

// 4. Query 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");

$bulan = ['Juli', 'Agustus', 'September', 'Oktober', 'November', 'Desember'];
?>

<!DOCTYPE html>
<html>
<head>
    <title>Absensi - <?= htmlspecialchars($info['nama_mapel'] ?? 'Mapel') ?></title>
    <style>
        body { font-family: 'Times New Roman', serif; font-size: 10px; }
        table { width: 100%; border-collapse: collapse; margin-top: 10px; }
        table, th, td { border: 1px solid #000; padding: 2px; 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>ABSENSI MAPEL - <?= strtoupper(htmlspecialchars($info['nama_mapel'] ?? '-')) ?></h3>
        <h4>Kelas: <?= htmlspecialchars($info['kelas'] ?? '-') ?></h4>
    </div>

<table>
        <thead>
            <tr>
                <th rowspan="2" style="width: 180px;">No. Nama Siswa</th> 
                <?php foreach($bulan as $bln): ?> 
                    <th colspan="8"><?= $bln ?></th> 
                <?php endforeach; ?>
            </tr>
            
            <tr>
                <?php for($i=1; $i<=48; $i++): ?>
                    <td style="height: 25px;">&nbsp;</td>
                <?php endfor; ?>
            </tr>
        </thead>
        <tbody>
            <?php $no = 1; while($s = mysqli_fetch_array($q_s)): ?>
                <tr>
                    <td class="nama" style="text-align:left; vertical-align:middle;">
                        <?= $no++ . '. ' . htmlspecialchars($s['nama']) ?>
                    </td>
                    <?php for($i=1; $i<=48; $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) ?>'">Kembali</button>
</div>
</body>
</html>