<?php
declare(strict_types=1);
require_once __DIR__ . '/includes/bootstrap.php';
require_once __DIR__ . '/includes/flash.php';
require_admin();
if (session_status() !== PHP_SESSION_ACTIVE) {
    session_start();
}
$id = (int)($_GET['id'] ?? 0);
$row = null;
if ($id > 0) {
    $st = db()->prepare('SELECT * FROM faqs WHERE id = ?');
    $st->execute([$id]);
    $row = $st->fetch();
}
$error = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST' && csrf_verify($_POST['csrf'] ?? null)) {
    $question = trim((string)($_POST['question'] ?? ''));
    $answer = trim((string)($_POST['answer'] ?? ''));
    $sort = (int)($_POST['sort_order'] ?? 0);
    if ($question === '') {
        $error = 'Question is required.';
    } else {
        if ($id > 0) {
            db()->prepare('UPDATE faqs SET question=?, answer=?, sort_order=? WHERE id=?')->execute([$question, $answer, $sort, $id]);
            admin_flash_set('success', 'FAQ updated.');
        } else {
            db()->prepare('INSERT INTO faqs (question, answer, sort_order) VALUES (?,?,?)')->execute([$question, $answer, $sort]);
            admin_flash_set('success', 'FAQ item created.');
        }
        header('Location: faqs.php');
        exit;
    }
}
if ($id > 0 && !$row) {
    admin_flash_set('danger', 'Not found.');
    header('Location: faqs.php');
    exit;
}
$page = 'faqs';
$pageTitle = $id ? 'Edit FAQ' : 'New FAQ';
$pageSubtitle = 'Pairs with the public FAQ accordion (Bootstrap collapse).';
$breadcrumbs = [['label' => 'Dashboard', 'href' => 'index.php'], ['label' => 'FAQ', 'href' => 'faqs.php'], ['label' => $id ? 'Edit' : 'New', 'href' => null]];
$headerActions = '<a href="faqs.php" class="btn btn-outline-secondary btn-sm">Back to list</a>';
require __DIR__ . '/includes/layout-header.php';
$r = $row ?: ['question' => '', 'answer' => '', 'sort_order' => 0];
if ($error) {
    echo '<div class="alert alert-danger">' . h($error) . '</div>';
}
?>
<form method="post" class="row g-4">
  <input type="hidden" name="csrf" value="<?= h(csrf_token()) ?>">
  <div class="col-lg-10">
    <div class="admin-card rounded mb-4">
      <div class="card-header">FAQ content</div>
      <div class="card-body">
        <div class="mb-4">
          <label class="form-label fw-semibold">Question <span class="text-danger">*</span></label>
          <input type="text" name="question" class="form-control form-control-lg" required value="<?= h((string)($_POST['question'] ?? $r['question'])) ?>">
        </div>
        <div class="mb-4">
          <label class="form-label fw-semibold">Answer</label>
          <textarea name="answer" class="form-control" rows="12" placeholder="Plain text; line breaks are preserved on the site."><?= h((string)($_POST['answer'] ?? $r['answer'])) ?></textarea>
        </div>
        <div class="row">
          <div class="col-md-4">
            <label class="form-label fw-semibold">Sort order</label>
            <input type="number" name="sort_order" class="form-control" value="<?= (int)($_POST['sort_order'] ?? $r['sort_order']) ?>">
            <div class="form-text">Lower numbers appear first in the list.</div>
          </div>
        </div>
      </div>
    </div>
    <button type="submit" class="btn btn-primary btn-lg me-2"><i class="bi bi-check-lg me-1"></i>Save</button>
    <a href="faqs.php" class="btn btn-outline-secondary">Cancel</a>
  </div>
</form>
<?php require __DIR__ . '/includes/layout-footer.php'; ?>
