<?php

$kategoriler = tabloOku("kategoriler", $conn);

$kategoriID = @$_GET["kategoriID"];
$katAd = isset($kategoriler[$kategoriID]) ? $kategoriler[$kategoriID]["kategori_ad"] : "Bilinmeyen";

$sql = "SELECT * FROM ilanlar WHERE kategori_id=$kategoriID ORDER BY ilan_id DESC";
$ornek = mysqli_query($conn, $sql);
?>

<div class="d-flex justify-content-between align-items-center mb-4 mt-2">
    <h2 class="fw-bold text-coffee"><i class="bi bi-folder2-open text-coffee-gold"></i> <?php echo $katAd; ?> Kategorisi</h2>
    <a href="?sayfa=main" class="btn btn-outline-coffee btn-sm rounded-pill px-3"><i class="bi bi-arrow-left"></i> Geri Dön</a>
</div>

<div class="row row-cols-1 row-cols-md-3 g-4">
    <?php
    if (mysqli_num_rows($ornek) == 0) {
        echo '<div class="col-12"><div class="alert alert-info bg-white text-coffee">Bu kategoride henüz bir blog yazısı bulunamadı.</div></div>';
    }
    while ($row = mysqli_fetch_array($ornek, MYSQLI_BOTH)) {
        $ilanID = $row["ilan_id"];
        $baslik = $row["ilan_baslik"];
        $aciklama = $row["ilan_aciklama"];
        $fiyat = $row["ilan_fiyat"];
        $sehir = $row["ilan_sehir"];
        $tarih = $row["ilan_tarih"];
        
        // Get short snippet of description (e.g. 90 chars)
        $ozet = mb_substr(strip_tags($aciklama), 0, 90) . (mb_strlen($aciklama) > 90 ? "..." : "");
        
        // Coffee blog card
        echo '<div class="col">';
        echo '  <div class="card h-100 border-0 shadow-sm rounded-4 bg-white">';
        echo '    <div class="card-body d-flex flex-column p-4">';
        echo '      <div class="mb-2">';
        echo '        <span class="badge rounded-pill bg-coffee text-white px-3 py-1.5" style="font-size: 0.75rem;">' . $katAd . '</span>';
        echo '      </div>';
        echo '      <h3 class="card-title h5 fw-bold mb-3"><a href="?sayfa=ilandetay&ilanID=' . $ilanID . '" class="text-decoration-none text-dark hover-coffee">' . $baslik . '</a></h3>';
        echo '      <p class="card-text text-muted flex-grow-1" style="font-size: 0.9rem; line-height: 1.5; margin-bottom: 1.5rem;">' . $ozet . '</p>';
        echo '      <div class="mt-auto">';
        echo '        <a href="?sayfa=ilandetay&ilanID=' . $ilanID . '" class="text-coffee-gold fw-bold text-decoration-none hover-coffee d-inline-flex align-items-center gap-1" style="font-size: 0.9rem;">Devamını Oku <i class="bi bi-arrow-right"></i></a>';
        echo '      </div>';
        echo '    </div>';
        echo '  </div>';
        echo '</div>';
    }
    ?>
</div>
