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

// 1. Cek Akses
cekAdminAtauGuru();

// 2. Normalisasi Data Sesi & Level
$level = isset($_SESSION['role']) ? strtolower($_SESSION['role']) : 'guru'; // Tambahkan fallback 'guru'
$id_guru_login = $_SESSION['id_guru'] ?? 0;

// 3. AMBIL PERIODE AKTIF LANGSUNG DARI DATABASE
$q_aktif = mysqli_query($koneksi, "SELECT s.id_semester, s.nama_semester, t.id_tp_tahun, t.tahun_pelajaran 
                                   FROM semester s, tahun_pelajaran t 
                                   WHERE s.status = 'Aktif' AND t.status = 'Aktif' LIMIT 1");
$aktif = mysqli_fetch_assoc($q_aktif);

$id_sem_aktif  = $aktif['id_semester'] ?? 0;
$id_tp_aktif   = $aktif['id_tp_tahun'] ?? 0;
$nama_semester = $aktif['nama_semester'] ?? 'Belum Set';
$nama_tahun    = $aktif['tahun_pelajaran'] ?? 'Belum Set';

// 4. Ambil Nama Guru
$q_guru = mysqli_query($koneksi, "SELECT nama FROM master_guru WHERE id_guru = '$id_guru_login'");
$g = mysqli_fetch_array($q_guru);
$nama_guru = $g['nama'] ?? 'Guru Tidak Dikenali';

include 'layout.php';
?>

<div class="container-fluid mt-3">
    <div class="alert alert-info shadow-sm">
        <div class="row">
            <div class="col-md-4">
                <i class="fa fa-user"></i> Guru: <strong><?= htmlspecialchars($nama_guru) ?></strong>
            </div>
            <div class="col-md-4 text-md-center">
                <i class="fa fa-calendar"></i> TP: <strong><?= htmlspecialchars($nama_tahun) ?></strong>
            </div>
            <div class="col-md-4 text-md-end">
                <i class="fa fa-book"></i> Semester: <strong><?= htmlspecialchars($nama_semester) ?></strong>
            </div>
        </div>
    </div>

    <div class="row">
        <?php if ($level !== 'admin') { ?>
        <div class="col-md-4">
            <div class="card p-4 shadow-sm">
                <h6>Input Tujuan Pembelajaran</h6><hr>
                <form method="POST" action="simpan_tujuan.php">
                    <div class="mb-3">
                        <label>Kode TP</label>
                        <select name="tp_kode" class="form-control" required>
                            <option value="">-- Pilih --</option>
                            <?php for($i=1; $i<=10; $i++) echo "<option value='TP $i'>TP $i</option>"; ?>
                        </select>
                    </div>
                    <div class="mb-3">
                        <label>Mapel & Kelas</label>
                        <select name="id_tugas" class="form-select" required>
                            <option value="">-- Pilih Mapel & Kelas --</option>
                            <?php 
                            $q_t = mysqli_query($koneksi, "SELECT tg.id_tugas, m.nama_mapel, r.kelas 
                                  FROM tugas_guru tg
                                  JOIN master_mapel m ON tg.id_mapel = m.id_mapel
                                  JOIN rombel r ON tg.id_rombel = r.id_rombel
                                  WHERE tg.id_guru = '$id_guru_login'
                                  AND tg.id_semester = '$id_sem_aktif' 
                                  AND tg.id_tp_tahun = '$id_tp_aktif'"); 
                            while($t = mysqli_fetch_array($q_t)) {
                                echo "<option value='{$t['id_tugas']}'>{$t['nama_mapel']} - {$t['kelas']}</option>";
                            }
                            ?>
                        </select>
                    </div>
                    <div class="mb-3">
                        <label>Deskripsi TP</label>
                        <textarea name="deskripsi_tp" class="form-control" required rows="3"></textarea>
                    </div>
                    <button type="submit" name="simpan" class="btn btn-primary w-100">Simpan TP</button>
                </form>
            </div>
        </div>
        <?php } ?>

        <div class="col-md-<?= ($level == 'admin') ? '12' : '8' ?>">
            <div class="card p-4 shadow-sm">
                <h6>Data Tujuan Pembelajaran</h6><hr>
                <table class="table table-hover border">
                    <thead class="table-dark">
                        <tr>
                            <th>No</th>
                            <?php if($level == 'admin') echo "<th>Guru</th>"; ?>
                            <th>Mapel & Kelas</th>
                            <th>Kode</th>
                            <th>Deskripsi</th>
                            <th>Aksi</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php 
                        $sql = ($level == 'admin') 
                            ? "SELECT tp.*, m.nama_mapel, r.kelas, g.nama as nama_guru 
                               FROM tujuan_pembelajaran tp 
                               LEFT JOIN tugas_guru tg ON tp.id_tugas = tg.id_tugas 
                               LEFT JOIN master_mapel m ON tg.id_mapel = m.id_mapel 
                               LEFT JOIN rombel r ON tg.id_rombel = r.id_rombel 
                               LEFT JOIN master_guru g ON tg.id_guru = g.id_guru 
                               WHERE tp.id_semester = '$id_sem_aktif' AND tp.id_tp_tahun = '$id_tp_aktif'"
                            : "SELECT tp.*, m.nama_mapel, r.kelas 
                               FROM tujuan_pembelajaran tp 
                               LEFT JOIN tugas_guru tg ON tp.id_tugas = tg.id_tugas 
                               LEFT JOIN master_mapel m ON tg.id_mapel = m.id_mapel 
                               LEFT JOIN rombel r ON tg.id_rombel = r.id_rombel 
                               WHERE tg.id_guru = '$id_guru_login' AND tp.id_semester = '$id_sem_aktif' AND tp.id_tp_tahun = '$id_tp_aktif'";

                        $q_data = mysqli_query($koneksi, $sql);
                        $no = 1;
                        while($r = mysqli_fetch_array($q_data)) {
                            echo "<tr>
                                    <td>{$no}</td>";
                            if($level == 'admin') echo "<td>" . ($r['nama_guru'] ?? '-') . "</td>";
                            echo "  <td>{$r['nama_mapel']} ({$r['kelas']})</td>
                                    <td>{$r['tp_kode']}</td>
                                    <td>{$r['deskripsi_tp']}</td> 
                                    <td>
                                        <a href='edit_tujuan.php?id={$r['id']}' class='btn btn-sm btn-warning'>Edit</a>
                                        <a href='hapus_tujuan.php?id={$r['id']}' class='btn btn-sm btn-danger' onclick='return confirm(\"Hapus?\")'>Hapus</a>
                                    </td>
                                  </tr>";
                            $no++;
                        }
                        ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

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