<div class="blog-form">
    <h4 style="margin-top:0; color:var(--nature-vert);">Partager une idée avec le village</h4>
    <form method="POST" style="margin:0;">
        <textarea name="blog_msg" rows="3" placeholder="Écrivez votre message ici..." required></textarea>
        <button type="submit" name="send_blog" class="btn-send">Publier mon message</button>
    </form>
</div>

<?php foreach($blog_messages as $msg): ?>
    <div class="blog-bubble">
        <div class="blog-meta">
            <span><strong><?php echo htmlspecialchars($msg['nom']); ?></strong> • <?php echo date('d/m/Y H:i', strtotime($msg['date_creation'])); ?></span>
            
            <?php if($msg['utilisateur_id'] == $_SESSION['user_id'] || hasRole('Administrateur') || hasRole('Editeur') || hasRole('Maire') || hasRole('Adjoint')): ?>
                <div class="action-group">
                    <button onclick="openEditBlogModal(<?php echo $msg['id']; ?>)" class="btn-action btn-edit">Modifier</button>
                    <form method="POST" style="margin:0;" onsubmit="return confirm('Supprimer ce message ?');">
                        <input type="hidden" name="delete_post_id" value="<?php echo $msg['id']; ?>">
                        <button type="submit" class="btn-action btn-delete">Supprimer</button>
                    </form>
                </div>
            <?php endif; ?>
        </div>

        <div id="blog_content_<?php echo $msg['id']; ?>" style="display:none;"><?php echo htmlspecialchars($msg['message']); ?></div>
        <div style="margin-bottom: 15px;"><?php echo nl2br(htmlspecialchars($msg['message'])); ?></div>

        <div class="comments-section">
            <?php foreach($msg['commentaires'] as $com): ?>
                <div class="comment-item">
                    <strong><?php echo htmlspecialchars($com['nom']); ?> :</strong> <?php echo htmlspecialchars($com['commentaire']); ?>
                </div>
            <?php endforeach; ?>
            <form method="POST" class="comment-form">
                <input type="hidden" name="msg_id" value="<?php echo $msg['id']; ?>">
                <input type="text" name="comment_text" placeholder="Ajouter un commentaire..." required>
                <button type="submit" name="send_comment" class="btn-com">Envoyer</button>
            </form>
        </div>
    </div>
<?php endforeach; ?>
<?php if(empty($blog_messages)) echo "<p style='text-align:center; color:#777;'>Aucun message sur le blog.</p>"; ?>

<div id="editBlogModal" class="modal">
    <div class="modal-content">
        <h3 style="margin-top:0; color:var(--nature-vert);">Modifier le billet</h3>
        <form method="POST">
            <input type="hidden" name="edit_post_id" id="edit_post_id">
            <textarea name="edit_post_content" id="edit_post_content" rows="5" required></textarea>
            <div style="display:flex; gap:10px;">
                <button type="submit" class="btn-valider" style="flex:1; margin:0;">Enregistrer</button>
                <button type="button" class="btn-annuler" style="flex:1; margin:0;" onclick="closeModal('editBlogModal')">Annuler</button>
            </div>
        </form>
    </div>
</div>