From 785f805d10685a411786cd28773840a56e54b701 Mon Sep 17 00:00:00 2001
From: Esenjin <esenjin@sangigi-fuchsia.fr>
Date: Sun, 16 Feb 2025 12:41:52 +0100
Subject: [PATCH] Il est de nouveau possible de supprimer les chapitres

---
 assets/js/story-edit.js | 83 +++++++++++++++++++++++++++++++----------
 1 file changed, 64 insertions(+), 19 deletions(-)

diff --git a/assets/js/story-edit.js b/assets/js/story-edit.js
index 5e44b69..1f07b64 100644
--- a/assets/js/story-edit.js
+++ b/assets/js/story-edit.js
@@ -2,6 +2,28 @@ document.addEventListener('DOMContentLoaded', function() {
     let currentChapterId = null;
     const storyId = document.querySelector('input[name="id"]')?.value;
 
+    // Fonction de notification
+    function showNotification(message, type = 'success') {
+        const notification = document.createElement('div');
+        notification.className = `notification ${type}`;
+        notification.textContent = message;
+        
+        document.body.appendChild(notification);
+        
+        // Animation d'entrée
+        setTimeout(() => {
+            notification.style.opacity = '1';
+            notification.style.transform = 'translateY(0)';
+        }, 10);
+        
+        // Auto-suppression après 3 secondes
+        setTimeout(() => {
+            notification.style.opacity = '0';
+            notification.style.transform = 'translateY(-100%)';
+            setTimeout(() => notification.remove(), 300);
+        }, 3000);
+    }
+
     // Création de l'icône SVG pour le séparateur
     const icons = {
         divider: `
@@ -213,6 +235,48 @@ document.addEventListener('DOMContentLoaded', function() {
                     showNotification('Erreur lors du chargement du chapitre', 'error');
                 }
             }
+            
+            // Gestion de la suppression
+            if (target.matches('.delete-chapter')) {
+                const chapterItem = target.closest('.chapter-item');
+                const chapterId = chapterItem.dataset.id;
+                const chapterTitle = chapterItem.querySelector('.chapter-title').textContent;
+
+                if (confirm(`Voulez-vous vraiment supprimer le chapitre "${chapterTitle}" ? Cette action est irréversible.`)) {
+                    try {
+                        const response = await fetch('api/delete-chapter.php', {
+                            method: 'POST',
+                            headers: {
+                                'Content-Type': 'application/json',
+                            },
+                            body: JSON.stringify({
+                                storyId: storyId,
+                                chapterId: chapterId
+                            })
+                        });
+
+                        if (!response.ok) {
+                            throw new Error('Erreur réseau');
+                        }
+
+                        const result = await response.json();
+                        if (result.success) {
+                            // Animation de suppression
+                            chapterItem.style.opacity = '0';
+                            chapterItem.style.transform = 'translateX(-100%)';
+                            setTimeout(() => {
+                                chapterItem.remove();
+                                showNotification('Chapitre supprimé avec succès');
+                            }, 300);
+                        } else {
+                            throw new Error(result.error || 'Erreur lors de la suppression');
+                        }
+                    } catch (error) {
+                        console.error('Erreur:', error);
+                        showNotification(error.message, 'error');
+                    }
+                }
+            }
         });
     }
 
@@ -258,23 +322,4 @@ document.addEventListener('DOMContentLoaded', function() {
         }
         return true;
     }
-
-    function showNotification(message, type = 'success') {
-        const notification = document.createElement('div');
-        notification.className = `notification ${type}`;
-        notification.textContent = message;
-        
-        document.body.appendChild(notification);
-        
-        setTimeout(() => {
-            notification.style.opacity = '1';
-            notification.style.transform = 'translateY(0)';
-        }, 10);
-        
-        setTimeout(() => {
-            notification.style.opacity = '0';
-            notification.style.transform = 'translateY(-100%)';
-            setTimeout(() => notification.remove(), 300);
-        }, 3000);
-    }
 });
\ No newline at end of file