<?php
require_once '../auth/cek_login.php';
require_role('admin');
require_once '../config/koneksi.php';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $nama = trim($_POST['nama_mapel'] ?? '');
    $urutan = (int)($_POST['urutan'] ?? 0);

    if ($nama !== '') {
        $stmt = $conn->prepare("
            INSERT INTO mapel(nama_mapel, urutan) 
            VALUES(?, ?)
        ");
        $stmt->bind_param('si', $nama, $urutan);
        $stmt->execute();
    }

    header('Location: mapel.php?ok=1');
    exit;
}

include 'header.php';

$mapel = $conn->query("
    SELECT * 
    FROM mapel 
    ORDER BY urutan ASC, nama_mapel ASC
");
?>

<div class="d-flex justify-content-between align-items-center mb-3">
    <h3 class="fw-bold mb-0">Mata Pelajaran</h3>
</div>

<?php if (isset($_GET['ok'])): ?>
    <div class="alert alert-success">
        Mata pelajaran berhasil disimpan.
    </div>
<?php endif; ?>

<div class="row g-3">

    <div class="col-md-4">
        <form method="post" class="card p-3">
            <h5 class="fw-bold">Tambah Mapel</h5>

            <label>Nama Mata Pelajaran</label>
            <input
                name="nama_mapel"
                class="form-control mb-2"
                required
                placeholder="Contoh: Bahasa Indonesia">

            <label>Urutan</label>
            <input
                type="number"
                name="urutan"
                class="form-control mb-3"
                value="0">

            <button class="btn btn-primary">
                Simpan
            </button>
        </form>
    </div>

    <div class="col-md-8">
        <div class="card p-3 table-responsive">
            <table class="table table-bordered table-striped align-middle">
                <thead>
                    <tr>
                        <th width="60">No</th>
                        <th>Mapel</th>
                        <th width="90">Urutan</th>
                        <th width="160">Aksi</th>
                    </tr>
                </thead>

                <tbody>
                    <?php $no = 1; ?>
                    <?php while ($m = $mapel->fetch_assoc()): ?>
                        <tr>
                            <td><?= $no++ ?></td>
                            <td><?= e($m['nama_mapel'] ?? '') ?></td>
                            <td><?= e($m['urutan'] ?? '') ?></td>
                            <td>
                                <a
                                    class="btn btn-sm btn-warning"
                                    href="edit_mapel.php?id=<?= e($m['id']) ?>">
                                    Edit
                                </a>

                                <a
                                    class="btn btn-sm btn-danger"
                                    onclick="return confirm('Hapus mapel ini? Nilai terkait juga akan ikut terhapus.')"
                                    href="hapus_mapel.php?id=<?= e($m['id']) ?>">
                                    Hapus
                                </a>
                            </td>
                        </tr>
                    <?php endwhile; ?>
                </tbody>

            </table>
        </div>
    </div>

</div>

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