<?php
/**
 * Admin - Slider (Vitrin Afiş) Yönetimi
 * Konum: /magaza/admin/slider/index.php
 */
require_once '../../includes/db.php';
require_once '../../includes/functions.php';
adminCheck();

$mesaj = '';

// Slider Silme İşlemi
if (isset($_GET['sil']) && is_numeric($_GET['sil'])) {
    $sil_id = (int)$_GET['sil'];
    
    // Önce resmi sunucudan silelim
    $sorgu = $db->prepare("SELECT image_path FROM sliders WHERE id = ?");
    $sorgu->execute([$sil_id]);
    $resim = $sorgu->fetchColumn();
    
    if ($resim && file_exists("../../" . $resim)) {
        unlink("../../" . $resim);
    }
    
    $db->prepare("DELETE FROM sliders WHERE id = ?")->execute([$sil_id]);
    $mesaj = "<div class='alert alert-success shadow-sm'><i class='bi bi-trash-fill me-2'></i> Slider başarıyla silindi.</div>";
}

// Sliderları Çek
$sliders = $db->query("SELECT * FROM sliders ORDER BY sort_order ASC, 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-images text-primary me-2"></i>Vitrin Slider Yönetimi</h2>
    <a href="ekle.php" class="btn btn-primary fw-bold shadow-sm">
        <i class="bi bi-plus-lg me-1"></i> Yeni Slider Ekle
    </a>
</div>

<?= $mesaj ?>

<div class="card shadow-sm border-0 rounded-4 overflow-hidden text-dark">
    <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örsel</th>
                        <th>Başlık / Alt Başlık</th>
                        <th>Buton Bağlantısı</th>
                        <th>Durum</th>
                        <th class="text-end pe-4">İşlemler</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if(count($sliders) > 0): ?>
                        <?php foreach($sliders as $s): ?>
                        <tr>
                            <td class="ps-4">
                                <img src="../../<?= htmlspecialchars($s['image_path']) ?>" class="img-fluid rounded border shadow-sm" style="max-height: 70px; object-fit: cover;">
                            </td>
                            <td>
                                <div class="fw-bold fs-6"><?= htmlspecialchars($s['title'] ?: 'Başlıksız') ?></div>
                                <div class="small text-muted"><?= htmlspecialchars($s['subtitle'] ?: '-') ?></div>
                            </td>
                            <td>
                                <span class="badge bg-light text-dark border"><?= htmlspecialchars($s['button_text']) ?></span>
                                <a href="<?= htmlspecialchars($s['button_link']) ?>" target="_blank" class="small ms-1"><i class="bi bi-link-45deg"></i> Link</a>
                            </td>
                            <td>
                                <?= $s['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="?sil=<?= $s['id'] ?>" class="btn btn-sm btn-outline-danger fw-bold shadow-sm" onclick="return confirm('Bu afişi 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-easel fs-1 d-block mb-3"></i>
                                Henüz vitrine eklenmiş bir slider bulunmuyor.
                            </td>
                        </tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

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