<?php
require_once '../../includes/db.php';
require_once '../header.php';
?>

<div class="container-fluid">
    <div class="card">
        <div class="card-header d-flex justify-content-between align-items-center">
            <h5 class="m-0">Blog Yazıları</h5>
            <div>
                <a href="kategori.php" class="btn btn-secondary btn-sm">Kategoriler</a>
                <a href="ekle.php" class="btn btn-primary btn-sm">Yeni Yazı Ekle</a>
            </div>
        </div>
        <div class="card-body">
            <table class="table table-bordered table-striped table-hover">
                <thead>
                    <tr>
                        <th width="50">ID</th>
                        <th width="80">Görsel</th>
                        <th>Başlık</th>
                        <th>Kategori</th>
                        <th>Görüntülenme</th>
                        <th>Durum</th>
                        <th width="150">İşlemler</th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                    // Yazıları ve ait oldukları kategori isimlerini çekiyoruz
                    $yazilar = $db->query("
                        SELECT b.*, c.name as category_name 
                        FROM blogs b 
                        LEFT JOIN blog_categories c ON b.category_id = c.id 
                        ORDER BY b.id DESC
                    ")->fetchAll(PDO::FETCH_ASSOC);

                    foreach ($yazilar as $yazi) {
                        ?>
                        <tr>
                            <td><?= $yazi['id'] ?></td>
                            <td>
                                <?php if (!empty($yazi['image'])) { ?>
                                    <img src="../../<?= htmlspecialchars($yazi['image']) ?>" alt="Görsel" style="width: 50px; height: 50px; object-fit: cover; border-radius: 4px;">
                                <?php } else { ?>
                                    <span class="text-muted">Yok</span>
                                <?php } ?>
                            </td>
                            <td>
                                <strong><?= htmlspecialchars($yazi['title']) ?></strong><br>
                                <small class="text-muted"><?= htmlspecialchars($yazi['slug']) ?></small>
                            </td>
                            <td><?= $yazi['category_name'] ? htmlspecialchars($yazi['category_name']) : '<span class="text-danger">Kategorisiz</span>' ?></td>
                            <td><?= $yazi['view_count'] ?> Kez</td>
                            <td>
                                <?php if ($yazi['status'] == 1) { ?>
                                    <span class="badge bg-success">Aktif</span>
                                <?php } else { ?>
                                    <span class="badge bg-danger">Pasif</span>
                                <?php } ?>
                            </td>
                            <td>
                                <a href="duzenle.php?id=<?= $yazi['id'] ?>" class="btn btn-sm btn-warning text-white">Düzenle</a>
                                <a href="sil.php?id=<?= $yazi['id'] ?>" class="btn btn-sm btn-danger" onclick="return confirm('Yazıyı silmek istediğinize emin misiniz?');">Sil</a>
                            </td>
                        </tr>
                    <?php } ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

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