<?php
// Session Guard: Keamanan halaman jika belum login
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_faq/aksi_faq.php";
$act  = isset($_GET['act']) ? $_GET['act'] : '';

switch ($act) {
    // ==========================================
    // TAMPIL DATA FAQ (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);">Manajemen FAQ</h4>
            <small style="color: var(--adm-text-sub);">Kelola pertanyaan dan jawaban yang sering ditanyakan</small>
        </div>
        <a href="?module=faq&act=tambahfaq" class="btn btn-danger d-inline-flex align-items-center gap-2">
            <i class="fa fa-plus"></i> Tambah FAQ
        </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: 50px;">NO</th>
                        <th scope="col" class="py-3" style="width: 30%;">PERTANYAAN</th>
                        <th scope="col" class="py-3">JAWABAN</th>
                        <th scope="col" class="py-3 text-center" style="width: 140px;">AKSI</th>
                    </tr>
                </thead>
                <tbody style="font-size: 0.88rem;">
                    <?php
                    $query  = "SELECT * FROM faq ORDER BY id_faq ASC";
                    $tampil = mysqli_query($konek, $query);
                    $no     = 1;

                    if ($tampil && mysqli_num_rows($tampil) > 0) {
                        while ($r = mysqli_fetch_array($tampil)) {
                            $pertanyaan = htmlspecialchars($r['pertanyaan'] ?? '');
                            $jawaban    = $r['jawaban'] ?? '';

                            echo "
                            <tr style='border-bottom: 1px solid var(--adm-border);'>
                                <td class='py-3 text-center align-top'>{$no}</td>
                                <td class='py-3 fw-semibold align-top'>{$pertanyaan}</td>
                                <td class='py-3 align-top' style='color: var(--adm-text-sub);'>{$jawaban}</td>
                                <td class='py-3 text-center align-top'>
                                    <div class='d-flex justify-content-center gap-2'>
                                        <a href='?module=faq&act=editfaq&id={$r['id_faq']}' 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=faq&act=hapus&id={$r['id_faq']}' onclick=\"return confirm('Yakin ingin menghapus FAQ 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='4' class='text-center py-4' style='color: var(--adm-text-sub);'>Belum ada data FAQ.</td></tr>";
                    }
                    ?>
                </tbody>
            </table>
        </div>
    </div>
<?php
    break;

    // ==========================================
    // FORM TAMBAH FAQ
    // ==========================================
    case "tambahfaq":
?>
    <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 FAQ Baru</h4>
            <small style="color: var(--adm-text-sub);">Tambahkan daftar pertanyaan dan jawaban baru</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-10">
            <div class="adm-card p-4">
                <form method="POST" action="<?= $aksi ?>?module=faq&act=input" id="formFaq">
                    <div class="mb-3 row">
                        <label class="col-sm-2 col-form-label fw-semibold" style="color: var(--adm-text);">Pertanyaan</label>
                        <div class="col-sm-10">
                            <input type="text" name="pertanyaan" class="form-control adm-input" placeholder="Masukkan pertanyaan..." required>
                        </div>
                    </div>

                    <div class="mb-4 row">
                        <label class="col-sm-2 col-form-label fw-semibold" style="color: var(--adm-text);">Jawaban</label>
                        <div class="col-sm-10">
                            <textarea name="jawaban" id="lokomedia" class="form-control adm-input" style="height: 200px;" placeholder="Masukkan jawaban..."></textarea>
                        </div>
                    </div>

                    <div class="row pt-2">
                        <div class="col-sm-10 offset-sm-2 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>

    <script>
    document.getElementById('formFaq').addEventListener('submit', function() {
        if (typeof CKEDITOR !== 'undefined' && CKEDITOR.instances.lokomedia) {
            CKEDITOR.instances.lokomedia.updateElement();
        }
        if (typeof tinymce !== 'undefined') {
            tinymce.triggerSave();
        }
    });
    </script>
<?php
    break;

    // ==========================================
    // FORM EDIT FAQ
    // ==========================================
    case "editfaq":
        $id     = mysqli_real_escape_string($konek, $_GET['id'] ?? '');
        $query  = "SELECT * FROM faq WHERE id_faq='$id'";
        $hasil  = mysqli_query($konek, $query);
        $r      = mysqli_fetch_array($hasil);
?>
    <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 FAQ</h4>
            <small style="color: var(--adm-text-sub);">Perbarui pertanyaan dan jawaban #<?= htmlspecialchars($r['id_faq'] ?? '') ?></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-10">
            <div class="adm-card p-4">
                <form method="POST" action="<?= $aksi ?>?module=faq&act=update" id="formFaq">
                    <input type="hidden" name="id" value="<?= htmlspecialchars($r['id_faq'] ?? '') ?>">

                    <div class="mb-3 row">
                        <label class="col-sm-2 col-form-label fw-semibold" style="color: var(--adm-text);">Pertanyaan</label>
                        <div class="col-sm-10">
                            <input type="text" name="pertanyaan" class="form-control adm-input" value="<?= htmlspecialchars($r['pertanyaan'] ?? '') ?>" required>
                        </div>
                    </div>

                    <div class="mb-4 row">
                        <label class="col-sm-2 col-form-label fw-semibold" style="color: var(--adm-text);">Jawaban</label>
                        <div class="col-sm-10">
                            <textarea name="jawaban" id="lokomedia" class="form-control adm-input" style="height: 200px;"><?= htmlspecialchars($r['jawaban'] ?? '') ?></textarea>
                        </div>
                    </div>

                    <div class="row pt-2">
                        <div class="col-sm-10 offset-sm-2 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>

    <script>
    document.getElementById('formFaq').addEventListener('submit', function() {
        if (typeof CKEDITOR !== 'undefined' && CKEDITOR.instances.lokomedia) {
            CKEDITOR.instances.lokomedia.updateElement();
        }
        if (typeof tinymce !== 'undefined') {
            tinymce.triggerSave();
        }
    });
    </script>
<?php
    break;
}
?>