<?php 
session_start();
require_once '../koneksi.php';
include 'layout_header.php'; 

// 1. Ambil Periode Aktif
$q_sem = mysqli_query($koneksi, "SELECT id_semester FROM semester WHERE status = 'Aktif' LIMIT 1");
$sem = mysqli_fetch_array($q_sem);
$id_sem_aktif = $sem['id_semester'] ?? 0;

$q_tp = mysqli_query($koneksi, "SELECT id_tp_tahun FROM tahun_pelajaran WHERE status = 'Aktif' LIMIT 1");
$tp = mysqli_fetch_array($q_tp);
$id_tp_aktif = $tp['id_tp_tahun'] ?? 0;

// 2. Ambil ID Guru
$id_guru = isset($_GET['id']) ? (int)$_GET['id'] : 0;
if ($id_guru == 0) { die("ID Guru tidak ditemukan."); }

// 3. Ambil Nama Periode untuk Tampilan
$q_info = mysqli_query($koneksi, "SELECT t.tahun_pelajaran, s.nama_semester 
                                  FROM tahun_pelajaran t, semester s 
                                  WHERE t.id_tp_tahun = '$id_tp_aktif' 
                                  AND s.id_semester = '$id_sem_aktif'");
$info = mysqli_fetch_array($q_info);
$nama_tahun = $info['tahun_pelajaran'] ?? 'Tidak ada';
$nama_semester = $info['nama_semester'] ?? 'Tidak ada';
?>

<div class="container-fluid">
    <h3>Atur Tugas Mengajar</h3>

    <div class="alert alert-info shadow-sm">
        <div class="row">
            <div class="col-md-6">
                <i class="fa fa-calendar"></i> Tahun Pelajaran: <strong><?= htmlspecialchars($nama_tahun) ?></strong>
            </div>
            <div class="col-md-6 text-md-end">
                <i class="fa fa-book"></i> Semester: <strong><?= htmlspecialchars($nama_semester) ?></strong>
            </div>
        </div>
    </div>

    <form action="guru_aksi.php" method="POST">
        <input type="hidden" name="id_guru" value="<?= $id_guru ?>">

        <table class="table table-bordered">
            <thead>
                <tr>
                    <th style="width: 30%;">Nama Mapel</th>
                    <th>Pilih Kelas (Centang yang sesuai)</th>
                </tr>
            </thead>
            <tbody>
            <?php 
            // Query Mapel Unik
            $q_mapel = mysqli_query($koneksi, "SELECT MIN(id_mapel) as id_m, nama_mapel 
                                               FROM master_mapel 
                                               WHERE id_semester = '$id_sem_aktif' 
                                               AND id_tp_tahun = '$id_tp_aktif'
                                               GROUP BY nama_mapel 
                                               ORDER BY nama_mapel ASC");

            while($m = mysqli_fetch_array($q_mapel)) {
                $id_m = $m['id_m']; 
                $nama_m = $m['nama_mapel'];
            ?>
                <tr>
                    <td class="fw-bold"><?= htmlspecialchars($nama_m) ?></td>
                    <td>
                        <div style="max-height: 150px; overflow-y: auto; border: 1px solid #ddd; padding: 10px; background: #f9f9f9;">
                            <div class="row">
                            <?php 
                            $q_rombel = mysqli_query($koneksi, "SELECT * FROM rombel 
                                                                WHERE id_semester = '$id_sem_aktif' 
                                                                AND id_tp_tahun = '$id_tp_aktif' 
                                                                ORDER BY kelas ASC");
                            
                            while($r = mysqli_fetch_array($q_rombel)) {
                                $id_r = $r['id_rombel'];
                                
                                // Cek tugas di periode aktif
                                $q_cek = mysqli_query($koneksi, "SELECT * FROM tugas_guru 
                                                                 WHERE id_guru = '$id_guru' 
                                                                 AND id_mapel = '$id_m' 
                                                                 AND id_rombel = '$id_r'
                                                                 AND id_semester = '$id_sem_aktif'
                                                                 AND id_tp_tahun = '$id_tp_aktif'");
                                
                                $checked = (mysqli_num_rows($q_cek) > 0) ? "checked" : "";
                            ?>
                                <div class="col-md-3">
                                    <label class="d-block">
                                        <input type="checkbox" name="tugas[]" value="<?= $id_m ?>_<?= $id_r ?>" <?= $checked ?>> 
                                        <?= htmlspecialchars($r['kelas']) ?>
                                    </label>
                                </div>
                            <?php } ?>
                            </div>
                        </div>
                    </td>
                </tr>
            <?php } ?>
            </tbody>
        </table>
        
        <div class="mt-3">
            <button type="submit" name="simpan" class="btn btn-success">Simpan Tugas</button>
            <a href="guru.php" class="btn btn-secondary">Kembali</a>
        </div>
    </form>
</div>

<?php include 'layout_footer.php'; ?>