<?php
require_once 'includes/db.php';
require_once 'includes/functions.php';

$kok_dizin = "/magaza/";

if (!isset($_GET['slug']) || empty($_GET['slug'])) {
    header("Location: " . $kok_dizin . "blog.php");
    exit;
}
$kategori_slug = trim($_GET['slug']);

// Kategoriyi Bul
$kat_stmt = $db->prepare("SELECT * FROM blog_categories WHERE slug = ? AND status = 1");
$kat_stmt->execute([$kategori_slug]);
$aktif_kategori = $kat_stmt->fetch(PDO::FETCH_ASSOC);

if (!$aktif_kategori) {
    header("Location: " . $kok_dizin . "blog.php");
    exit;
}

$page_title = $aktif_kategori['name'] . " | Blog | SINAVKOCU Mağaza";
require_once 'header.php';

// Sayfalama (Pagination) Ayarları
$sayfada = 9; 
$sorgu = $db->prepare("SELECT COUNT(id) FROM blogs WHERE status = 1 AND category_id = ?");
$sorgu->execute([$aktif_kategori['id']]);
$toplam_icerik = $sorgu->fetchColumn();
$toplam_sayfa = ceil($toplam_icerik / $sayfada);
$sayfa = isset($_GET['sayfa']) ? (int) $_GET['sayfa'] : 1;
if($sayfa < 1) $sayfa = 1;
if($sayfa > $toplam_sayfa && $toplam_sayfa > 0) $sayfa = $toplam_sayfa;
$limit = ($sayfa - 1) * $sayfada;

// Aktif kategorideki blog yazılarını çek
$yazilar = $db->prepare("
    SELECT b.*, c.name as category_name, c.slug as category_slug 
    FROM blogs b 
    LEFT JOIN blog_categories c ON b.category_id = c.id 
    WHERE b.status = 1 AND b.category_id = :kat_id
    ORDER BY b.id DESC LIMIT :limit, :sayfada
");
$yazilar->bindValue(':kat_id', $aktif_kategori['id'], PDO::PARAM_INT);
$yazilar->bindValue(':limit', (int) $limit, PDO::PARAM_INT);
$yazilar->bindValue(':sayfada', (int) $sayfada, PDO::PARAM_INT);
$yazilar->execute();
$blog_listesi = $yazilar->fetchAll(PDO::FETCH_ASSOC);

// Sağ Taraf İçin Tüm Kategorileri Çek
$kategoriler = $db->query("SELECT * FROM blog_categories WHERE status = 1 ORDER BY name ASC")->fetchAll(PDO::FETCH_ASSOC);
?>

<main class="container py-4 text-dark">
    <nav aria-label="breadcrumb" class="mb-4">
        <ol class="breadcrumb mb-0">
            <li class="breadcrumb-item"><a href="<?= $kok_dizin ?>index.php" class="text-decoration-none"><i class="bi bi-house-door"></i> Ana Sayfa</a></li>
            <li class="breadcrumb-item"><a href="<?= $kok_dizin ?>blog.php" class="text-decoration-none">Blog</a></li>
            <li class="breadcrumb-item active" aria-current="page"><?= htmlspecialchars($aktif_kategori['name']) ?></li>
        </ol>
    </nav>

    <div class="row">
        <div class="col-lg-9 mb-4">
            <div class="d-flex justify-content-between align-items-center border-bottom pb-2 mb-4">
                <h1 class="h3 fw-bold mb-0 text-primary"><?= htmlspecialchars($aktif_kategori['name']) ?> Kategorisi</h1>
            </div>

            <?php if(count($blog_listesi) > 0): ?>
                <div class="row row-cols-1 row-cols-md-2 row-cols-xl-3 g-4">
                    <?php foreach($blog_listesi as $yazi): 
                        $gorsel = !empty($yazi['image']) ? $yazi['image'] : 'assets/img/no-image.jpg';
                        $ozet = mb_substr(strip_tags($yazi['content']), 0, 110) . '...';
                    ?>
                    <div class="col">
                        <div class="card h-100 border-0 shadow-sm product-card transition rounded-4 overflow-hidden">
                            <div class="position-relative overflow-hidden bg-light">
                                <a href="<?= $kok_dizin ?>blog/<?= $yazi['slug'] ?>">
                                    <img src="<?= $kok_dizin ?><?= htmlspecialchars($gorsel) ?>" class="card-img-top w-100" alt="<?= htmlspecialchars($yazi['title']) ?>" style="aspect-ratio: 1200/630; object-fit: cover;">
                                </a>
                            </div>
                            <div class="card-body d-flex flex-column bg-white p-3">
                                <div class="text-muted small mb-2 d-flex justify-content-between">
                                    <span><i class="bi bi-calendar-event me-1"></i> <?= date('d.m.Y', strtotime($yazi['created_at'])) ?></span>
                                    <span><i class="bi bi-eye me-1"></i> <?= $yazi['view_count'] ?></span>
                                </div>
                                <h5 class="card-title fw-bold fs-6 mb-2" style="height: 40px; overflow: hidden;">
                                    <a href="<?= $kok_dizin ?>blog/<?= $yazi['slug'] ?>" class="text-dark text-decoration-none">
                                        <?= htmlspecialchars($yazi['title']) ?>
                                    </a>
                                </h5>
                                <p class="card-text text-muted small flex-grow-1"><?= $ozet ?></p>
                                <div class="mt-auto pt-2 border-top">
                                    <a href="<?= $kok_dizin ?>blog/<?= $yazi['slug'] ?>" class="btn btn-outline-primary btn-sm w-100 fw-bold rounded-pill">Devamını Oku</a>
                                </div>
                            </div>
                        </div>
                    </div>
                    <?php endforeach; ?>
                </div>

                <?php if($toplam_sayfa > 1): ?>
                <nav class="mt-5">
                    <ul class="pagination justify-content-center">
                        <?php for($i = 1; $i <= $toplam_sayfa; $i++): ?>
                            <li class="page-item <?= ($i == $sayfa) ? 'active' : '' ?>">
                                <a class="page-link shadow-sm" href="?slug=<?= $kategori_slug ?>&sayfa=<?= $i ?>"><?= $i ?></a>
                            </li>
                        <?php endfor; ?>
                    </ul>
                </nav>
                <?php endif; ?>

            <?php else: ?>
                <div class="alert alert-secondary text-center py-5 shadow-sm border-0 rounded-4">
                    <i class="bi bi-folder-x fs-1 text-muted d-block mb-3"></i>
                    <h5 class="fw-bold">Bu kategoride henüz yazı yok.</h5>
                </div>
            <?php endif; ?>
        </div>

        <div class="col-lg-3">
            <div class="card shadow-sm border-0 rounded-4 mb-4">
                <div class="card-header bg-white border-bottom fw-bold py-3">
                    <i class="bi bi-folder text-primary me-2"></i> Diğer Kategoriler
                </div>
                <div class="list-group list-group-flush rounded-bottom-4">
                    <?php foreach($kategoriler as $kat): 
                        $kat_say_stmt = $db->prepare("SELECT COUNT(*) FROM blogs WHERE category_id = ? AND status = 1");
                        $kat_say_stmt->execute([$kat['id']]);
                        $kat_sayi = $kat_say_stmt->fetchColumn();
                        
                        $active_class = ($kat['id'] == $aktif_kategori['id']) ? 'active bg-primary border-primary' : '';
                    ?>
                        <a href="<?= $kok_dizin ?>blog/kategori/<?= $kat['slug'] ?>" class="list-group-item list-group-item-action d-flex justify-content-between align-items-center <?= $active_class ?>">
                            <?= htmlspecialchars($kat['name']) ?>
                            <span class="badge <?= ($kat['id'] == $aktif_kategori['id']) ? 'bg-white text-primary' : 'bg-light text-dark' ?> rounded-pill border"><?= $kat_sayi ?></span>
                        </a>
                    <?php endforeach; ?>
                </div>
            </div>
            
            <div class="card bg-primary text-white shadow-sm border-0 rounded-4 text-center p-4">
                <i class="bi bi-rocket-takeoff fs-1 mb-2"></i>
                <h5 class="fw-bold">Sınavlara Hazır Mısın?</h5>
                <p class="small text-white-50">Özel deneme paketlerimizle başarıyı yakala.</p>
                <a href="<?= $kok_dizin ?>deneme-paketi-olustur.php" class="btn btn-light btn-sm fw-bold rounded-pill text-primary">Paketini Oluştur</a>
            </div>
            
        </div>
    </div>
</main>

<style>
.product-card { transition: all 0.3s ease; }
.product-card:hover { transform: translateY(-7px); box-shadow: 0 14px 28px rgba(0,0,0,0.1) !important; }
</style>

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