<?php
require_once 'config/config.php';

// Sayfalama
$page = max(1, intval($_GET['page'] ?? 1));
$perPage = 9;
$offset = ($page - 1) * $perPage;

// Toplam blog yazısı sayısı
$stmt = $pdo->query("SELECT COUNT(*) as total FROM blog_posts WHERE status = 'published'");
$totalPosts = $stmt->fetch()['total'];
$totalPages = ceil($totalPosts / $perPage);

// Blog yazılarını al
$stmt = $pdo->prepare("
    SELECT * FROM blog_posts 
    WHERE status = 'published' 
    ORDER BY created_at DESC 
    LIMIT ? OFFSET ?
");
$stmt->execute([$perPage, $offset]);
$posts = $stmt->fetchAll();

$page_title = 'Blog';
include 'includes/header.php';
?>

<div class="container mt-4">
    <div class="row mb-4">
        <div class="col-12">
            <h1>Blog Yazıları</h1>
            <p class="text-muted"><?= $totalPosts ?> yazı bulundu</p>
        </div>
    </div>
    
    <?php if (empty($posts)): ?>
        <div class="alert alert-info">
            <i class="fas fa-info-circle me-2"></i>Henüz blog yazısı yayınlanmamış.
        </div>
    <?php else: ?>
        <div class="row">
            <?php foreach ($posts as $post): ?>
                <div class="col-md-4 mb-4">
                    <div class="card h-100">
                        <?php if ($post['image']): ?>
                            <img src="<?= e(getImageUrl($post['image'])) ?>" class="card-img-top" alt="<?= e($post['title']) ?>" style="height: 200px; object-fit: cover;">
                        <?php endif; ?>
                        <div class="card-body">
                            <h5 class="card-title"><?= e($post['title']) ?></h5>
                            <p class="card-text"><?= e($post['excerpt'] ?: excerpt(strip_tags($post['content']), 100)) ?></p>
                            <a href="<?= SITE_URL ?>/blog/<?= e($post['slug']) ?>" class="btn btn-primary">Devamını Oku</a>
                        </div>
                        <div class="card-footer text-muted small">
                            <?= formatDate($post['created_at'], 'd.m.Y') ?>
                        </div>
                    </div>
                </div>
            <?php endforeach; ?>
        </div>
        
        <!-- Sayfalama -->
        <?php if ($totalPages > 1): ?>
            <nav aria-label="Blog sayfalama">
                <ul class="pagination justify-content-center">
                    <?php if ($page > 1): ?>
                        <li class="page-item">
                            <a class="page-link" href="?page=<?= $page - 1 ?>">Önceki</a>
                        </li>
                    <?php endif; ?>
                    
                    <?php for ($i = 1; $i <= $totalPages; $i++): ?>
                        <?php if ($i == 1 || $i == $totalPages || ($i >= $page - 2 && $i <= $page + 2)): ?>
                            <li class="page-item <?= $i == $page ? 'active' : '' ?>">
                                <a class="page-link" href="?page=<?= $i ?>"><?= $i ?></a>
                            </li>
                        <?php elseif ($i == $page - 3 || $i == $page + 3): ?>
                            <li class="page-item disabled">
                                <span class="page-link">...</span>
                            </li>
                        <?php endif; ?>
                    <?php endfor; ?>
                    
                    <?php if ($page < $totalPages): ?>
                        <li class="page-item">
                            <a class="page-link" href="?page=<?= $page + 1 ?>">Sonraki</a>
                        </li>
                    <?php endif; ?>
                </ul>
            </nav>
        <?php endif; ?>
    <?php endif; ?>
</div>

<?php include 'includes/footer.php'; ?>




