<?php
require_once 'includes/db_connect.php';
$sql = "SELECT * FROM blog_posts ORDER BY created_at DESC";
$result = $conn->query($sql);

$page_title = "Blog & Insights";
include 'includes/header.php';
?>

<section class="bg-brand-blue py-20 text-white text-center">
    <div class="container mx-auto px-6">
        <h1 class="text-4xl font-bold mb-4">HR Insights & News</h1>
        <p class="text-xl text-blue-200">Latest trends in recruitment, talent management, and corporate culture.</p>
    </div>
</section>

<section class="py-16 bg-gray-50">
    <div class="container mx-auto px-6">
        <div class="grid grid-cols-1 md:grid-cols-3 gap-8">
            <?php if($result->num_rows > 0): ?>
                <?php while($row = $result->fetch_assoc()): ?>
                    <div class="bg-white rounded-xl shadow-md overflow-hidden hover:shadow-xl transition flex flex-col h-full">
                        <?php if($row['image']): ?>
                            <img src="<?php echo htmlspecialchars($row['image']); ?>" alt="<?php echo htmlspecialchars($row['title']); ?>" class="w-full h-48 object-cover">
                        <?php else: ?>
                            <div class="w-full h-48 bg-gray-200 flex items-center justify-center text-gray-400"><i class="fas fa-newspaper text-3xl"></i></div>
                        <?php endif; ?>
                        
                        <div class="p-6 flex flex-col flex-grow">
                            <span class="text-xs text-blue-600 font-bold uppercase mb-2"><?php echo date("M d, Y", strtotime($row['created_at'])); ?></span>
                            <h3 class="text-xl font-bold text-gray-900 mb-3 leading-tight hover:text-blue-900">
                                <a href="post.php?id=<?php echo $row['id']; ?>"><?php echo htmlspecialchars($row['title']); ?></a>
                            </h3>
                            <p class="text-gray-600 text-sm mb-4 flex-grow">
                                <?php echo substr(strip_tags($row['content']), 0, 100); ?>...
                            </p>
                            <a href="post.php?id=<?php echo $row['id']; ?>" class="text-blue-900 font-bold text-sm hover:underline mt-auto">Read Article &rarr;</a>
                        </div>
                    </div>
                <?php endwhile; ?>
            <?php else: ?>
                <div class="col-span-3 text-center py-12">
                    <p class="text-gray-500 text-lg">No articles published yet. Check back soon!</p>
                </div>
            <?php endif; ?>
        </div>
    </div>
</section>

<?php include 'includes/footer.php'; ?>