<?php
require_once __DIR__ . '/includes/auth.php';
require_admin();

if ($_SERVER['REQUEST_METHOD'] === 'POST' && csrf_check()) {
    $question = trim($_POST['question'] ?? '');
    $answer = trim($_POST['answer'] ?? '');
    $category = trim($_POST['category'] ?? 'General');
    $sortOrder = (int)($_POST['sort_order'] ?? 0);
    if ($question !== '' && $answer !== '') {
        if (!empty($_POST['id'])) {
            $stmt = db()->prepare('UPDATE faqs SET question=?, answer=?, category=?, sort_order=? WHERE id=?');
            $stmt->execute([$question, $answer, $category, $sortOrder, (int)$_POST['id']]);
        } else {
            $stmt = db()->prepare('INSERT INTO faqs (question, answer, category, sort_order) VALUES (?,?,?,?)');
            $stmt->execute([$question, $answer, $category, $sortOrder]);
        }
    }
    header('Location: /admin/faqs.php');
    exit;
}
if (!empty($_GET['delete']) && isset($_GET['csrf']) && hash_equals($_SESSION['csrf'] ?? '', $_GET['csrf'])) {
    db()->prepare('DELETE FROM faqs WHERE id=?')->execute([(int)$_GET['delete']]);
    header('Location: /admin/faqs.php');
    exit;
}

$editing = null;
if (!empty($_GET['edit'])) {
    $stmt = db()->prepare('SELECT * FROM faqs WHERE id=?');
    $stmt->execute([(int)$_GET['edit']]);
    $editing = $stmt->fetch();
}

$faqs = db()->query('SELECT * FROM faqs ORDER BY category, sort_order')->fetchAll();
$pageTitle = 'FAQs';
include __DIR__ . '/includes/layout-header.php';
?>
<div class="admin-card mb-6">
  <h3><?= $editing ? 'Edit FAQ' : 'Add FAQ' ?></h3>
  <form method="post">
    <input type="hidden" name="csrf" value="<?= e(csrf_token()) ?>">
    <?php if ($editing): ?><input type="hidden" name="id" value="<?= (int)$editing['id'] ?>"><?php endif; ?>
    <div class="field mb-4"><label>Question</label><input type="text" name="question" value="<?= e($editing['question'] ?? '') ?>" required></div>
    <div class="field mb-4"><label>Answer</label><textarea name="answer" rows="3" required><?= e($editing['answer'] ?? '') ?></textarea></div>
    <div class="field-row mb-4">
      <div class="field"><label>Category</label><input type="text" name="category" value="<?= e($editing['category'] ?? 'General') ?>"></div>
      <div class="field"><label>Sort Order</label><input type="number" name="sort_order" value="<?= e($editing['sort_order'] ?? '0') ?>"></div>
    </div>
    <button type="submit" class="btn btn-primary"><?= $editing ? 'Update' : 'Add' ?> FAQ</button>
    <?php if ($editing): ?><a href="/admin/faqs.php" class="btn btn-outline">Cancel</a><?php endif; ?>
  </form>
</div>
<div class="admin-card" style="padding:0;overflow-x:auto;">
  <table class="admin-table">
    <thead><tr><th>Question</th><th>Category</th><th></th></tr></thead>
    <tbody>
      <?php foreach ($faqs as $f): ?>
        <tr>
          <td><?= e($f['question']) ?></td>
          <td><?= e($f['category']) ?></td>
          <td class="flex gap-2">
            <a href="/admin/faqs.php?edit=<?= (int)$f['id'] ?>" class="btn btn-outline btn-sm">Edit</a>
            <a href="/admin/faqs.php?delete=<?= (int)$f['id'] ?>&csrf=<?= e(csrf_token()) ?>" class="btn btn-outline btn-sm" onclick="return confirm('Delete this FAQ?')">Delete</a>
          </td>
        </tr>
      <?php endforeach; ?>
    </tbody>
  </table>
</div>
<?php include __DIR__ . '/includes/layout-footer.php'; ?>
