// 图书列表页面脚本 $(document).ready(function() { // 自定义下拉列表处理 const filterHeaders = document.querySelectorAll('.filter-header'); filterHeaders.forEach(header => { header.addEventListener('click', function() { // 切换当前下拉列表 const dropdown = this.nextElementSibling; const isOpen = dropdown.classList.contains('show'); // 关闭所有下拉列表 document.querySelectorAll('.dropdown-options').forEach(el => { el.classList.remove('show'); }); document.querySelectorAll('.filter-header').forEach(el => { el.classList.remove('open'); }); // 如果当前不是打开状态,则打开它 if(!isOpen) { dropdown.classList.add('show'); this.classList.add('open'); } }); }); // 点击选项时更新并提交表单 const optionItems = document.querySelectorAll('.option-item'); optionItems.forEach(item => { item.addEventListener('click', function() { const container = this.closest('.custom-select-container'); const header = container.querySelector('.filter-header .filter-title'); const dropdown = this.closest('.dropdown-options'); const hiddenInput = container.querySelector('input[type="hidden"]'); // 移除之前的选中项 dropdown.querySelectorAll('.option-item').forEach(opt => { opt.classList.remove('selected'); }); // 设置新的选中项 this.classList.add('selected'); // 更新隐藏输入值 const value = this.getAttribute('data-value'); hiddenInput.value = value; // 更新显示的文本和图标 const icon = this.querySelector('i').cloneNode(true); const text = this.querySelector('span').textContent; header.innerHTML = ''; header.appendChild(icon); const span = document.createElement('span'); span.textContent = text; header.appendChild(span); // 关闭下拉列表 dropdown.classList.remove('show'); container.querySelector('.filter-header').classList.remove('open'); // 提交表单 document.getElementById('filter-form').submit(); }); }); // 点击页面其他地方关闭下拉列表 document.addEventListener('click', function(e) { if(!e.target.closest('.custom-select-container')) { document.querySelectorAll('.dropdown-options').forEach(el => { el.classList.remove('show'); }); document.querySelectorAll('.filter-header').forEach(el => { el.classList.remove('open'); }); } }); // 处理删除图书 let bookIdToDelete = null; $('.delete-book').click(function(e) { e.preventDefault(); bookIdToDelete = $(this).data('id'); const bookTitle = $(this).data('title'); $('#deleteBookTitle').text(bookTitle); $('#deleteModal').modal('show'); }); $('#confirmDelete').click(function() { if (!bookIdToDelete) return; $.ajax({ url: `/book/delete/${bookIdToDelete}`, type: 'POST', success: function(response) { if (response.success) { $('#deleteModal').modal('hide'); // 显示成功消息 showNotification(response.message, 'success'); // 移除图书卡片 $(`.book-card[data-id="${bookIdToDelete}"]`).fadeOut(300, function() { $(this).remove(); }); setTimeout(() => { if ($('.book-card').length === 0) { location.reload(); // 如果没有图书了,刷新页面显示"无图书"提示 } }, 500); } else { $('#deleteModal').modal('hide'); showNotification(response.message, 'error'); } }, error: function() { $('#deleteModal').modal('hide'); showNotification('删除操作失败,请稍后重试', 'error'); } }); }); // 处理借阅图书 $('.borrow-book').click(function(e) { e.preventDefault(); const bookId = $(this).data('id'); $.ajax({ url: `/borrow/add/${bookId}`, type: 'POST', success: function(response) { if (response.success) { showNotification(response.message, 'success'); // 可以更新UI显示,比如更新库存或禁用借阅按钮 setTimeout(() => { location.reload(); }, 800); } else { showNotification(response.message, 'error'); } }, error: function() { showNotification('借阅操作失败,请稍后重试', 'error'); } }); }); // 显示通知 function showNotification(message, type) { // 移除可能存在的旧通知 $('.notification-alert').remove(); const alertClass = type === 'success' ? 'notification-success' : 'notification-error'; const iconClass = type === 'success' ? 'fa-check-circle' : 'fa-exclamation-circle'; const notification = `