<?php
/**
 * Admin - Manuel Yorum Ekle
 * Konum: /magaza/admin/yorumlar/ekle.php
 */
require_once '../../includes/db.php';
require_once '../../includes/functions.php';
adminCheck();

$mesaj = '';

// Ürün Yorumu eklenecekse diye ürünleri listelemek için çekiyoruz
$urunler = $db->query("SELECT id, name FROM products WHERE status = 1 ORDER BY id DESC")->fetchAll();

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $type = $_POST['type']; // 'site' veya 'product'
    $product_id = ($type == 'product' && !empty($_POST['product_id'])) ? (int)$_POST['product_id'] : NULL;
    $name = trim($_POST['name']);
    $rating = (int)$_POST['rating'];
    $comment = trim($_POST['comment']);
    $status = isset($_POST['status']) ? 1 : 0;

    if (empty($name) || empty($comment)) {
        $mesaj = "<div class='alert alert-warning shadow-sm'>Lütfen müşteri adını ve yorum içeriğini boş bırakmayın.</div>";
    } elseif ($type == 'product' && empty($product_id)) {
        $mesaj = "<div class='alert alert-warning shadow-sm'>Lütfen yorumun yapılacağı ürünü seçin.</div>";
    } else {
        try {
            $stmt = $db->prepare("INSERT INTO reviews (product_id, name, rating, comment, status) VALUES (?, ?, ?, ?, ?)");
            $stmt->execute([$product_id, $name, $rating, $comment, $status]);
            $mesaj = "<div class='alert alert-success shadow-sm'><i class='bi bi-check-circle-fill me-2'></i> Harika! Yorum 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>
            Manuel Yorum 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">Yorumun Hedefi <span class="text-danger">*</span></label>
                        <div class="form-check form-check-inline">
                            <input class="form-check-input" type="radio" name="type" id="typeSite" value="site" checked onchange="toggleProductSelect()">
                            <label class="form-check-label fw-bold text-primary" for="typeSite">Genel Site Yorumu (Ana Sayfa Slider)</label>
                        </div>
                        <div class="form-check form-check-inline">
                            <input class="form-check-input" type="radio" name="type" id="typeProduct" value="product" onchange="toggleProductSelect()">
                            <label class="form-check-label fw-bold text-info" for="typeProduct">Spesifik Ürün Yorumu</label>
                        </div>
                    </div>

                    <div class="mb-4" id="productSelectDiv" style="display: none;">
                        <label class="form-label fw-bold">Hangi Ürüne Yorum Yapılıyor?</label>
                        <select name="product_id" class="form-select border-2">
                            <option value="">-- Ürün Seçiniz --</option>
                            <?php foreach($urunler as $u): ?>
                                <option value="<?= $u['id'] ?>"><?= htmlspecialchars($u['name']) ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>

                    <div class="row g-3 mb-4">
                        <div class="col-md-8">
                            <label class="form-label fw-bold">Müşteri Adı Soyadı <span class="text-danger">*</span></label>
                            <input type="text" name="name" class="form-control border-2" required placeholder="Örn: Ali Yılmaz">
                            <div class="form-text small">Müşteri adı KVKK gereği sitede <b>A** Y*****</b> olarak maskelenecektir.</div>
                        </div>
                        <div class="col-md-4">
                            <label class="form-label fw-bold">Puan (Yıldız) <span class="text-danger">*</span></label>
                            <select name="rating" class="form-select border-2 fw-bold text-warning">
                                <option value="5" selected>⭐⭐⭐⭐⭐ (5 Yıldız)</option>
                                <option value="4">⭐⭐⭐⭐ (4 Yıldız)</option>
                                <option value="3">⭐⭐⭐ (3 Yıldız)</option>
                                <option value="2">⭐⭐ (2 Yıldız)</option>
                                <option value="1">⭐ (1 Yıldız)</option>
                            </select>
                        </div>
                    </div>

                    <div class="mb-4">
                        <label class="form-label fw-bold">Yorum İçeriği <span class="text-danger">*</span></label>
                        <textarea name="comment" class="form-control border-2" rows="4" required placeholder="Örn: Kargolama harikaydı, denemeler tam yeni nesil..."></textarea>
                    </div>

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

        <div class="col-lg-4">
            <div class="card shadow-sm border-0 rounded-4 mb-4">
                <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="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">
                        <i class="bi bi-plus-circle me-1"></i> Yorumu Ekle
                    </button>
                </div>
            </div>
        </div>
    </div>
</form>

<script>
// "Spesifik Ürün Yorumu" seçildiğinde ürün listesini gösteren basit JS
function toggleProductSelect() {
    var isProduct = document.getElementById('typeProduct').checked;
    document.getElementById('productSelectDiv').style.display = isProduct ? 'block' : 'none';
}
</script>

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