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

$query_tp  = mysqli_query($koneksi, "SELECT * FROM tahun_pelajaran ORDER BY tahun_pelajaran DESC");
$query_sem = mysqli_query($koneksi, "SELECT * FROM semester");
?>

<div class="container-fluid">
    <div class="card p-4 shadow-sm">
        <h5 class="mb-3">Tambah Tahun Pelajaran</h5>
        <form action="proses_set.php" method="POST" class="row g-3 mb-4">
            <input type="hidden" name="aksi" value="tambah_tp">
            <div class="col-auto">
                <input type="text" name="tahun" class="form-control" placeholder="Contoh: 2026/2027" required>
            </div>
            <div class="col-auto">
                <button type="submit" class="btn btn-success">Simpan</button>
            </div>
        </form>

        <hr>

        <h5 class="mb-3">Daftar Tahun Pelajaran</h5>
        <table class="table table-bordered">
            <?php while($d = mysqli_fetch_assoc($query_tp)) { ?>
            <tr>
                <td><?= htmlspecialchars($d['tahun_pelajaran']); ?></td>
                <td>
                    <?php if(($d['status'] ?? '') == 'Aktif') { ?>
                        <span class="badge bg-success">AKTIF</span>
                    <?php } else { ?>
                        <a href="proses_set.php?id=<?= $d['id_tp_tahun']; ?>&tabel=tahun_pelajaran&aksi=aktifkan" class="btn btn-sm btn-primary">Aktifkan</a>
                        <a href="proses_set.php?id=<?= $d['id_tp_tahun']; ?>&tabel=tahun_pelajaran&aksi=hapus" class="btn btn-sm btn-danger" onclick="return confirm('Yakin hapus?')">Hapus</a>
                    <?php } ?>
                </td>
            </tr>
            <?php } ?>
        </table>

        <h5 class="mt-4 mb-3">Semester</h5>
        <table class="table table-bordered">
            <?php while($s = mysqli_fetch_assoc($query_sem)) { ?>
            <tr>
                <td><?= htmlspecialchars($s['nama_semester']); ?></td>
                <td>
                    <?php if(($s['status'] ?? '') == 'Aktif') { ?>
                        <span class="badge bg-success">AKTIF</span>
                    <?php } else { ?>
                        <a href="proses_set.php?id=<?= $s['id_semester']; ?>&tabel=semester&aksi=aktifkan" class="btn btn-sm btn-primary">Aktifkan</a>
                    <?php } ?>
                </td>
            </tr>
            <?php } ?>
        </table>
    </div>
</div>

<?php 
include 'layout_footer.php'; 
ob_end_flush(); 
?>