<?php
/**
 * Admin - Kurumsal Sayfalar Listesi
 * Konum: /magaza/admin/sayfalar/index.php
 */

require_once '../../includes/db.php';
require_once '../../includes/functions.php';
adminCheck();

$mesaj = '';

// Sayfa Silme İşlemi
if (isset($_GET['sil']) && is_numeric($_GET['sil'])) {
    $sil_id = (int)$_GET['sil'];
    try {
        $db->prepare("DELETE FROM pages WHERE id = ?")->execute([$sil_id]);
        $mesaj = "<div class='alert alert-success shadow-sm'><i class='bi bi-trash-fill me-2'></i> Sayfa başarıyla silindi.</div>";
    } catch (Exception $e) {
        $mesaj = "<div class='alert alert-danger shadow-sm'>Hata: Sayfa silinemedi. " . $e->getMessage() . "</div>";
    }
}

// Sayfaları Çek
$sayfalar = $db->query("SELECT * FROM pages 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-file-earmark-richtext text-primary me-2"></i>Kurumsal Sayfalar</h2>
    <a href="ekle.php" class="btn btn-primary fw-bold shadow-sm">
        <i class="bi bi-plus-lg me-1"></i> Yeni Sayfa Ekle
    </a>
</div>

<?= $mesaj ?>

<div class="card shadow-sm border-0 rounded-4 overflow-hidden">
    <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">Sayfa Başlığı</th>
                        <th>Bağlantı (Slug)</th>
                        <th>Durum</th>
                        <th class="text-end pe-4">İşlemler</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if(count($sayfalar) > 0): ?>
                        <?php foreach($sayfalar as $s): ?>
                        <tr>
                            <td class="ps-4 fw-bold text-dark"><?= htmlspecialchars($s['title']) ?></td>
                            <td><span class="badge bg-light text-secondary border">/sayfa.php?slug=<?= htmlspecialchars($s['slug']) ?></span></td>
                            <td>
                                <?php if($s['status'] == 1): ?>
                                    <span class="badge bg-success rounded-pill px-3">Aktif</span>
                                <?php else: ?>
                                    <span class="badge bg-secondary rounded-pill px-3">Pasif</span>
                                <?php endif; ?>
                            </td>
                            <td class="text-end pe-4">
                                <a href="../../sayfa.php?slug=<?= $s['slug'] ?>" target="_blank" class="btn btn-sm btn-outline-info fw-bold me-1" title="Görüntüle"><i class="bi bi-eye"></i></a>
                                <a href="duzenle.php?id=<?= $s['id'] ?>" class="btn btn-sm btn-outline-primary fw-bold me-1" title="Düzenle"><i class="bi bi-pencil-square"></i></a>
                                <a href="?sil=<?= $s['id'] ?>" class="btn btn-sm btn-outline-danger fw-bold" onclick="return confirm('Bu sayfayı 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-file-earmark-x fs-1 d-block mb-3"></i>
                                Henüz hiçbir sayfa eklenmemiş.
                            </td>
                        </tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

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