<?php
require_once __DIR__ . '/auth-check.php';
require_once __DIR__ . '/../includes/functions.php';

$faqs = getData('faq');

// Handle add
if ($_SERVER['REQUEST_METHOD'] === 'POST' && !empty($_POST['question']) && empty($_POST['edit_id'])) {
    $faqs[] = [
        'id'       => generateId($faqs),
        'question' => trim($_POST['question'] ?? ''),
        'answer'   => trim($_POST['answer'] ?? ''),
    ];
    saveData('faq', $faqs);
    setFlash('success', 'FAQ added');
    header('Location: faq.php');
    exit;
}

// Handle edit
if ($_SERVER['REQUEST_METHOD'] === 'POST' && !empty($_POST['edit_id'])) {
    foreach ($faqs as &$f) {
        if ($f['id'] == $_POST['edit_id']) {
            $f['question'] = trim($_POST['question'] ?? $f['question']);
            $f['answer'] = trim($_POST['answer'] ?? $f['answer']);
            break;
        }
    }
    saveData('faq', $faqs);
    setFlash('success', 'FAQ updated');
    header('Location: faq.php');
    exit;
}

// Handle delete
if (!empty($_GET['delete'])) {
    $faqs = array_values(array_filter($faqs, fn($f) => $f['id'] != $_GET['delete']));
    saveData('faq', $faqs);
    setFlash('success', 'FAQ deleted');
    header('Location: faq.php');
    exit;
}

$pageTitle = 'FAQ';
include __DIR__ . '/sidebar.php';
?>

<div class="admin-header">
    <h1 class="admin-title">FAQ</h1>
</div>

<!-- Add Form -->
<div class="admin-card mb-4">
    <h3 style="font-family:var(--font-heading);font-size:0.9rem;text-transform:uppercase;color:var(--text);margin-bottom:1rem;">Add FAQ</h3>
    <form method="POST">
        <div class="mb-3">
            <input type="text" name="question" class="form-control" placeholder="Question *" required style="background:var(--bg2);color:var(--text);border-color:var(--border);">
        </div>
        <div class="mb-3">
            <textarea name="answer" class="form-control" rows="3" placeholder="Answer *" required style="background:var(--bg2);color:var(--text);border-color:var(--border);"></textarea>
        </div>
        <button type="submit" class="btn-admin-primary" style="font-size:0.8rem;padding:0.5rem 1rem;">Add FAQ</button>
    </form>
</div>

<!-- FAQ List -->
<div class="admin-card">
    <?php if (!empty($faqs)): ?>
    <div class="accordion" id="faqAdminAccordion">
        <?php foreach ($faqs as $faq): ?>
        <div class="accordion-item" style="background:var(--bg3);border-color:var(--border);margin-bottom:0.5rem;">
            <h2 class="accordion-header">
                <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#afaq<?php echo $faq['id']; ?>" style="background:var(--bg3);color:var(--text);font-family:var(--font-heading);font-size:0.85rem;text-transform:none;">
                    <?php echo e($faq['question']); ?>
                </button>
            </h2>
            <div id="afaq<?php echo $faq['id']; ?>" class="accordion-collapse collapse" data-bs-parent="#faqAdminAccordion">
                <div class="accordion-body" style="color:var(--muted);font-size:0.85rem;">
                    <?php echo nl2br(e($faq['answer'])); ?>
                    <div class="mt-3">
                        <button class="btn-admin-outline" style="font-size:0.7rem;padding:0.3rem 0.6rem;" data-bs-toggle="collapse" data-bs-target="#faqedit<?php echo $faq['id']; ?>">Edit</button>
                        <a href="faq.php?delete=<?php echo e($faq['id']); ?>" class="btn-admin-outline" style="font-size:0.7rem;padding:0.3rem 0.6rem;color:var(--danger);border-color:var(--danger);" onclick="return confirm('Delete this FAQ?');">Delete</a>
                    </div>
                    <div class="collapse mt-3" id="faqedit<?php echo $faq['id']; ?>">
                        <form method="POST" style="background:var(--bg2);padding:1rem;border-radius:4px;">
                            <input type="hidden" name="edit_id" value="<?php echo e($faq['id']); ?>">
                            <div class="mb-2">
                                <input type="text" name="question" class="form-control" value="<?php echo e($faq['question']); ?>" required style="background:var(--bg);color:var(--text);border-color:var(--border);font-size:0.85rem;">
                            </div>
                            <div class="mb-2">
                                <textarea name="answer" class="form-control" rows="3" required style="background:var(--bg);color:var(--text);border-color:var(--border);font-size:0.85rem;"><?php echo e($faq['answer']); ?></textarea>
                            </div>
                            <button type="submit" class="btn-admin-primary" style="font-size:0.75rem;padding:0.4rem 0.8rem;">Save</button>
                        </form>
                    </div>
                </div>
            </div>
        </div>
        <?php endforeach; ?>
    </div>
    <?php else: ?>
    <p style="color:var(--muted);text-align:center;padding:2rem;">No FAQs yet. Add your first FAQ above.</p>
    <?php endif; ?>
</div>

<?php include __DIR__ . '/../includes/footer.php'; ?>
