<?php
// admin/kelas.php
include_once 'init.php'; 

$page_title = 'Manajemen Kelas';
$page_icon = 'bi-easel-fill';

$action = $_POST['action'] ?? $_GET['action'] ?? '';

// === LOGIKA PROSES ===
try {
    if ($action === 'tambah_kelas') {
        $nama_kelas_otomatis = $_POST['tingkat'] . '-' . $_POST['rombel'];
        $stmt = $pdo->prepare("INSERT INTO kelas (tingkat, rombel, nama_kelas) VALUES (?, ?, ?)");
        $stmt->execute([$_POST['tingkat'], $_POST['rombel'], $nama_kelas_otomatis]);
        $_SESSION['message'] = "Kelas $nama_kelas_otomatis berhasil ditambahkan.";
        header("Location: kelas.php"); exit();
    }

    if ($action === 'update_kelas') {
        $nama_kelas_otomatis = $_POST['tingkat'] . '-' . $_POST['rombel'];
        $stmt = $pdo->prepare("UPDATE kelas SET tingkat = ?, rombel = ?, nama_kelas = ? WHERE id_kelas = ?");
        $stmt->execute([$_POST['tingkat'], $_POST['rombel'], $nama_kelas_otomatis, $_POST['id_kelas']]);
        $_SESSION['message'] = "Data kelas berhasil diperbarui.";
        header("Location: kelas.php"); exit();
    }

    if ($action === 'hapus_kelas' && isset($_POST['id_kelas_hapus'])) {
        $stmt = $pdo->prepare("DELETE FROM kelas WHERE id_kelas = ?");
        $stmt->execute([$_POST['id_kelas_hapus']]);
        $_SESSION['message'] = "Kelas berhasil dihapus.";
        header("Location: kelas.php"); exit();
    }
} catch (PDOException $e) {
    $_SESSION['error'] = "Terjadi kesalahan: " . $e->getMessage();
    header("Location: kelas.php"); exit();
}

$kelasList = $pdo->query("SELECT * FROM kelas ORDER BY tingkat, rombel ASC")->fetchAll(PDO::FETCH_ASSOC);
$tingkat_options = [10, 11, 12];

include 'header.php';
?>

<div class="content-card shadow-sm p-4 bg-white rounded">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h5 class="mb-0 fw-bold">Daftar Kelas</h5>
            <small class="text-muted">Kelola tingkat dan rombongan belajar</small>
        </div>
        <button class="btn btn-primary btn-sm" data-bs-toggle="modal" data-bs-target="#modalKelas">
            <i class="bi bi-plus-circle me-1"></i> Tambah Kelas
        </button>
    </div>

    <?php if (isset($_SESSION['message'])): ?>
        <div class="alert alert-success alert-dismissible fade show"><?= $_SESSION['message']; unset($_SESSION['message']); ?><button type="button" class="btn-close" data-bs-dismiss="alert"></button></div>
    <?php endif; ?>

    <div class="table-responsive">
        <table class="table table-hover align-middle border">
            <thead class="table-light">
                <tr>
                    <th width="5%">No</th>
                    <th>Tingkat</th>
                    <th>Rombel</th>
                    <th>Nama Kelas (Auto)</th>
                    <th class="text-center">Aksi</th>
                </tr>
            </thead>
            <tbody>
                <?php $no=1; foreach ($kelasList as $k): ?>
                <tr>
                    <td><?= $no++ ?></td>
                    <td><?= htmlspecialchars($k['tingkat']) ?></td>
                    <td><?= htmlspecialchars($k['rombel']) ?></td>
                    <td><strong><?= htmlspecialchars($k['nama_kelas']) ?></strong></td>
                    <td class="text-center">
                        <button class="btn btn-sm btn-outline-warning btn-edit" 
                                data-bs-toggle="modal" data-bs-target="#modalKelas"
                                data-id="<?= $k['id_kelas'] ?>"
                                data-tingkat="<?= $k['tingkat'] ?>"
                                data-rombel="<?= $k['rombel'] ?>"><i class="bi bi-pencil"></i></button>
                        <button class="btn btn-sm btn-outline-danger btn-delete" 
                                data-id="<?= $k['id_kelas'] ?>" 
                                data-nama="<?= $k['nama_kelas'] ?>"><i class="bi bi-trash"></i></button>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>

<div class="modal fade" id="modalKelas" tabindex="-1">
    <div class="modal-dialog">
        <form class="modal-content" method="POST">
            <div class="modal-header"><h5>Input Data Kelas</h5><button type="button" class="btn-close" data-bs-dismiss="modal"></button></div>
            <div class="modal-body">
                <input type="hidden" name="id_kelas" id="id_kelas">
                <input type="hidden" name="action" id="action_form" value="tambah_kelas">
                <div class="mb-3">
                    <label>Tingkat</label>
                    <select name="tingkat" id="tingkat" class="form-select">
                        <?php foreach($tingkat_options as $t): ?><option value="<?= $t ?>"><?= $t ?></option><?php endforeach; ?>
                    </select>
                </div>
                <div class="mb-3">
                    <label>Rombel</label>
                    <input type="text" name="rombel" id="rombel" class="form-control" placeholder="Misal: A, B, atau 1" required>
                </div>
            </div>
            <div class="modal-footer"><button type="submit" class="btn btn-primary">Simpan</button></div>
        </form>
    </div>
</div>

<div class="modal fade" id="deleteModal" tabindex="-1">
    <div class="modal-dialog modal-sm">
        <form class="modal-content" method="POST">
            <div class="modal-body text-center py-4">
                <input type="hidden" name="id_kelas_hapus" id="delete_id">
                <input type="hidden" name="action" value="hapus_kelas">
                <i class="bi bi-exclamation-octagon text-danger display-4"></i>
                <h5 class="mt-3">Hapus Kelas <b id="nama_del"></b>?</h5>
                <div class="d-flex justify-content-center gap-2 mt-4">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Batal</button>
                    <button type="submit" class="btn btn-danger">Hapus</button>
                </div>
            </div>
        </form>
    </div>
</div>

<script>
document.addEventListener('DOMContentLoaded', function() {
    const editBtns = document.querySelectorAll('.btn-edit');
    editBtns.forEach(btn => {
        btn.addEventListener('click', function() {
            document.getElementById('id_kelas').value = this.dataset.id;
            document.getElementById('tingkat').value = this.dataset.tingkat;
            document.getElementById('rombel').value = this.dataset.rombel;
            document.getElementById('action_form').value = 'update_kelas';
        });
    });

    const delBtns = document.querySelectorAll('.btn-delete');
    const delModal = new bootstrap.Modal(document.getElementById('deleteModal'));
    delBtns.forEach(btn => {
        btn.addEventListener('click', function() {
            document.getElementById('delete_id').value = this.dataset.id;
            document.getElementById('nama_del').textContent = this.dataset.nama;
            delModal.show();
        });
    });
});
</script>

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