<?php
// Session Guard: Keamanan akses halaman
if (empty($_SESSION['namauser']) && empty($_SESSION['passuser'])) {
    echo "
    <div class='adm-card text-center py-5' style='max-width: 480px; margin: 40px auto;'>
        <div class='mb-3' style='font-size: 3rem; color: var(--adm-primary);'>
            <i class='fa fa-lock'></i>
        </div>
        <h4 style='color: var(--adm-text); font-weight: 700;' class='mb-2'>Akses Terbatas</h4>
        <p style='color: var(--adm-text-sub); font-size: 0.9rem;' class='mb-4'>
            Silahkan login terlebih dahulu untuk mengakses modul ini.
        </p>
        <a href='../../index.php' class='btn btn-danger d-inline-flex align-items-center gap-2 px-4 py-2'>
            <i class='fa fa-sign-in-alt'></i> Login Sekarang
        </a>
    </div>";
    exit();
}

include "../config/koneksi.php";

$aksi = "modul/mod_kategori/aksi_kategori.php";
$act  = isset($_GET['act']) ? $_GET['act'] : '';

switch ($act) {
    // ==========================================
    // TAMPIL DATA KATEGORI (DEFAULT)
    // ==========================================
    default:
?>
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h4 class="fw-bold m-0" style="color: var(--adm-text);">Kategori </h4>
            <small style="color: var(--adm-text-sub);">Kelola daftar kategori konten </small>
        </div>
        <a href="?module=kategori&act=tambahkategori" class="btn btn-danger d-inline-flex align-items-center gap-2">
            <i class="fa fa-plus"></i> Tambah Kategori
        </a>
    </div>

    <div class="adm-card p-4">
        <div class="table-responsive">
            <table class="table align-middle mb-0" style="color: var(--adm-text); border-color: var(--adm-border);">
                <thead>
                    <tr style="border-bottom: 2px solid var(--adm-border-active); color: var(--adm-text-sub); font-size: 0.8rem; text-transform: uppercase;">
                        <th scope="col" class="py-3 text-center" style="width: 60px;">NO</th>
                        <th scope="col" class="py-3">NAMA KATEGORI</th>
                        <th scope="col" class="py-3 text-center" style="width: 160px;">AKSI</th>
                    </tr>
                </thead>
                <tbody style="font-size: 0.88rem;">
                    <?php
                    $query  = "SELECT * FROM kategori ORDER BY id_kategori DESC";
                    $tampil = mysqli_query($konek, $query);
                    $no     = 1;

                    if ($tampil && mysqli_num_rows($tampil) > 0) {
                        while ($r = mysqli_fetch_array($tampil)) {
                            $nama_kat = htmlspecialchars($r['nama_kategori'] ?? '');
                            echo "
                            <tr style='border-bottom: 1px solid var(--adm-border);'>
                                <td class='py-3 text-center align-middle'>{$no}</td>
                                <td class='py-3 align-middle fw-semibold'>{$nama_kat}</td>
                                <td class='py-3 text-center align-middle'>
                                    <div class='d-flex justify-content-center gap-2'>
                                        <a href='?module=kategori&act=editkategori&id={$r['id_kategori']}' class='btn btn-sm btn-outline-primary d-inline-flex align-items-center gap-1' title='Edit'>
                                            <i class='fa fa-edit'></i> Edit
                                        </a>
                                        <a href='{$aksi}?module=kategori&act=hapus&id={$r['id_kategori']}' onclick=\"return confirm('Yakin ingin menghapus kategori ini?')\" class='btn btn-sm btn-outline-danger d-inline-flex align-items-center gap-1' title='Hapus'>
                                            <i class='fa fa-trash'></i>
                                        </a>
                                    </div>
                                </td>
                            </tr>";
                            $no++;
                        }
                    } else {
                        echo "<tr><td colspan='3' class='text-center py-4' style='color: var(--adm-text-sub);'>Belum ada data kategori.</td></tr>";
                    }
                    ?>
                </tbody>
            </table>
        </div>
    </div>
<?php
    break;

    // ==========================================
    // FORM TAMBAH KATEGORI
    // ==========================================
    case "tambahkategori":
?>
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h4 class="fw-bold m-0" style="color: var(--adm-text);">Tambah Kategori</h4>
            <small style="color: var(--adm-text-sub);">Buat kategori baru untuk pengelompokan</small>
        </div>
        <button onclick="self.history.back()" class="btn btn-sm btn-secondary d-inline-flex align-items-center gap-2">
            <i class="fa fa-arrow-left"></i> Kembali
        </button>
    </div>

    <div class="row">
        <div class="col-12 col-lg-8">
            <div class="adm-card p-4">
                <form method="POST" action="<?= $aksi ?>?module=kategori&act=input">
                    <div class="mb-4 row">
                        <label class="col-sm-3 col-form-label fw-semibold" style="color: var(--adm-text);">Nama Kategori</label>
                        <div class="col-sm-9">
                            <input type="text" name="nama_kategori" class="form-control adm-input" placeholder="Masukkan nama kategori..." required autocomplete="off">
                        </div>
                    </div>

                    <div class="row pt-2">
                        <div class="col-sm-9 offset-sm-3 d-flex gap-2">
                            <button type="submit" class="btn btn-danger px-4 d-inline-flex align-items-center gap-2">
                                <i class="fa fa-save"></i> Simpan
                            </button>
                            <button type="button" onclick="self.history.back()" class="btn btn-outline-secondary px-4">
                                Batal
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
<?php
    break;

    // ==========================================
    // FORM EDIT KATEGORI
    // ==========================================
    case "editkategori":
        $id     = mysqli_real_escape_string($konek, $_GET['id'] ?? '');
        $query  = "SELECT * FROM kategori WHERE id_kategori='$id'";
        $hasil  = mysqli_query($konek, $query);
        $r      = mysqli_fetch_array($hasil);

        if (!$r) {
            echo "<script>alert('Data kategori tidak ditemukan!'); window.location='?module=kategori';</script>";
            exit();
        }
?>
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h4 class="fw-bold m-0" style="color: var(--adm-text);">Edit Kategori</h4>
            <small style="color: var(--adm-text-sub);">Perbarui informasi kategori #<?= htmlspecialchars($r['id_kategori']) ?></small>
        </div>
        <button onclick="self.history.back()" class="btn btn-sm btn-secondary d-inline-flex align-items-center gap-2">
            <i class="fa fa-arrow-left"></i> Kembali
        </button>
    </div>

    <div class="row">
        <div class="col-12 col-lg-8">
            <div class="adm-card p-4">
                <form method="POST" action="<?= $aksi ?>?module=kategori&act=update">
                    <input type="hidden" name="id" value="<?= htmlspecialchars($r['id_kategori']) ?>">

                    <div class="mb-4 row">
                        <label class="col-sm-3 col-form-label fw-semibold" style="color: var(--adm-text);">Nama Kategori</label>
                        <div class="col-sm-9">
                            <input type="text" name="nama_kategori" class="form-control adm-input" value="<?= htmlspecialchars($r['nama_kategori'] ?? '') ?>" required autocomplete="off">
                        </div>
                    </div>

                    <div class="row pt-2">
                        <div class="col-sm-9 offset-sm-3 d-flex gap-2">
                            <button type="submit" class="btn btn-danger px-4 d-inline-flex align-items-center gap-2">
                                <i class="fa fa-save"></i> Perbarui
                            </button>
                            <button type="button" onclick="self.history.back()" class="btn btn-outline-secondary px-4">
                                Batal
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
<?php
    break;
}
?>