<?php if($is_admin_legal): ?>
    <div style="display: flex; gap: 10px; margin-bottom: 20px; flex-wrap: wrap;">
        <button class="btn-admin-news" onclick="document.getElementById('addLegalModal').style.display='flex'" style="background-color: #2d3436; margin: 0;">+ Ajouter un document</button>
        <button class="btn-admin-news" onclick="document.getElementById('manageTypesModal').style.display='flex'" style="background-color: #607d8b; margin: 0;">⚙️ Gérer les catégories</button>
    </div>
<?php endif; ?>

<?php foreach($all_legales as $l): 
    $is_future = (strtotime($l['date_debut']) > time());
?>
    <div class="news-item" style="border-left-color: #2d3436; background-color: #f8f9fa;">
        <span class="legal-badge <?php echo $is_future ? 'future' : ''; ?>">
            <?php echo htmlspecialchars($l['type_publication']); ?>
            <?php if($is_future) echo " (Programmé)"; ?>
        </span>
        
        <div class="news-header">
            <div>
                <h3 style="margin:0; color:#2d3436;"><?php echo htmlspecialchars($l['titre']); ?></h3>
                <span class="news-date">
                    <?php echo $is_future ? 'Sera publié le ' : 'Publié le '; ?>
                    <strong><?php echo date('d/m/Y', strtotime($l['date_debut'])); ?></strong> 
                    par <?php echo htmlspecialchars($l['auteur_nom'] ?? 'La Mairie'); ?>
                    <?php if(!empty($l['date_fin'])) echo " (Jusqu'au " . date('d/m/Y', strtotime($l['date_fin'])) . ")"; ?>
                </span>
            </div>
            
            <?php if($is_admin_legal): ?>
                <div class="action-group">
                    <button onclick="openEditLegalModal(<?php echo $l['id']; ?>, '<?php echo htmlspecialchars(addslashes($l['titre'])); ?>', '<?php echo htmlspecialchars(addslashes($l['type_publication'])); ?>', '<?php echo $l['date_debut']; ?>', '<?php echo $l['date_fin']; ?>')" class="btn-action btn-edit">Modifier</button>
                    <form method="POST" style="margin:0;" onsubmit="return confirm('Supprimer ce document officiel et ses pièces jointes ?');">
                        <input type="hidden" name="delete_legal_id" value="<?php echo $l['id']; ?>">
                        <button type="submit" class="btn-action btn-delete">Supprimer</button>
                    </form>
                </div>
            <?php endif; ?>
        </div>
        
        <div id="legal_content_<?php echo $l['id']; ?>" style="display:none;"><?php echo htmlspecialchars($l['contenu']); ?></div>
        <p style="margin-top:15px; line-height:1.6;"><?php echo nl2br(htmlspecialchars($l['contenu'])); ?></p>
        
        <?php if(!empty($l['fichiers_joints'])): ?>
            <div style="margin-top: 20px; border-top: 1px dashed #ccc; padding-top: 15px;">
                <strong style="color: #666; font-size: 0.9em; display:block; margin-bottom: 5px;">Pièces jointes :</strong>
                <?php foreach(explode(',', $l['fichiers_joints']) as $file): 
                    $ext = pathinfo($file, PATHINFO_EXTENSION);
                    $path = 'datas/infos_legales/' . $file;
                    if ($ext === 'pdf'):
                ?>
                    <a href="<?php echo htmlspecialchars($path); ?>" target="_blank" class="file-link">📄 Télécharger le PDF</a>
                <?php else: ?>
                    <img src="<?php echo htmlspecialchars($path); ?>" class="news-thumb" onclick="openLightbox(this.src)" style="vertical-align: top; margin-top: 10px;">
                <?php endif; endforeach; ?>
            </div>
        <?php endif; ?>
    </div>
<?php endforeach; ?>
<?php if(empty($all_legales)) echo "<p style='text-align:center; color:#777;'>Aucune publication officielle en cours.</p>"; ?>

<div id="addLegalModal" class="modal">
    <div class="modal-content">
        <h3 style="margin-top:0; color:#2d3436;">Nouvelle Info Légale</h3>
        <form method="POST" enctype="multipart/form-data">
            <select name="legal_type" required>
                <option value="">-- Choisir le type de document --</option>
                <?php foreach($all_types as $t): ?>
                    <option value="<?php echo htmlspecialchars($t['nom_type']); ?>"><?php echo htmlspecialchars($t['nom_type']); ?></option>
                <?php endforeach; ?>
            </select>
            <input type="text" name="legal_title" placeholder="Titre de la publication..." required>
            <div class="form-grid-dates">
                <div>
                    <label style="font-size: 0.85em; color: #666; font-weight: bold;">Date de publication :</label>
                    <input type="date" name="legal_debut" value="<?php echo date('Y-m-d'); ?>" required>
                </div>
                <div>
                    <label style="font-size: 0.85em; color: #666; font-weight: bold;">Date de fin (Optionnel) :</label>
                    <input type="date" name="legal_fin">
                </div>
            </div>
            <label style="font-size: 0.85em; color: #666; font-weight: bold; margin-top: 10px; display: block;">Pièces jointes (Max 5Mo, PDF/JPG/PNG/WEBP) :</label>
            <input type="file" name="legal_files[]" accept=".pdf, image/jpeg, image/png, image/webp" multiple>
            <textarea name="legal_content" rows="5" placeholder="Saisir le contenu ou un résumé..." required></textarea>
            <div style="display:flex; gap:10px;">
                <button type="submit" name="add_legal" class="btn-valider" style="flex:1; margin:0; background-color:#2d3436;">Publier</button>
                <button type="button" class="btn-annuler" style="flex:1; margin:0;" onclick="closeModal('addLegalModal')">Annuler</button>
            </div>
        </form>
    </div>
</div>

<div id="editLegalModal" class="modal">
    <div class="modal-content">
        <h3 style="margin-top:0; color:#2d3436;">Modifier l'Info Légale</h3>
        <form method="POST" enctype="multipart/form-data">
            <input type="hidden" name="edit_legal_id" id="edit_legal_id">
            <select name="edit_legal_type" id="edit_legal_type" required>
                <?php foreach($all_types as $t): ?>
                    <option value="<?php echo htmlspecialchars($t['nom_type']); ?>"><?php echo htmlspecialchars($t['nom_type']); ?></option>
                <?php endforeach; ?>
            </select>
            <input type="text" name="edit_legal_title" id="edit_legal_title" required>
            <div class="form-grid-dates">
                <div>
                    <label style="font-size: 0.85em; color: #666; font-weight: bold;">Date de publication :</label>
                    <input type="date" name="edit_legal_debut" id="edit_legal_debut" required>
                </div>
                <div>
                    <label style="font-size: 0.85em; color: #666; font-weight: bold;">Date de fin :</label>
                    <input type="date" name="edit_legal_fin" id="edit_legal_fin">
                </div>
            </div>
            <label style="font-size: 0.85em; color: #666; font-weight: bold; margin-top: 10px; display: block;">Ajouter des pièces jointes (Max 5Mo) :</label>
            <input type="file" name="edit_legal_files[]" accept=".pdf, image/jpeg, image/png, image/webp" multiple>
            <textarea name="edit_legal_content" id="edit_legal_content" rows="5" required></textarea>
            <div style="display:flex; gap:10px;">
                <button type="submit" class="btn-valider" style="flex:1; margin:0; background-color:#2d3436;">Enregistrer</button>
                <button type="button" class="btn-annuler" style="flex:1; margin:0;" onclick="closeModal('editLegalModal')">Annuler</button>
            </div>
        </form>
    </div>
</div>

<div id="manageTypesModal" class="modal">
    <div class="modal-content">
        <h3 style="margin-top:0; color:#607d8b;">Gérer les catégories</h3>
        <form method="POST" style="display:flex; gap:10px; margin-bottom:20px; padding-bottom: 20px; border-bottom: 2px solid #eee;">
            <input type="text" name="new_type_name" placeholder="Nouvelle catégorie..." required style="margin:0; flex:1;">
            <button type="submit" name="add_legal_type" class="btn-valider" style="margin:0; background-color:#607d8b;">Ajouter</button>
        </form>
        <div class="type-list">
            <?php foreach($all_types as $t): ?>
                <div class="type-item">
                    <strong><?php echo htmlspecialchars($t['nom_type']); ?></strong>
                    <div style="display:flex; gap:5px;">
                        <button onclick="openEditTypeModal(<?php echo $t['id']; ?>, '<?php echo htmlspecialchars(addslashes($t['nom_type'])); ?>')" class="btn-small btn-small-edit">✎</button>
                        <form method="POST" style="margin:0;" onsubmit="return confirm('Supprimer cette catégorie ?');">
                            <input type="hidden" name="delete_legal_type_id" value="<?php echo $t['id']; ?>">
                            <button type="submit" class="btn-small btn-small-del">✕</button>
                        </form>
                    </div>
                </div>
            <?php endforeach; ?>
        </div>
        <button type="button" class="btn-annuler" style="width:100%; margin-top:20px;" onclick="closeModal('manageTypesModal')">Fermer</button>
    </div>
</div>

<div id="editTypeModal" class="modal" style="z-index: 4000;">
    <div class="modal-content" style="max-width: 400px;">
        <h3 style="margin-top:0; color:#607d8b;">Renommer la catégorie</h3>
        <form method="POST">
            <input type="hidden" name="edit_legal_type_id" id="edit_type_id">
            <input type="text" name="edit_type_name" id="edit_type_name" required>
            <div style="display:flex; gap:10px;">
                <button type="submit" class="btn-valider" style="flex:1; margin:0; background-color:#607d8b;">Valider</button>
                <button type="button" class="btn-annuler" style="flex:1; margin:0;" onclick="closeModal('editTypeModal')">Annuler</button>
            </div>
        </form>
    </div>
</div>