<?php $basePath = defined('BASE_PATH') ? BASE_PATH : '/gmind'; ?>
<!-- FAQs Management -->
<div class="admin-section">
    <?php if (!($showForm ?? false)): ?>
    <div class="admin-section-header">
        <a href="<?= $basePath ?>/admin/faqs/create" class="admin-btn admin-btn-primary">
            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="18" height="18"><line x1="12" y1="5" x2="12" y2="19"/><line x1="5" y1="12" x2="19" y2="12"/></svg>
            Add New FAQ
        </a>
    </div>
    <?php endif; ?>

    <?php if ($showForm ?? false): ?>
    <!-- Add/Edit FAQ Form -->
    <div class="admin-card" style="margin-bottom: 24px;">
        <div class="admin-card-header">
            <h2 class="admin-card-title"><?= $editFaq ? 'Edit FAQ' : 'Add New FAQ' ?></h2>
        </div>
        <div class="admin-card-body">
            <form method="POST" action="<?= $editFaq ? $basePath . '/admin/faqs/' . (int)$editFaq['id'] : $basePath . '/admin/faqs' ?>" class="admin-form" novalidate>
                <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken ?? '', ENT_QUOTES, 'UTF-8') ?>">

                <div class="admin-form-group">
                    <label for="question" class="admin-form-label">Question <span class="admin-required">*</span></label>
                    <input type="text" id="question" name="question" class="admin-form-input" required
                           value="<?= htmlspecialchars($editFaq['question'] ?? '', ENT_QUOTES, 'UTF-8') ?>">
                </div>

                <div class="admin-form-group">
                    <label for="answer" class="admin-form-label">Answer <span class="admin-required">*</span></label>
                    <textarea id="answer" name="answer" class="admin-form-textarea" rows="5" required><?= htmlspecialchars($editFaq['answer'] ?? '', ENT_QUOTES, 'UTF-8') ?></textarea>
                </div>

                <div class="admin-form-row">
                    <div class="admin-form-group">
                        <label for="category" class="admin-form-label">Category</label>
                        <input type="text" id="category" name="category" class="admin-form-input" placeholder="e.g. General, Courses, Admission"
                               value="<?= htmlspecialchars($editFaq['category'] ?? '', ENT_QUOTES, 'UTF-8') ?>">
                    </div>
                    <div class="admin-form-group">
                        <label for="sort_order" class="admin-form-label">Sort Order</label>
                        <input type="number" id="sort_order" name="sort_order" class="admin-form-input" min="0"
                               value="<?= htmlspecialchars($editFaq['sort_order'] ?? '0', ENT_QUOTES, 'UTF-8') ?>">
                    </div>
                </div>

                <div class="admin-form-row">
                    <div class="admin-form-group">
                        <label class="admin-form-label">Options</label>
                        <label class="admin-toggle">
                            <input type="checkbox" name="show_on_home" value="1" <?= ($editFaq['show_on_home'] ?? 0) ? 'checked' : '' ?>>
                            <span class="admin-toggle-slider"></span>
                            <span class="admin-toggle-label">Show on Homepage</span>
                        </label>
                        <label class="admin-toggle" style="margin-top: 8px;">
                            <input type="checkbox" name="active" value="1" <?= ($editFaq['active'] ?? 1) ? 'checked' : '' ?>>
                            <span class="admin-toggle-slider"></span>
                            <span class="admin-toggle-label">Active</span>
                        </label>
                    </div>
                </div>

                <div class="admin-form-actions">
                    <button type="submit" class="admin-btn admin-btn-primary"><?= $editFaq ? 'Update FAQ' : 'Create FAQ' ?></button>
                    <a href="<?= $basePath ?>/admin/faqs" class="admin-btn admin-btn-secondary">Cancel</a>
                </div>
            </form>
        </div>
    </div>
    <?php endif; ?>

    <!-- FAQs Table -->
    <?php if (!empty($faqs)): ?>
    <div class="admin-card">
        <div class="admin-card-body">
            <div class="admin-table-responsive">
                <table class="admin-table" data-search="true">
                    <thead>
                        <tr>
                            <th>Question</th>
                            <th>Category</th>
                            <th>Show on Home</th>
                            <th>Active</th>
                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($faqs as $faq): ?>
                        <tr>
                            <td>
                                <strong><?= htmlspecialchars(mb_substr($faq['question'], 0, 80) . (mb_strlen($faq['question']) > 80 ? '...' : ''), ENT_QUOTES, 'UTF-8') ?></strong>
                                <br><small class="admin-text-muted"><?= htmlspecialchars(mb_substr($faq['answer'], 0, 100) . (mb_strlen($faq['answer']) > 100 ? '...' : ''), ENT_QUOTES, 'UTF-8') ?></small>
                            </td>
                            <td><?= htmlspecialchars($faq['category'] ?? '—', ENT_QUOTES, 'UTF-8') ?: '—' ?></td>
                            <td><?= $faq['show_on_home'] ? '<span class="admin-badge admin-badge-success">Yes</span>' : '—' ?></td>
                            <td>
                                <label class="admin-toggle admin-toggle-sm">
                                    <input type="checkbox" <?= $faq['active'] ? 'checked' : '' ?>>
                                    <span class="admin-toggle-slider"></span>
                                </label>
                            </td>
                            <td>
                                <div class="admin-actions">
                                    <a href="<?= $basePath ?>/admin/faqs/<?= (int) $faq['id'] ?>/edit" class="admin-btn-icon" title="Edit">
                                        <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="16" height="16"><path d="M11 4H4a2 2 0 00-2 2v14a2 2 0 002 2h14a2 2 0 002-2v-7"/><path d="M18.5 2.5a2.121 2.121 0 013 3L12 15l-4 1 1-4 9.5-9.5z"/></svg>
                                    </a>
                                    <button type="button" class="admin-btn-icon admin-btn-delete" title="Delete"
                                            data-delete-url="<?= $basePath ?>/admin/faqs/<?= (int) $faq['id'] ?>/delete">
                                        <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="16" height="16"><polyline points="3 6 5 6 21 6"/><path d="M19 6v14a2 2 0 01-2 2H7a2 2 0 01-2-2V6m3 0V4a2 2 0 012-2h4a2 2 0 012 2v2"/></svg>
                                    </button>
                                </div>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    <?php elseif (!($showForm ?? false)): ?>
    <div class="admin-card">
        <div class="admin-card-body">
            <p class="admin-empty-text">No FAQs found. <a href="<?= $basePath ?>/admin/faqs/create">Add your first FAQ</a>.</p>
        </div>
    </div>
    <?php endif; ?>
</div>
