<?php
/**
 * Admin - Sıkça Sorulan Sorular (S.S.S) Yönetimi
 * Konum: /magaza/admin/sss/index.php
 */
require_once '../../includes/db.php';
require_once '../../includes/functions.php';
adminCheck();

$mesaj = '';

// --- HIZLI İŞLEMLER (Silme ve Durum Değiştirme) ---
if (isset($_GET['islem']) && isset($_GET['id']) && is_numeric($_GET['id'])) {
    $id = (int)$_GET['id'];
    $islem = $_GET['islem'];

    if ($islem == 'aktif') {
        $db->prepare("UPDATE faqs SET status = 1 WHERE id = ?")->execute([$id]);
        $mesaj = "<div class='alert alert-success shadow-sm alert-dismissible fade show'><i class='bi bi-check-circle me-2'></i> Soru yayına alındı!<button type='button' class='btn-close' data-bs-dismiss='alert'></button></div>";
    } elseif ($islem == 'pasif') {
        $db->prepare("UPDATE faqs SET status = 0 WHERE id = ?")->execute([$id]);
        $mesaj = "<div class='alert alert-warning shadow-sm alert-dismissible fade show'><i class='bi bi-eye-slash me-2'></i> Soru siteden gizlendi.<button type='button' class='btn-close' data-bs-dismiss='alert'></button></div>";
    } elseif ($islem == 'sil') {
        $db->prepare("DELETE FROM faqs WHERE id = ?")->execute([$id]);
        $mesaj = "<div class='alert alert-danger shadow-sm alert-dismissible fade show'><i class='bi bi-trash me-2'></i> Soru kalıcı olarak silindi.<button type='button' class='btn-close' data-bs-dismiss='alert'></button></div>";
    }
}

// Tüm soruları çek
$sorular = $db->query("SELECT * FROM faqs ORDER BY page_type ASC, sort_order ASC, id DESC")->fetchAll();

require_once '../header.php';
?>

<div class="d-flex justify-content-between align-items-center mb-4">
    <div>
        <h2 class="fw-bold mb-0 text-dark"><i class="bi bi-question-circle text-primary me-2"></i>S.S.S Yönetimi</h2>
        <p class="text-muted small mb-0">Ana sayfa, kategori ve ürün detay için Sıkça Sorulan Soruları yönetin.</p>
    </div>
    <a href="ekle.php" class="btn btn-primary fw-bold shadow-sm rounded-pill px-4">
        <i class="bi bi-plus-lg me-1"></i> Yeni Soru Ekle
    </a>
</div>

<?= $mesaj ?>

<div class="card shadow-sm border-0 rounded-4 overflow-hidden text-dark mb-5">
    <div class="card-body p-0">
        <div class="table-responsive">
            <table class="table table-hover align-middle mb-0">
                <thead class="table-light">
                    <tr>
                        <th class="ps-4 py-3">Gösterim Yeri</th>
                        <th>Soru</th>
                        <th>Sıra</th>
                        <th>Durum</th>
                        <th class="text-end pe-4">İşlemler</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if(count($sorular) > 0): ?>
                        <?php foreach($sorular as $s): ?>
                        <tr class="<?= $s['status'] == 0 ? 'table-warning' : '' ?>">
                            <td class="ps-4">
                                <?php 
                                    if($s['page_type'] == 'home') echo '<span class="badge bg-primary px-3 py-2 rounded-pill"><i class="bi bi-house-door me-1"></i> Ana Sayfa</span>';
                                    elseif($s['page_type'] == 'category') echo '<span class="badge bg-info text-dark px-3 py-2 rounded-pill"><i class="bi bi-tags me-1"></i> Kategori Sayfaları</span>';
                                    elseif($s['page_type'] == 'product') echo '<span class="badge bg-secondary px-3 py-2 rounded-pill"><i class="bi bi-box-seam me-1"></i> Ürün Detay</span>';
                                ?>
                            </td>
                            <td style="max-width: 400px;">
                                <div class="fw-bold fs-6 text-dark"><?= htmlspecialchars($s['question']) ?></div>
                                <div class="small text-muted text-truncate mt-1" title="<?= htmlspecialchars($s['answer']) ?>">
                                    <?= htmlspecialchars($s['answer']) ?>
                                </div>
                            </td>
                            <td><span class="badge bg-light text-dark border fw-bold"><?= $s['sort_order'] ?></span></td>
                            <td>
                                <?php if($s['status'] == 1): ?>
                                    <span class="badge bg-success rounded-pill px-3">Yayında</span>
                                <?php else: ?>
                                    <span class="badge bg-warning text-dark rounded-pill px-3">Gizli</span>
                                <?php endif; ?>
                            </td>
                            <td class="text-end pe-4 text-nowrap">
                                <?php if($s['status'] == 0): ?>
                                    <a href="?islem=aktif&id=<?= $s['id'] ?>" class="btn btn-sm btn-success fw-bold shadow-sm me-1" title="Yayına Al"><i class="bi bi-check-lg"></i></a>
                                <?php else: ?>
                                    <a href="?islem=pasif&id=<?= $s['id'] ?>" class="btn btn-sm btn-secondary fw-bold shadow-sm me-1" title="Gizle"><i class="bi bi-eye-slash"></i></a>
                                <?php endif; ?>
                                
                                <a href="duzenle.php?id=<?= $s['id'] ?>" class="btn btn-sm btn-outline-primary fw-bold shadow-sm me-1" title="Düzenle"><i class="bi bi-pencil-square"></i></a>
                                <a href="?islem=sil&id=<?= $s['id'] ?>" class="btn btn-sm btn-outline-danger fw-bold shadow-sm" onclick="return confirm('Bu soruyu kalıcı olarak silmek istediğinize emin misiniz?')" title="Sil"><i class="bi bi-trash"></i></a>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    <?php else: ?>
                        <tr>
                            <td colspan="5" class="text-center py-5 text-muted">
                                <i class="bi bi-question-circle fs-1 d-block mb-3 text-secondary"></i>
                                <h5 class="fw-bold">Henüz S.S.S eklenmemiş.</h5>
                                <p class="small">Müşterilerinizin aklındaki soruları gidermek için hemen bir soru ekleyin.</p>
                            </td>
                        </tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<?php require_once '../footer.php'; ?>