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

// Aksi Hapus
if (isset($_GET['hapus'])) {
    $id = $_GET['hapus'];
    mysqli_query($koneksi, "DELETE FROM jadwal_waktu_rombels WHERE id_waktu_rombel = '$id'");
}

// Proses Simpan Jam Baru
if (isset($_POST['tambah_jam'])) {
    $id_rombel = $_POST['id_rombel'];
    $nama = mysqli_real_escape_string($koneksi, $_POST['nama_jam']);
    $mulai = $_POST['waktu_mulai'];
    $selesai = $_POST['waktu_selesai'];
    $istirahat = $_POST['is_istirahat'] ?? 0;

    mysqli_query($koneksi, "INSERT INTO jadwal_waktu_rombels (id_rombel, nama_jam, waktu_mulai, waktu_selesai, is_istirahat) 
                            VALUES ('$id_rombel', '$nama', '$mulai', '$selesai', '$istirahat')");
}

$id_rombel_aktif = $_GET['id_rombel'] ?? 1;
include 'layout.php';
?>

<div class="container-fluid mt-4">
    <div class="card p-4 shadow-sm">
        <h3>Atur Jam Pelajaran Per Kelas</h3>
        
        <div class="mb-4">
            <label>Pilih Kelas untuk Diatur:</label><br>
            <select class="form-select w-50" onchange="window.location.href='?id_rombel='+this.value">
        <option value="">-- Pilih Kelas --</option>
        <?php 
        $q_k = mysqli_query($koneksi, "SELECT * FROM rombel");
        while($k = mysqli_fetch_array($q_k)) {
            // Menambahkan logika 'selected' agar dropdown tahu kelas mana yang sedang aktif
            $selected = ($id_rombel_aktif == $k['id_rombel']) ? 'selected' : '';
            echo "<option value='{$k['id_rombel']}' $selected>{$k['kelas']}</option>"; 
        }
        ?>
    </select>
        </div>

        <form method="POST" class="row g-3 mb-4 align-items-end">
            <input type="hidden" name="id_rombel" value="<?= $id_rombel_aktif ?>">
            <div class="col-md-2"><label>Nama Jam</label><input type="text" name="nama_jam" class="form-control" placeholder="Contoh: Jam ke-1" required></div>
            <div class="col-md-2"><label>Mulai</label><input type="time" name="waktu_mulai" class="form-control" required></div>
            <div class="col-md-2"><label>Selesai</label><input type="time" name="waktu_selesai" class="form-control" required></div>
            <div class="col-md-2"><label>Istirahat?</label><select name="is_istirahat" class="form-select"><option value="0">Tidak</option><option value="1">Ya</option></select></div>
            <div class="col-md-2"><button type="submit" name="tambah_jam" class="btn btn-success">Tambah Jam</button></div>
        </form>

        <table class="table table-bordered">
            <thead class="table-dark">
                <tr><th>Nama Jam</th><th>Mulai</th><th>Selesai</th><th>Jenis</th><th>Aksi</th></tr>
            </thead>
            <tbody>
                <?php 
                $q = mysqli_query($koneksi, "SELECT * FROM jadwal_waktu_rombels WHERE id_rombel = '$id_rombel_aktif' ORDER BY waktu_mulai ASC");
                while($j = mysqli_fetch_array($q)): ?>
                <tr>
                    <td><?= $j['nama_jam'] ?></td>
                    <td><?= substr($j['waktu_mulai'],0,5) ?></td>
                    <td><?= substr($j['waktu_selesai'],0,5) ?></td>
                    <td><?= $j['is_istirahat'] ? 'Istirahat' : 'Pelajaran' ?></td>
                    <td><a href="?id_rombel=<?= $id_rombel_aktif ?>&hapus=<?= $j['id_waktu_rombel'] ?>" class="btn btn-danger btn-sm" onclick="return confirm('Hapus jam ini?')">Hapus</a></td>
                </tr>
                <?php endwhile; ?>
            </tbody>
        </table>
    </div>
</div>
<?php include 'layout_footer.php'; ?>