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

// 1. Ambil Semester dan Tahun Pelajaran Aktif
$q_aktif = mysqli_query($koneksi, "SELECT s.id_semester, t.id_tp_tahun 
                                   FROM semester s, tahun_pelajaran t 
                                   WHERE s.status = 'Aktif' AND t.status = 'Aktif' LIMIT 1");
$aktif = mysqli_fetch_array($q_aktif);
$id_sem = $aktif['id_semester'] ?? 0;
$id_tp_tahun = $aktif['id_tp_tahun'] ?? 0;

// 2. Proses Simpan
if (isset($_POST['simpan'])) {
    $nama = mysqli_real_escape_string($koneksi, $_POST['nama_ekskul']);
    $id_guru = mysqli_real_escape_string($koneksi, $_POST['id_guru']);
    
    $simpan = mysqli_query($koneksi, "INSERT INTO ekstrakurikuler (nama_ekskul, id_guru, id_semester, id_tp_tahun) 
                                      VALUES ('$nama', '$id_guru', '$id_sem', '$id_tp_tahun')");
    if ($simpan) {
        echo "<script>alert('Berhasil ditambah'); window.location='ekskul.php';</script>";
    } else {
        echo "<script>alert('Gagal: " . mysqli_error($koneksi) . "');</script>";
    }
}

// 3. Proses Hapus
if (isset($_GET['hapus'])) {
    $id = $_GET['hapus'];
    mysqli_query($koneksi, "DELETE FROM ekstrakurikuler WHERE id_ekskul = '$id'");
    echo "<script>alert('Berhasil dihapus'); window.location='ekskul.php';</script>";
}

include 'layout_header.php'; 
?>

<div class="container-fluid p-4">
    <h3>Manajemen Ekstrakurikuler</h3>
    <div class="alert alert-info shadow-sm">
    <i class="fa fa-info-circle"></i> 
    Periode Aktif: <strong>TP <?= $id_tp_tahun ?> | Semester <?= $id_sem ?></strong>
</div>
    <form method="POST" class="row g-3 mb-4 card p-3 shadow-sm">
        <div class="col-md-6">
            <input type="text" name="nama_ekskul" class="form-control" placeholder="Nama Ekstrakurikuler" required>
        </div>
        <div class="col-md-4">
            <select name="id_guru" class="form-select" required>
                <option value="">-- Pilih Pembina --</option>
                <?php
                $qg = mysqli_query($koneksi, "SELECT id_guru, nama FROM master_guru");
                while($g = mysqli_fetch_array($qg)) {
                    echo "<option value='{$g['id_guru']}'>{$g['nama']}</option>";
                }
                ?>
            </select>
        </div>
        <div class="col-md-2">
            <button type="submit" name="simpan" class="btn btn-success w-100">Tambah</button>
        </div>
    </form>

    <div class="card shadow p-4">
        <table class="table table-bordered table-hover">
            <thead class="table-dark">
                <tr>
                    <th>No</th>
                    <th>Nama Ekskul</th>
                    <th>Pembina</th>
                    <th>Aksi</th>
                </tr>
            </thead>
            <tbody>
                <?php
                $no = 1;
                // Query dengan filter semester dan tahun aktif
                $sql = "SELECT e.*, g.nama as nama_guru FROM ekstrakurikuler e 
                        LEFT JOIN master_guru g ON e.id_guru = g.id_guru 
                        WHERE e.id_semester = '$id_sem' AND e.id_tp_tahun = '$id_tp_tahun'";
                
                $q = mysqli_query($koneksi, $sql);
                
                if (mysqli_num_rows($q) > 0) {
                    while($d = mysqli_fetch_array($q)) {
                        echo "<tr>
                            <td>{$no}</td>
                            <td>{$d['nama_ekskul']}</td>
                            <td>" . ($d['nama_guru'] ?? 'Tidak ada') . "</td>
                            <td>
                                <a href='ekskul.php?hapus={$d['id_ekskul']}' class='btn btn-danger btn-sm' onclick=\"return confirm('Yakin?')\">Hapus</a>
                            </td>
                        </tr>";
                        $no++;
                    }
                } else {
                    echo "<tr><td colspan='4' class='text-center'>Data tidak ditemukan untuk tahun pelajaran ini.</td></tr>";
                }
                ?>
            </tbody>
        </table>
    </div>
</div>

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