<?php
/**
 * Admin - Kayan Yazı (Marquee) Yönetimi
 * Konum: /magaza/admin/marquee/index.php
 */
require_once '../../includes/db.php';
require_once '../../includes/functions.php';
adminCheck();

$mesaj = '';

// --- SİLME İŞLEMİ ---
if (isset($_GET['sil']) && is_numeric($_GET['sil'])) {
    $sil_id = (int)$_GET['sil'];
    $db->prepare("DELETE FROM marquees WHERE id = ?")->execute([$sil_id]);
    $mesaj = "<div class='alert alert-success shadow-sm alert-dismissible fade show'><i class='bi bi-trash-fill me-2'></i> Kayan yazı başarıyla silindi.<button type='button' class='btn-close' data-bs-dismiss='alert'></button></div>";
}

// --- VERİLERİ ÇEK ---
$marquees = $db->query("SELECT * FROM marquees ORDER BY id DESC")->fetchAll();

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

<div class="d-flex justify-content-between align-items-center mb-4">
    <h2 class="fw-bold mb-0 text-dark"><i class="bi bi-cursor-text text-primary me-2"></i>Kayan Yazı (Marquee) Yönetimi</h2>
    <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 Yazı 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" style="width: 150px;">Gösterim Yeri</th>
                        <th>Kayan Yazı İçeriği</th>
                        <th>Durum</th>
                        <th class="text-end pe-4">İşlemler</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if(count($marquees) > 0): ?>
                        <?php foreach($marquees as $m): ?>
                        <tr>
                            <td class="ps-4">
                                <?php 
                                    if($m['page_type'] == 'home') echo '<span class="badge bg-primary px-3 py-2 rounded-pill"><i class="bi bi-house me-1"></i> Ana Sayfa</span>';
                                    elseif($m['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 Sayfası</span>';
                                    elseif($m['page_type'] == 'product') echo '<span class="badge bg-success px-3 py-2 rounded-pill"><i class="bi bi-box me-1"></i> Ürün Detayı</span>';
                                    else echo '<span class="badge bg-secondary">Bilinmiyor</span>';
                                ?>
                            </td>
                            <td>
                                <div class="small bg-light p-2 rounded border" style="max-height: 60px; overflow: hidden;">
                                    <?= htmlspecialchars($m['content']) ?>
                                </div>
                            </td>
                            <td>
                                <?= $m['status'] == 1 ? '<span class="badge bg-success rounded-pill px-3">Aktif</span>' : '<span class="badge bg-secondary rounded-pill px-3">Pasif</span>' ?>
                            </td>
                            <td class="text-end pe-4">
                                <a href="duzenle.php?id=<?= $m['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="?sil=<?= $m['id'] ?>" class="btn btn-sm btn-outline-danger fw-bold shadow-sm" onclick="return confirm('Bu yazıyı silmek istediğinize emin misiniz?')" title="Sil">
                                    <i class="bi bi-trash"></i>
                                </a>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    <?php else: ?>
                        <tr>
                            <td colspan="4" class="text-center py-5 text-muted">
                                <i class="bi bi-cursor-text fs-1 d-block mb-3 text-secondary"></i>
                                <h5 class="fw-bold">Henüz hiç kayan yazı eklenmemiş.</h5>
                            </td>
                        </tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

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