<?php if(hasRole('Administrateur') || hasRole('Editeur') || hasRole('Maire')): ?>
    <button class="btn-admin-news" onclick="document.getElementById('newsModal').style.display='flex'" style="margin-bottom: 20px;">+ Ajouter une Actualité</button>
<?php endif; ?>

<?php foreach($all_news as $n): ?>
    <div class="news-item">
        <div class="news-header">
            <div>
                <h3 style="margin:0; color:var(--nature-vert);"><?php echo htmlspecialchars($n['titre']); ?></h3>
                <span class="news-date">Publié le <?php echo date('d/m/Y', strtotime($n['date_creation'])); ?> par <?php echo htmlspecialchars($n['auteur_nom'] ?? 'La Mairie'); ?></span>
                
                <?php if(!empty($n['date_fin'])): ?>
                    <br><span style="font-size: 0.8em; color: #e74c3c; font-weight:bold;">(Sera effacée le <?php echo date('d/m/Y', strtotime($n['date_fin'])); ?>)</span>
                <?php endif; ?>
            </div>
            <?php if(isLogged() && ($n['auteur_id'] == $_SESSION['user_id'] || hasRole('Administrateur') || hasRole('Editeur') || hasRole('Maire'))): ?>
                <div class="action-group">
                    <button onclick="openEditNewsModal(<?php echo $n['id']; ?>, '<?php echo htmlspecialchars(addslashes($n['titre'])); ?>', '<?php echo $n['date_fin'] ?? ''; ?>')" class="btn-action btn-edit">Modifier</button>
                    <form method="POST" style="margin:0;" onsubmit="return confirm('Supprimer définitivement cette actualité et son image ?');">
                        <input type="hidden" name="delete_news_id" value="<?php echo $n['id']; ?>">
                        <button type="submit" class="btn-action btn-delete">Supprimer</button>
                    </form>
                </div>
            <?php endif; ?>
        </div>
        
        <div id="news_content_<?php echo $n['id']; ?>" style="display:none;"><?php echo htmlspecialchars($n['contenu']); ?></div>
        <p style="margin-top:15px; line-height:1.6;"><?php echo nl2br(htmlspecialchars($n['contenu'])); ?></p>
        
        <?php if(!empty($n['images'])): ?>
            <div class="news-gallery">
                <?php foreach(explode(',', $n['images']) as $img): ?>
                    <img src="images/news/<?php echo htmlspecialchars($img); ?>" class="news-thumb" onclick="openLightbox(this.src)">
                <?php endforeach; ?>
            </div>
        <?php endif; ?>
    </div>
<?php endforeach; ?>
<?php if(empty($all_news)) echo "<p style='text-align:center; color:#777;'>Aucune actualité pour le moment.</p>"; ?>

<!-- MODAL D'AJOUT D'ACTUALITÉ -->
<div id="newsModal" class="modal">
    <div class="modal-content">
        <h3 style="margin-top:0; color:var(--nature-vert);">Nouvelle Actualité</h3>
        <form method="POST" enctype="multipart/form-data">
            <label style="font-weight: bold; margin-bottom: 5px; display:block;">Titre :</label>
            <input type="text" name="news_title" placeholder="Ex: Travaux route du stade" required>
            
            <label style="font-weight: bold; margin-top: 10px; display:block;">Date de fin / Suppression automatique (Optionnel) :</label>
            <input type="date" name="news_fin">
            
            <label style="font-weight: bold; margin-top: 10px; display:block;">Image jointe (Optionnel) :</label>
            <input type="file" name="news_images[]" accept="image/*" multiple style="border: none; padding: 0; background: none;">
            
            <label style="font-weight: bold; margin-top: 10px; display:block;">Contenu du message :</label>
            <textarea name="news_content" rows="6" placeholder="Écrivez le détail ici..." required></textarea>
            
            <div style="display:flex; gap:10px; margin-top: 15px;">
                <button type="submit" name="add_news" class="btn-valider" style="flex:1; margin:0; padding:12px; border-radius:12px; border:none; background:#4a7c44; color:white; font-weight:bold; cursor:pointer;">Publier</button>
                <button type="button" class="btn-annuler" style="flex:1; margin:0; padding:12px; border-radius:12px; border:none; background:#999; color:white; font-weight:bold; cursor:pointer;" onclick="closeModal('newsModal')">Annuler</button>
            </div>
        </form>
    </div>
</div>

<!-- MODAL D'ÉDITION D'ACTUALITÉ -->
<div id="editNewsModal" class="modal">
    <div class="modal-content">
        <h3 style="margin-top:0; color:var(--nature-vert);">Modifier l'Actualité</h3>
        <form method="POST" enctype="multipart/form-data">
            <input type="hidden" name="edit_news_id" id="edit_news_id">
            
            <label style="font-weight: bold; margin-bottom: 5px; display:block;">Titre :</label>
            <input type="text" name="edit_news_title" id="edit_news_title" required>
            
            <label style="font-weight: bold; margin-top: 10px; display:block;">Date de fin / Suppression automatique (Optionnel) :</label>
            <input type="date" name="edit_news_fin" id="edit_news_fin">
            
            <label style="font-weight: bold; margin-top: 10px; display:block;">Remplacer l'image (Laissez vide pour conserver l'actuelle) :</label>
            <input type="file" name="edit_news_images[]" accept="image/*" multiple style="border: none; padding: 0; background: none;">

            <label style="font-weight: bold; margin-top: 10px; display:block;">Contenu du message :</label>
            <textarea name="edit_news_content" id="edit_news_content" rows="6" required></textarea>
            
            <div style="display:flex; gap:10px; margin-top: 15px;">
                <button type="submit" class="btn-valider" style="flex:1; margin:0; padding:12px; border-radius:12px; border:none; background:#4a7c44; color:white; font-weight:bold; cursor:pointer;">Enregistrer</button>
                <button type="button" class="btn-annuler" style="flex:1; margin:0; padding:12px; border-radius:12px; border:none; background:#999; color:white; font-weight:bold; cursor:pointer;" onclick="closeModal('editNewsModal')">Annuler</button>
            </div>
        </form>
    </div>
</div>