<?php
$kategoriler = tabloOku("kategoriler", $conn);
$ilanlarQuery = mysqli_query($conn, "SELECT * FROM ilanlar ORDER BY ilan_id DESC LIMIT 6");
$mesajlarQuery = mysqli_query($conn, "SELECT * FROM mesajlar ORDER BY mesaj_id DESC LIMIT 3");
?>

<div class="row g-5">
    <!-- Left Column: Recent Blog Posts -->
    <div class="col-lg-8 border-end-lg pe-lg-5">
        <div class="d-flex justify-content-between align-items-center mb-4">
            <h5 class="fw-bold text-coffee mb-0"><i class="bi bi-journal-text text-coffee-gold me-2"></i> Son Yayınlanan Yazılar</h5>
            <a href="?sayfa=ilanlar" class="btn btn-sm btn-outline-coffee rounded-pill px-3">Tümünü Yönet</a>
        </div>
        <div class="table-responsive">
            <table class="table table-hover align-middle table-borderless">
                    <thead>
                        <tr class="table-light">
                            <th>Yazı Başlığı</th>
                            <th>Kategori</th>
                            <th class="text-end" style="width: 220px;">İşlemler</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        if (mysqli_num_rows($ilanlarQuery) == 0) {
                            echo '<tr><td colspan="3" class="text-center text-muted py-4">Henüz yayınlanmış bir yazı yok.</td></tr>';
                        }
                        while ($row = mysqli_fetch_array($ilanlarQuery, MYSQLI_BOTH)) {
                            $id = $row["ilan_id"];
                            $baslik = $row["ilan_baslik"];
                            $katID = $row["kategori_id"];
                            $katAd = isset($kategoriler[$katID]) ? $kategoriler[$katID]["kategori_ad"] : "Diğer";
                            
                            echo '<tr>';
                            echo '  <td><strong class="text-dark">' . $baslik . '</strong></td>';
                            echo '  <td><span class="badge rounded-pill bg-light text-coffee px-3 py-1.5 border">' . $katAd . '</span></td>';
                            echo '  <td class="text-end">';
                            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>';
                        }
                        ?>
                    </tbody>
            </table>
        </div>
    </div>

    <!-- Right Column: Messages & Quick Actions -->
    <div class="col-lg-4">
        <!-- Recent Messages -->
        <div class="mb-5">
            <div class="d-flex justify-content-between align-items-center mb-3">
                <h5 class="fw-bold text-coffee mb-0"><i class="bi bi-chat-left-quote text-coffee-gold me-2"></i> Son Mesajlar</h5>
                <a href="?sayfa=mesajlar" class="btn btn-sm btn-link text-coffee-gold p-0 text-decoration-none small">Tümü</a>
            </div>
            <div class="d-flex flex-column gap-3">
                <?php
                if (mysqli_num_rows($mesajlarQuery) == 0) {
                    echo '<div class="text-muted small py-3 text-center bg-light rounded-3">Gelen yeni mesaj bulunmuyor.</div>';
                }
                while ($row = mysqli_fetch_array($mesajlarQuery, MYSQLI_BOTH)) {
                    $mID = $row["mesaj_id"];
                    $ad = $row["gonderen_ad"];
                    $cevap = $row["cevap"];
                    
                    $statusIcon = (is_null($cevap) || trim($cevap) == "") 
                        ? '<i class="bi bi-envelope text-warning fs-5" title="Cevap Bekliyor"></i>' 
                        : '<i class="bi bi-envelope-open text-success fs-5" title="Cevaplandı"></i>';

                    echo '<div class="d-flex justify-content-between align-items-center p-3 rounded-3 bg-light">';
                    echo '  <div class="d-flex align-items-center gap-2">';
                    echo '    ' . $statusIcon;
                    echo '    <div>';
                    echo '      <div class="fw-bold text-dark small" style="font-size:0.85rem;">' . $ad . '</div>';
                    echo '    </div>';
                    echo '  </div>';
                    echo '  <a href="?sayfa=mesajdetay&mesajID=' . $mID . '" class="btn btn-sm btn-outline-coffee rounded-pill px-2.5 py-1" style="font-size:0.75rem;">Oku</a>';
                    echo '</div>';
                }
                ?>
            </div>
        </div>

        <!-- Quick Actions -->
        <div>
            <h5 class="fw-bold text-coffee mb-3"><i class="bi bi-lightning-fill text-coffee-gold me-2"></i> Hızlı İşlemler</h5>
            <div class="d-grid gap-2">
                <a href="?sayfa=ilanEkle" class="btn btn-coffee w-100 rounded-pill py-2.5 px-3 text-center small fw-bold"><i class="bi bi-plus-circle me-1 text-white"></i> Yeni Yazı Ekle</a>
                <a href="?sayfa=kategoriEkle" class="btn btn-outline-coffee w-100 rounded-pill py-2.5 px-3 text-center small fw-bold"><i class="bi bi-folder-plus me-1"></i> Yeni Kategori Ekle</a>
                <a href="../" target="_blank" class="btn btn-outline-secondary w-100 rounded-pill py-2.5 px-3 text-center small"><i class="bi bi-globe me-1"></i> Blog Önyüzünü Aç</a>
            </div>
        </div>
    </div>
</div>
