<?php
/**
 * Admin - Yorum Yönetimi (Site ve Ürün Yorumları)
 * Konum: /magaza/admin/yorumlar/index.php
 */
require_once '../../includes/db.php';
require_once '../../includes/functions.php';
adminCheck();

$mesaj = '';

// --- HIZLI İŞLEMLER (Onayla, Gizle, Sil) ---
if (isset($_GET['islem']) && isset($_GET['id']) && is_numeric($_GET['id'])) {
    $id = (int)$_GET['id'];
    $islem = $_GET['islem'];

    if ($islem == 'onayla') {
        $db->prepare("UPDATE reviews 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> Yorum yayına alındı!<button type='button' class='btn-close' data-bs-dismiss='alert'></button></div>";
    } elseif ($islem == 'gizle') {
        $db->prepare("UPDATE reviews 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> Yorum siteden gizlendi.<button type='button' class='btn-close' data-bs-dismiss='alert'></button></div>";
    } elseif ($islem == 'sil') {
        $db->prepare("DELETE FROM reviews WHERE id = ?")->execute([$id]);
        $mesaj = "<div class='alert alert-danger shadow-sm alert-dismissible fade show'><i class='bi bi-trash me-2'></i> Yorum kalıcı olarak silindi.<button type='button' class='btn-close' data-bs-dismiss='alert'></button></div>";
    }
}

// --- YORUMLARI ÇEK (Ürün adlarıyla birlikte, Bekleyenler en üstte) ---
$yorumlar = $db->query("
    SELECT r.*, p.name as product_name 
    FROM reviews r 
    LEFT JOIN products p ON r.product_id = p.id 
    ORDER BY r.status ASC, r.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-chat-quote text-primary me-2"></i>Müşteri Yorumları</h2>
        <p class="text-muted small mb-0">Siteye ve ürünlere yapılan tüm değerlendirmeler.</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> Manuel Yorum 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">Tür / Hedef</th>
                        <th>Müşteri</th>
                        <th>Puan / Yorum</th>
                        <th>Tarih</th>
                        <th>Durum</th>
                        <th class="text-end pe-4">İşlemler</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if(count($yorumlar) > 0): ?>
                        <?php foreach($yorumlar as $y): ?>
                        <tr class="<?= $y['status'] == 0 ? 'table-warning' : '' ?>">
                            <td class="ps-4">
                                <?php if(empty($y['product_id'])): ?>
                                    <span class="badge bg-primary px-3 py-2 rounded-pill"><i class="bi bi-globe me-1"></i> Genel Site Yorumu</span>
                                <?php else: ?>
                                    <span class="badge bg-info text-dark px-3 py-2 rounded-pill"><i class="bi bi-box-seam me-1"></i> Ürün Yorumu</span>
                                    <div class="small text-muted mt-1 text-truncate" style="max-width: 150px;" title="<?= htmlspecialchars($y['product_name'] ?? 'Ürün bulunamadı') ?>">
                                        <?= htmlspecialchars($y['product_name'] ?? 'Ürün bulunamadı') ?>
                                    </div>
                                <?php endif; ?>
                            </td>
                            <td>
                                <div class="fw-bold fs-6"><?= htmlspecialchars($y['name']) ?></div>
                                <div class="small text-muted border border-success rounded px-1 d-inline-block mt-1" title="Sitede Görünecek İsim">KVKK: <?= maskNameKVKK($y['name']) ?></div>
                            </td>
                            <td style="max-width: 300px;">
                                <div class="text-warning mb-1" style="font-size: 0.8rem;">
                                    <?= str_repeat('<i class="bi bi-star-fill"></i>', $y['rating']) ?>
                                    <?= str_repeat('<i class="bi bi-star"></i>', 5 - $y['rating']) ?>
                                </div>
                                <div class="small text-dark" style="white-space: normal;">
                                    <?= htmlspecialchars($y['comment']) ?>
                                </div>
                            </td>
                            <td><small class="text-muted"><?= date('d.m.Y H:i', strtotime($y['created_at'])) ?></small></td>
                            <td>
                                <?php if($y['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"><i class="bi bi-hourglass-split me-1"></i> Onay Bekliyor</span>
                                <?php endif; ?>
                            </td>
                            <td class="text-end pe-4 text-nowrap">
                                <?php if($y['status'] == 0): ?>
                                    <a href="?islem=onayla&id=<?= $y['id'] ?>" class="btn btn-sm btn-success fw-bold shadow-sm me-1" title="Onayla ve Yayınla"><i class="bi bi-check-lg"></i></a>
                                <?php else: ?>
                                    <a href="?islem=gizle&id=<?= $y['id'] ?>" class="btn btn-sm btn-secondary fw-bold shadow-sm me-1" title="Siteden Gizle"><i class="bi bi-eye-slash"></i></a>
                                <?php endif; ?>
                                
                                <a href="duzenle.php?id=<?= $y['id'] ?>" class="btn btn-sm btn-outline-primary fw-bold shadow-sm me-1" title="Yorumu Düzenle"><i class="bi bi-pencil-square"></i></a>
                                
                                <a href="?islem=sil&id=<?= $y['id'] ?>" class="btn btn-sm btn-outline-danger fw-bold shadow-sm" onclick="return confirm('Bu yorumu 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="6" class="text-center py-5 text-muted">
                                <i class="bi bi-chat-square-text fs-1 d-block mb-3 text-secondary"></i>
                                <h5 class="fw-bold">Henüz hiç müşteri yorumu yok.</h5>
                                <p class="small">Sosyal kanıt oluşturmak için hemen bir manuel yorum ekleyebilirsiniz.</p>
                            </td>
                        </tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

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