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

$slug = $_GET['slug'] ?? '';

if (empty($slug)) {
    header("HTTP/1.0 404 Not Found");
    include '404.php';
    exit;
}

// Blog yazısını bul
$stmt = $pdo->prepare("SELECT * FROM blog_posts WHERE slug = ? AND status = 'published'");
$stmt->execute([$slug]);
$post = $stmt->fetch();

if (!$post) {
    header("HTTP/1.0 404 Not Found");
    include '404.php';
    exit;
}

// Görüntülenme sayısını artır
$stmt = $pdo->prepare("UPDATE blog_posts SET views = views + 1 WHERE id = ?");
$stmt->execute([$post['id']]);

// SEO bilgileri
$page_title = $post['meta_title'] ?: $post['title'];
$meta_description = $post['meta_description'] ?: $post['excerpt'] ?: excerpt(strip_tags($post['content']), 160);
$meta_keywords = $post['meta_keywords'];

include 'includes/header.php';
?>

<div class="container mt-4">
    <div class="row">
        <div class="col-md-8">
            <article class="card mb-4">
                <?php if ($post['image']): ?>
                    <img src="<?= e(getImageUrl($post['image'])) ?>" class="card-img-top" alt="<?= e($post['title']) ?>" style="max-height: 400px; object-fit: cover;">
                <?php endif; ?>
                <div class="card-body">
                    <h1 class="h2 mb-3"><?= e($post['title']) ?></h1>
                    <p class="text-muted mb-4">
                        <i class="fas fa-calendar me-2"></i><?= formatDate($post['created_at'], 'd.m.Y H:i') ?>
                        <span class="ms-3"><i class="fas fa-eye me-2"></i><?= $post['views'] ?> görüntülenme</span>
                    </p>
                    <div class="content blog-content">
                        <?= $post['content'] ?>
                    </div>
                </div>
            </article>
            
            <div class="mb-4">
                <a href="<?= SITE_URL ?>/blog.php" class="btn btn-secondary">
                    <i class="fas fa-arrow-left me-2"></i>Blog'a Dön
                </a>
            </div>
        </div>
        
        <div class="col-md-4">
            <!-- Son Blog Yazıları -->
            <?php
            $stmt = $pdo->query("
                SELECT * FROM blog_posts 
                WHERE status = 'published' AND id != ?
                ORDER BY created_at DESC 
                LIMIT 5
            ");
            $stmt->execute([$post['id']]);
            $recentPosts = $stmt->fetchAll();
            ?>
            
            <?php if (!empty($recentPosts)): ?>
                <div class="card mb-4">
                    <div class="card-header">
                        <h5 class="mb-0">Son Yazılar</h5>
                    </div>
                    <div class="list-group list-group-flush">
                        <?php foreach ($recentPosts as $recentPost): ?>
                            <a href="<?= SITE_URL ?>/blog/<?= e($recentPost['slug']) ?>" class="list-group-item list-group-item-action">
                                <h6 class="mb-1"><?= e($recentPost['title']) ?></h6>
                                <small class="text-muted"><?= formatDate($recentPost['created_at'], 'd.m.Y') ?></small>
                            </a>
                        <?php endforeach; ?>
                    </div>
                </div>
            <?php endif; ?>
        </div>
    </div>
</div>

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



