<?php

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

$seciliKategori = @$_GET["kategoriFilterID"];

if (!is_null($seciliKategori) && $seciliKategori !== "") {
    $sql = "SELECT * FROM ilanlar WHERE kategori_id = " . intval($seciliKategori) . " ORDER BY ilan_id DESC";
} else {
    $sql = "SELECT * FROM ilanlar ORDER BY ilan_id DESC";
}
$ornek = mysqli_query($conn, $sql);
?>

<div class="d-flex flex-column flex-md-row justify-content-between align-items-md-center gap-3 mb-4">
    <h3 class="fw-bold text-coffee mb-0"><i class="bi bi-journal-richtext text-coffee-gold me-2"></i> Yazı Yönetimi</h3>
    <div class="d-flex align-items-center gap-2">
        <select class="form-select rounded-pill px-3 py-2 border-secondary" style="width: 220px;" onchange="location = this.value;">
            <option value="?sayfa=ilanlar">Tüm Kategoriler</option>
            <?php
            foreach ($kategoriler as $kat) {
                $katID = $kat["kategori_id"];
                $katAd = $kat["kategori_ad"];
                $selected = ($seciliKategori == $katID) ? 'selected' : '';
                echo "<option value=\"?sayfa=ilanlar&kategoriFilterID=$katID\" $selected>$katAd</option>";
            }
            ?>
        </select>
        <a href="?sayfa=ilanEkle" class="btn btn-coffee rounded-pill px-4 py-2"><i class="bi bi-plus-circle me-2 text-white"></i> Yeni Yazı Ekle</a>
    </div>
</div>

<?php
echo "<div class='table-responsive border border-light rounded-4 shadow-sm bg-white p-3'>
    <table class=\"table table-hover align-middle table-borderless mb-0\" >
    <thead>
    <tr class=\"table-light border-bottom\">
      <th scope=\"col\" class=\"py-3\">Yazı Başlığı</th>
      <th scope=\"col\" class=\"py-3\">Kategori</th>
      <th scope=\"col\" class=\"py-3\">Yazar</th>
      <th scope=\"col\" class=\"py-3 text-center\" style=\"width: 240px;\">İşlemler</th>
      </tr>
  </thead><tbody>";
while ($row = mysqli_fetch_array($ornek, MYSQLI_BOTH)) {
    $id = $row[0];
    $baslik = $row["ilan_baslik"];
    $sehir = $row["ilan_sehir"];
    $katID = $row["kategori_id"];
    $katAd = isset($kategoriler[$katID]) ? $kategoriler[$katID]["kategori_ad"] : "Diğer";

    echo "<tr class=\"border-bottom\">";
    echo "<td class=\"py-3\"><strong class=\"text-dark\">$baslik</strong></td>";
    echo "<td class=\"py-3\"><span class=\"badge rounded-pill bg-light text-coffee border px-3 py-1.5\">$katAd</span></td>";
    echo "<td class=\"py-3 text-muted small\">$sehir</td>";
    echo "<td class=\"py-3 text-center\">";
    echo "  <a href=\"?sayfa=ilanEkle&ilanID=$id\" class=\"btn btn-sm btn-outline-coffee rounded-pill px-3 py-1.5 me-1\"><i class=\"bi bi-pencil me-1\"></i> Düzenle</a>";
    echo "  <a href=\"?sayfa=ilanSil&ilanID=$id\" onclick=\"return confirm('Bu yazıyı silmek istediğinize emin misiniz?');\" class=\"btn btn-sm btn-outline-danger rounded-pill px-3 py-1.5\"><i class=\"bi bi-trash me-1\"></i> Sil</a>";
    echo "</td>";
    echo "</tr>";
}
echo "</tbody></table></div>";
