<?php
require_once 'config.php';

$actualites = [];
try {
    $stmt = $pdo->query("SELECT * FROM actualites WHERE statut = 'publie' ORDER BY date_publication DESC LIMIT 20");
    $actualites = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch(PDOException $e) {}

$evenements = [];
try {
    $stmt = $pdo->query("SELECT * FROM evenement WHERE statut IN ('a_venir', 'en_cours') ORDER BY date_debut ASC LIMIT 5");
    $evenements = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch(PDOException $e) {}

$galerie = [];
try {
    $stmt = $pdo->query("SELECT * FROM galerie ORDER BY ordre ASC, date_creation DESC LIMIT 8");
    $galerie = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch(PDOException $e) {}
?>
<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <title>Actualités | CPN Formation</title>
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; font-family: 'Poppins', sans-serif; }
        body { background: #f5f7fa; }
        .header { background: linear-gradient(135deg, #2f6d8c, #095175); color: white; padding: 60px 20px; text-align: center; }
        .container { max-width: 1200px; margin: 0 auto; padding: 40px 20px; }
        h2 { color: #1e293b; margin: 40px 0 20px; }
        .news-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(350px, 1fr)); gap: 25px; }
        .news-card { background: white; border-radius: 16px; overflow: hidden; box-shadow: 0 5px 20px rgba(0,0,0,0.1); }
        .news-image { width: 100%; height: 200px; object-fit: cover; }
        .news-content { padding: 20px; }
        .news-title { font-size: 18px; font-weight: 700; margin-bottom: 10px; }
        .news-date { font-size: 12px; color: #64748b; margin-bottom: 10px; }
        .events-list { display: flex; flex-direction: column; gap: 15px; }
        .event-item { background: white; border-radius: 12px; padding: 15px; border-left: 4px solid #2f6d8c; }
        .gallery-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 15px; }
        .gallery-img { width: 100%; height: 150px; object-fit: cover; border-radius: 12px; }
        @media (max-width: 768px) {
            .news-grid { grid-template-columns: 1fr; }
        }
    </style>
</head>
<body>
    <div class="header">
        <h1>📰 Actualités CPN Formation</h1>
        <p>Restez informé des dernières nouvelles et événements</p>
    </div>
    
    <div class="container">
        <h2>📰 Dernières actualités</h2>
        <div class="news-grid">
            <?php foreach($actualites as $news): ?>
            <div class="news-card">
                <?php if(!empty($news['image']) && file_exists('uploads/news/' . $news['image'])): ?>
                    <img src="uploads/news/<?php echo $news['image']; ?>" class="news-image" alt="">
                <?php else: ?>
                    <div class="news-image" style="background: #e2e8f0; display: flex; align-items: center; justify-content: center;">📰</div>
                <?php endif; ?>
                <div class="news-content">
                    <div class="news-title"><?php echo htmlspecialchars($news['titre']); ?></div>
                    <div class="news-date">📅 <?php echo date('d/m/Y', strtotime($news['date_publication'])); ?></div>
                    <p><?php echo nl2br(htmlspecialchars(substr($news['contenu'], 0, 150))); ?>...</p>
                </div>
            </div>
            <?php endforeach; ?>
        </div>
        
        <h2>📅 Événements à venir</h2>
        <div class="events-list">
            <?php foreach($evenements as $event): ?>
            <div class="event-item">
                <strong><?php echo htmlspecialchars($event['titre']); ?></strong><br>
                📅 <?php echo date('d/m/Y', strtotime($event['date_debut'])); ?> 
                <?php if($event['lieu']): ?>| 📍 <?php echo htmlspecialchars($event['lieu']); ?><?php endif; ?>
                <p style="margin-top: 5px; color: #64748b;"><?php echo htmlspecialchars(substr($event['description'] ?? '', 0, 100)); ?></p>
            </div>
            <?php endforeach; ?>
        </div>
        
        <h2>🖼️ Galerie</h2>
        <div class="gallery-grid">
            <?php foreach($galerie as $media): ?>
                <?php if($media['type'] == 'image' && !empty($media['fichier']) && file_exists('uploads/gallery/' . $media['fichier'])): ?>
                    <img src="uploads/gallery/<?php echo $media['fichier']; ?>" class="gallery-img" alt="<?php echo htmlspecialchars($media['titre']); ?>">
                <?php endif; ?>
            <?php endforeach; ?>
        </div>
    </div>
</body>
</html>