diff --git a/views/chapters/index.php b/views/chapters/index.php index 33bacd6..a166b0e 100755 --- a/views/chapters/index.php +++ b/views/chapters/index.php @@ -90,19 +90,18 @@ include 'views/layouts/header.php'; -
- - - -
- - -
-
- +
+ + + + +
+ @@ -192,6 +191,66 @@ document.addEventListener('DOMContentLoaded', function() { }); }); }); +document.addEventListener('DOMContentLoaded', function() { + // Обработчик удаления глав + document.querySelectorAll('.delete-chapter-btn').forEach(button => { + button.addEventListener('click', function(e) { + e.preventDefault(); + + const chapterId = this.getAttribute('data-chapter-id'); + const csrfToken = this.getAttribute('data-csrf'); + + if (confirm('Вы уверены, что хотите удалить эту главу? Это действие нельзя отменить.')) { + const formData = new FormData(); + formData.append('csrf_token', csrfToken); + + fetch(`/chapters/${chapterId}/delete`, { + method: 'POST', + body: formData + }) + .then(response => { + if (response.redirected) { + window.location.href = response.url; + } else { + return response.json(); + } + }) + .then(data => { + if (data && data.success) { + // Удаляем строку таблицы + const row = document.querySelector(`tr[data-chapter-id="${chapterId}"]`); + if (row) { + row.remove(); + // Обновляем номера глав + updateChapterNumbers(); + showNotification('Глава успешно удалена', 'success'); + } + } else if (data && data.error) { + alert('Ошибка: ' + data.error); + } + }) + .catch(error => { + console.error('Error:', error); + alert('Произошла ошибка при удалении главы'); + }); + } + }); + }); + + // Функция для обновления номеров глав + function updateChapterNumbers() { + const orderNumbers = document.querySelectorAll('.chapter-order'); + orderNumbers.forEach((element, index) => { + element.textContent = index + 1; + }); + } + + // Функция для показа уведомлений + function showNotification(message, type = 'success') { + // Можно использовать библиотеку для уведомлений или простой alert + alert(message); + } +});