<?php
/**
 * Admin - Sıkça Sorulan Soru (S.S.S) Ekle
 * Konum: /magaza/admin/sss/ekle.php
 */
require_once '../../includes/db.php';
require_once '../../includes/functions.php';
adminCheck();

$mesaj = '';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $page_type = $_POST['page_type']; // 'home', 'category' veya 'product'
    $question = trim($_POST['question']);
    $answer = trim($_POST['answer']);
    $sort_order = (int)$_POST['sort_order'];
    $status = isset($_POST['status']) ? 1 : 0;

    if (empty($question) || empty($answer)) {
        $mesaj = "<div class='alert alert-warning shadow-sm'><i class='bi bi-exclamation-triangle me-2'></i>Lütfen soru ve cevap alanlarını boş bırakmayın.</div>";
    } else {
        try {
            $stmt = $db->prepare("INSERT INTO faqs (page_type, question, answer, sort_order, status) VALUES (?, ?, ?, ?, ?)");
            $stmt->execute([$page_type, $question, $answer, $sort_order, $status]);
            $mesaj = "<div class='alert alert-success shadow-sm'><i class='bi bi-check-circle-fill me-2'></i> Harika! Soru başarıyla eklendi.</div>";
        } catch (Exception $e) {
            $mesaj = "<div class='alert alert-danger shadow-sm'>Hata: " . $e->getMessage() . "</div>";
        }
    }
}

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

<div class="row mb-4 align-items-center text-dark">
    <div class="col-md-6">
        <h2 class="fw-bold mb-0">
            <a href="index.php" class="btn btn-sm btn-outline-secondary me-2 border-2"><i class="bi bi-arrow-left"></i> Geri</a>
            Yeni Soru Ekle
        </h2>
    </div>
</div>

<?= $mesaj ?>

<form action="" method="POST">
    <div class="row text-dark">
        <div class="col-lg-8">
            <div class="card shadow-sm border-0 rounded-4 mb-4">
                <div class="card-body p-4 p-md-5">
                    
                    <div class="mb-4 pb-3 border-bottom">
                        <label class="form-label fw-bold d-block">Soru Nerede Gösterilsin? <span class="text-danger">*</span></label>
                        <div class="form-check form-check-inline">
                            <input class="form-check-input" type="radio" name="page_type" id="typeHome" value="home" checked>
                            <label class="form-check-label fw-bold text-primary" for="typeHome"><i class="bi bi-house-door me-1"></i>Ana Sayfa</label>
                        </div>
                        <div class="form-check form-check-inline">
                            <input class="form-check-input" type="radio" name="page_type" id="typeCategory" value="category">
                            <label class="form-check-label fw-bold text-info" for="typeCategory"><i class="bi bi-tags me-1"></i>Kategori Sayfaları</label>
                        </div>
                        <div class="form-check form-check-inline">
                            <input class="form-check-input" type="radio" name="page_type" id="typeProduct" value="product">
                            <label class="form-check-label fw-bold text-secondary" for="typeProduct"><i class="bi bi-box-seam me-1"></i>Ürün Detayları</label>
                        </div>
                    </div>

                    <div class="mb-4">
                        <label class="form-label fw-bold">Müşterinin Sorusu <span class="text-danger">*</span></label>
                        <input type="text" name="question" class="form-control form-control-lg border-2 fs-6" required placeholder="Örn: Kargo kaç günde teslim ediliyor?">
                    </div>

                    <div class="mb-4">
                        <label class="form-label fw-bold">Sizin Cevabınız <span class="text-danger">*</span></label>
                        <textarea name="answer" class="form-control border-2" rows="5" required placeholder="Örn: Saat 15:00'a kadar verilen siparişler aynı gün kargolanmaktadır..."></textarea>
                        <div class="form-text small mt-2">Cevap alanında müşteriyi tatmin edecek net ve güven verici bir dil kullanın.</div>
                    </div>

                </div>
            </div>
        </div>

        <div class="col-lg-4">
            <div class="card shadow-sm border-0 rounded-4 mb-4 h-100">
                <div class="card-header bg-white py-3 border-bottom"><h6 class="mb-0 fw-bold"><i class="bi bi-gear-fill text-secondary me-2"></i>Yayın Ayarları</h6></div>
                <div class="card-body p-4">
                    
                    <div class="mb-4">
                        <label class="form-label fw-bold small text-muted">Sıralama (Öncelik)</label>
                        <input type="number" name="sort_order" class="form-control border-2" value="0">
                        <small class="text-muted" style="font-size:11px;">Küçük numara her zaman en üstte görünür.</small>
                    </div>

                    <div class="form-check form-switch mb-4 fs-5">
                        <input class="form-check-input cursor-pointer" type="checkbox" name="status" id="statusSwitch" checked>
                        <label class="form-check-label fs-6 ms-2 fw-bold" for="statusSwitch">Anında Yayına Al</label>
                    </div>

                    <button type="submit" class="btn btn-primary btn-lg w-100 fw-bold shadow-sm rounded-pill mt-auto">
                        <i class="bi bi-plus-circle me-1"></i> Soruyu Ekle
                    </button>
                </div>
            </div>
        </div>
    </div>
</form>

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