<?php 
include 'koneksi.php';
cek_akses(['Super Admin', 'Kepala']);

// Filter unit untuk Kepala Sekolah
$filter_unit = "";
if($_SESSION['role'] == 'Kepala') {
    $filter_unit = "WHERE unit = '{$_SESSION['unit']}'";
}

// Proses Tambah Kelas
if(isset($_POST['simpan'])) {
    $name = mysqli_real_escape_string($conn, $_POST['name']);
    $unit = mysqli_real_escape_string($conn, $_POST['unit']);
    
    $query = "INSERT INTO classes (name, unit) VALUES ('$name', '$unit')";
    if(mysqli_query($conn, $query)) {
        echo "<script>alert('Kelas berhasil ditambahkan!'); window.location='kelola_kelas.php';</script>";
    }
}

// Proses Edit Kelas
if(isset($_POST['update'])) {
    $id = $_POST['id'];
    $name = mysqli_real_escape_string($conn, $_POST['name']);
    $unit = mysqli_real_escape_string($conn, $_POST['unit']);
    
    mysqli_query($conn, "UPDATE classes SET name='$name', unit='$unit' WHERE id=$id");
    echo "<script>alert('Kelas berhasil diupdate!'); window.location='kelola_kelas.php';</script>";
}

// Proses Hapus Kelas
if(isset($_GET['hapus'])) {
    $id = $_GET['hapus'];
    mysqli_query($conn, "DELETE FROM classes WHERE id=$id");
    echo "<script>alert('Kelas berhasil dihapus!'); window.location='kelola_kelas.php';</script>";
}

// Ambil data kelas
$kelas = mysqli_query($conn, "SELECT * FROM classes $filter_unit ORDER BY unit, name");
?>

<!DOCTYPE html>
<html>
<head>
    <title>Kelola Kelas</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/admin-lte@3.2/dist/css/adminlte.min.css">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <link rel="stylesheet" href="assets/css/theme-istiqlal-green.css">
</head>
<body class="hold-transition sidebar-mini layout-fixed">
<div class="wrapper">
    <?php include 'sidebar.php'; ?>
    
    <div class="content-wrapper">
        <div class="content-header">
            <div class="container-fluid">
                <h1 class="m-0"><i class="fas fa-school"></i> Manajemen Kelas</h1>
            </div>
        </div>

        <div class="content">
            <div class="container-fluid">
                <div class="row">
                    <!-- Form Tambah Kelas -->
                    <div class="col-md-5">
                        <div class="card card-primary">
                            <div class="card-header">
                                <h4><i class="fas fa-plus"></i> Tambah Kelas Baru</h4>
                            </div>
                            <div class="card-body">
                                <form method="POST">
                                    <div class="form-group">
                                        <label>Nama Kelas</label>
                                        <input type="text" name="name" class="form-control" placeholder="Contoh: Kelas 1A, Kelas 2B, dll" required>
                                    </div>
                                    <div class="form-group">
                                        <label>Unit</label>
                                        <select name="unit" class="form-control" <?= ($_SESSION['role']=='Kepala')?'disabled':'' ?> required>
                                            <option value="">-- Pilih Unit --</option>
                                            <option value="MIJ">MIJ</option>
                                            <option value="Kelompok Bermain">Kelompok Bermain</option>
                                            <option value="Raudhatul Athfal">Raudhatul Athfal</option>
                                            <option value="Madrasah Ibtidaiyah">Madrasah Ibtidaiyah</option>
                                            <option value="Madrasah Tsanawiyah">Madrasah Tsanawiyah</option>
                                            <option value="Madrasah Aliyah">Madrasah Aliyah</option>
                                            <option value="Istiqlal Boarding School">Istiqlal Boarding School</option>
                                        </select>
                                        <?php if($_SESSION['role']=='Kepala'): ?>
                                            <input type="hidden" name="unit" value="<?= $_SESSION['unit'] ?>">
                                            <small class="text-muted">Unit: <?= $_SESSION['unit'] ?></small>
                                        <?php endif; ?>
                                    </div>
                                    <button type="submit" name="simpan" class="btn btn-primary btn-block">
                                        <i class="fas fa-save"></i> Simpan Kelas
                                    </button>
                                </form>
                            </div>
                        </div>
                    </div>

                    <!-- Daftar Kelas -->
                    <div class="col-md-7">
                        <div class="card">
                            <div class="card-header bg-info text-white">
                                <h4><i class="fas fa-list"></i> Daftar Kelas</h4>
                            </div>
                            <div class="card-body">
                                <table class="table table-bordered">
                                    <thead class="thead-dark">
                                        <tr><th>No</th><th>Nama Kelas</th><th>Unit</th><th>Aksi</th></tr>
                                    </thead>
                                    <tbody>
                                        <?php if(mysqli_num_rows($kelas) > 0): ?>
                                            <?php $no=1; while($row = mysqli_fetch_assoc($kelas)): ?>
                                            <tr>
                                                <td><?= $no++ ?></td>
                                                <td><strong><?= $row['name'] ?></strong></td>
                                                <td><span class="badge badge-secondary"><?= $row['unit'] ?></span></td>
                                                <td>
                                                    <button class="btn btn-sm btn-warning" onclick="editKelas(<?= $row['id'] ?>, '<?= $row['name'] ?>', '<?= $row['unit'] ?>')">
                                                        <i class="fas fa-edit"></i> Edit
                                                    </button>
                                                    <a href="kelola_kelas.php?hapus=<?= $row['id'] ?>" class="btn btn-sm btn-danger" onclick="return confirm('Yakin hapus?')">
                                                        <i class="fas fa-trash"></i> Hapus
                                                    </a>
                                                </td>
                                            </tr>
                                            <?php endwhile; ?>
                                        <?php else: ?>
                                            <tr><td colspan="4" class="text-center">Belum ada kelas</td></tr>
                                        <?php endif; ?>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Modal Edit Kelas -->
<div class="modal fade" id="modalEditKelas">
    <div class="modal-dialog">
        <div class="modal-content">
            <form method="POST">
                <div class="modal-header bg-warning"><h5>Edit Kelas</h5></div>
                <div class="modal-body">
                    <input type="hidden" name="id" id="edit_id">
                    <div class="form-group"><label>Nama Kelas</label><input type="text" name="name" id="edit_name" class="form-control" required></div>
                    <div class="form-group"><label>Unit</label><select name="unit" id="edit_unit" class="form-control" required><?php
                        $units = ['MIJ','Kelompok Bermain','Raudhatul Athfal','Madrasah Ibtidaiyah','Madrasah Tsanawiyah','Madrasah Aliyah','Istiqlal Boarding School'];
                        foreach($units as $u) echo "<option>$u</option>";
                    ?></select></div>
                </div>
                <div class="modal-footer"><button type="submit" name="update" class="btn btn-warning">Update</button></div>
            </form>
        </div>
    </div>
</div>

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
<script>
function editKelas(id, name, unit) {
    document.getElementById('edit_id').value = id;
    document.getElementById('edit_name').value = name;
    document.getElementById('edit_unit').value = unit;
    new bootstrap.Modal(document.getElementById('modalEditKelas')).show();
}
</script>
</body>
</html>