<?php
require_once __DIR__.'/../inc/config.php';
require_once __DIR__.'/../inc/db.php';
require_once __DIR__.'/../inc/helpers.php';
require_once __DIR__.'/../inc/csrf.php';

// ... require_once ...
if(empty($_SESSION['admin_id'])){ header('Location: login.php'); exit; } // 1. Semak login dahulu
if ($_SESSION['admin_role'] !== 'admin') { // 2. Kemudian baru semak role
    exit('Akses disekat. Anda tiada kebenaran untuk melihat halaman ini.');
}

if(empty($_SESSION['admin_id'])){ header('Location: login.php'); exit; }

$feedback_msg = '';
$feedback_type = 'error';

if ($_SERVER['REQUEST_METHOD'] === 'POST' && csrf_verify()) {
    $action = $_POST['action'] ?? '';
    $id = (int)($_POST['id'] ?? 0);

    if ($action === 'delete' && $id > 0) {
        db()->prepare("DELETE FROM faq WHERE id=?")->execute([$id]);
        $feedback_msg = 'Soalan lazim telah berjaya dipadam.';
        $feedback_type = 'success';
    } elseif ($action === 'save') {
        $q = trim($_POST['q'] ?? '');
        $a = trim($_POST['a'] ?? '');
        $active = !empty($_POST['active']) ? 1 : 0;

        if ($q && $a) {
            if ($id > 0) {
                db()->prepare("UPDATE faq SET q=?, a=?, active=? WHERE id=?")->execute([$q, $a, $active, $id]);
                $feedback_msg = 'Soalan lazim berjaya dikemaskini.';
            } else {
                db()->prepare("INSERT INTO faq(q, a, active) VALUES(?,?,?)")->execute([$q, $a, $active]);
                $feedback_msg = 'Soalan lazim baru berjaya ditambah.';
            }
            $feedback_type = 'success';
        } else {
            $feedback_msg = 'Soalan dan Jawapan diperlukan.';
        }
    }
}

$faqs = db()->query("SELECT * FROM faq ORDER BY id ASC")->fetchAll();
$editing_faq = null;
if (isset($_GET['edit'])) {
    $edit_id = (int)$_GET['edit'];
    $st = db()->prepare("SELECT * FROM faq WHERE id=?");
    $st->execute([$edit_id]);
    $editing_faq = $st->fetch();
}
?>
<!doctype html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="../public/assets/css/style.css"><title>Urus Soalan Lazim (FAQ)</title>
<style>
    .admin-wrapper { display: flex; align-items: flex-start; gap: 24px; } .admin-sidebar { width: 220px; flex-shrink: 0; } .admin-content { flex-grow: 1; } .feedback { padding: 12px; border-radius: 12px; margin-bottom: 20px; } .feedback.success { background-color: #dcfce7; color: #166534; } .feedback.error { background-color: #fee2e2; color: #991b1b; } table { width: 100%; border-collapse: collapse; } th, td { padding: 10px; text-align: left; border-bottom: 1px solid #e5e7eb; } th { background-color: #f8fafc; } .form-group label { display: block; margin-bottom: 16px; font-weight: 600; } .form-group input, .form-group textarea { width: 100%; padding: 10px; border: 1px solid #d1d5db; border-radius: 8px; margin-top: 6px; box-sizing: border-box; } .form-check { margin-top: 10px; }
</style>
</head><body><div class="container">
<div class="admin-wrapper">
    <aside class="admin-sidebar"><?php require_once '_menu.php'; ?></aside>
    <main class="admin-content">
        <h1>Urus Soalan Lazim (FAQ)</h1>
        <?php if ($feedback_msg): ?><div class="feedback <?= $feedback_type ?>"><?= e($feedback_msg) ?></div><?php endif; ?>

        <div class="card">
            <h2>Senarai FAQ</h2>
            <table>
                <tr><th>ID</th><th>Soalan</th><th>Status</th><th>Tindakan</th></tr>
                <?php foreach($faqs as $f): ?>
                <tr>
                    <td><?= $f['id'] ?></td>
                    <td style="max-width:400px;"><?= e($f['q']) ?></td>
                    <td><?= $f['active'] ? 'Aktif' : 'Tidak Aktif' ?></td>
                    <td>
                        <a href="?edit=<?= $f['id'] ?>" class="cta" style="font-size:12px; padding:6px 10px; background-color:#4f46e5;">Edit</a>
                        <form action="faq.php" method="post" style="display:inline-block;" onsubmit="return confirm('Anda pasti mahu padam ini?');">
                            <?= csrf_field() ?><input type="hidden" name="action" value="delete"><input type="hidden" name="id" value="<?= $f['id'] ?>">
                            <button type="submit" style="background-color:#ef4444; color:white; border:none; border-radius: 8px; padding:6px 10px; font-size:12px; cursor:pointer;">Padam</button>
                        </form>
                    </td>
                </tr>
                <?php endforeach; ?>
            </table>
        </div>

        <div class="card">
            <h2><?= $editing_faq ? 'Edit FAQ' : 'Tambah FAQ Baru' ?></h2>
            <form action="faq.php" method="post">
                <?= csrf_field() ?><input type="hidden" name="action" value="save"><input type="hidden" name="id" value="<?= $editing_faq['id'] ?? 0 ?>">
                <div class="form-group"><label>Soalan (Q)<input name="q" value="<?= e($editing_faq['q'] ?? '') ?>" required></label></div>
                <div class="form-group"><label>Jawapan (A)<textarea name="a" rows="5" required><?= e($editing_faq['a'] ?? '') ?></textarea></label></div>
                <div class="form-check"><label><input type="checkbox" name="active" value="1" <?= ($editing_faq['active'] ?? 1) ? 'checked' : '' ?>> Aktifkan</label></div>
                <button type="submit" class="cta" style="margin-top:20px;"><?= $editing_faq ? 'Simpan Perubahan' : 'Tambah FAQ' ?></button>
                <?php if ($editing_faq): ?><a href="faq.php" style="margin-left:10px;">Batal</a><?php endif; ?>
            </form>
        </div>
    </main>
</div>
</div></body></html>