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

$stmt = $pdo->prepare("SELECT * FROM articles WHERE is_published = 1 AND (publish_start IS NULL OR publish_start <= CURDATE()) AND (publish_end IS NULL OR publish_end >= CURDATE()) ORDER BY created_at DESC");
$stmt->execute();
$articles = $stmt->fetchAll();

include 'includes/header.php';
?>

<main class="min-h-screen py-16 bg-gray-50">
    <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
        <h2 class="font-serif text-3xl md:text-4xl font-bold text-alsace-red mb-8 border-b-2 border-alsace-red pb-4">
            Toutes les actualités
        </h2>
        
        <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-8">
            <?php if (count($articles) > 0): ?>
                <?php foreach ($articles as $article): ?>
                <a href="article.php?slug=<?php echo htmlspecialchars($article['slug']); ?>" class="bg-white rounded-xl shadow-md overflow-hidden hover:shadow-lg transition group">
                    <?php if ($article['image']): ?>
                        <img src="uploads/<?php echo htmlspecialchars($article['image']); ?>" alt="<?php echo htmlspecialchars($article['title']); ?>" class="w-full h-48 object-cover group-hover:scale-105 transition duration-300">
                    <?php else: ?>
                        <img src="https://placehold.co/600x400/EAE0D5/7B2C2D?text=Actualité" alt="Actualité" class="w-full h-48 object-cover group-hover:scale-105 transition duration-300">
                    <?php endif; ?>
                    <div class="p-6">
                        <span class="text-xs font-bold text-alsace-red bg-red-50 px-2 py-1 rounded mb-2 inline-block"><?php echo htmlspecialchars($article['category']); ?></span>
                        <p class="text-sm text-gray-500 mb-2"><?php echo date('d/m/Y', strtotime($article['created_at'])); ?></p>
                        <h3 class="font-bold text-xl text-gray-800 mb-2 group-hover:text-alsace-red transition"><?php echo htmlspecialchars($article['title']); ?></h3>
                        <p class="text-gray-600 text-sm line-clamp-3"><?php echo htmlspecialchars($article['excerpt']); ?></p>
                    </div>
                </a>
                <?php endforeach; ?>
            <?php else: ?>
                <div class="col-span-full text-center py-12 bg-white rounded-xl shadow-sm border border-gray-100">
                    <p class="text-gray-500 text-lg">Aucune actualité disponible pour le moment.</p>
                </div>
            <?php endif; ?>
        </div>
    </div>
</main>

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