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

// Ambil status aktif saat ini
$aktif = mysqli_fetch_array(mysqli_query($koneksi, "SELECT * FROM pengaturan_aktif WHERE id_aktif=1"));
?>

<div class="container-fluid">
    <div class="d-flex justify-content-between mb-4">
        <h3>Manajemen Tahun & Semester</h3>
        <button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#modalTambah">+ Tambah Tahun</button>
    </div>

    <div class="card shadow-sm border-0">
        <div class="card-body">
            <table class="table table-bordered align-middle">
                <thead class="table-light">
                    <tr><th>Tahun Pelajaran</th><th>Semester Ganjil</th><th>Semester Genap</th><th>Aksi</th></tr>
                </thead>
                <tbody>
                    <?php
                    $sql = mysqli_query($koneksi, "SELECT * FROM tahun_pelajaran ORDER BY id_tp_tahun DESC");
                    while($d = mysqli_fetch_array($sql)) {
                        echo "<tr>
                            <td><b>$d[tahun_pelajaran]</b></td>
                            <td>";
                            // Cek jika Ganjil (id_semester = 1) aktif
                            if($aktif['id_tp_tahun'] == $d['id_tp_tahun'] && $aktif['id_semester'] == 1) {
                                echo '<span class="badge bg-success">✔ SEDANG AKTIF</span>';
                            } else {
                                echo '<a href="aksi_tp.php?tp='.$d['id_tp'].'&sem=1" class="btn btn-sm btn-outline-primary">Aktifkan Ganjil</a>';
                            }
                            echo "</td><td>";
                            // Cek jika Genap (id_semester = 2) aktif
                            if($aktif['id_tp_tahun'] == $d['id_tp_tahun'] && $aktif['id_semester'] == 2) {
                                echo '<span class="badge bg-success">✔ SEDANG AKTIF</span>';
                            } else {
                                echo '<a href="aksi_tp.php?tp='.$d['id_tp_tahun'].'&sem=2" class="btn btn-sm btn-outline-primary">Aktifkan Genap</a>';
                            }
                            echo "</td>
                            <td><a href='aksi_tp.php?hapus=$d[id_tp_tahun]' class='btn btn-sm btn-danger'>Hapus</a></td>
                        </tr>";
                    }
                    ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<div class="modal fade" id="modalTambah" tabindex="-1" aria-hidden="true">
    <div class="modal-dialog">
        <form action="aksi_tp.php" method="POST">
            <div class="modal-content">
                <div class="modal-header"><h5>Tambah Tahun Pelajaran</h5></div>
                <div class="modal-body">
                    <input type="text" name="tahun" class="form-control" placeholder="Contoh: 2026/2027" required>
                </div>
                <div class="modal-footer">
                    <button type="submit" name="simpan" class="btn btn-primary">Simpan</button>
                </div>
            </div>
        </form>
    </div>
</div>

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