// 图书列表页面脚本 $(document).ready(function() { // 处理分类筛选 function setFilter(button, categoryId) { // 移除所有按钮的活跃状态 $('.filter-btn').removeClass('active'); // 为当前点击的按钮添加活跃状态 $(button).addClass('active'); // 设置隐藏的分类ID输入值 $('#category_id').val(categoryId); // 提交表单 $(button).closest('form').submit(); } // 处理排序方向切换 function toggleSortDirection(button) { const $button = $(button); const isAsc = $button.hasClass('asc'); // 切换方向类 $button.toggleClass('asc desc'); // 更新图标 if (isAsc) { $button.find('i').removeClass('fa-sort-amount-up').addClass('fa-sort-amount-down'); $('#sort_order').val('desc'); } else { $button.find('i').removeClass('fa-sort-amount-down').addClass('fa-sort-amount-up'); $('#sort_order').val('asc'); } // 提交表单 $button.closest('form').submit(); } // 将函数暴露到全局作用域 window.setFilter = setFilter; window.toggleSortDirection = toggleSortDirection; // 处理删除图书 let bookIdToDelete = null; $('.delete-btn').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'); // 移除图书卡片 setTimeout(() => { location.reload(); }, 800); } else { showNotification(response.message, 'error'); } }, error: function() { showNotification('删除操作失败,请稍后重试', 'error'); } }); }); // 处理借阅图书 $('.borrow-btn').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 = `
${message}
`; $('body').append(notification); // 显示通知 setTimeout(() => { $('.notification-alert').addClass('show'); }, 10); // 通知自动关闭 setTimeout(() => { $('.notification-alert').removeClass('show'); setTimeout(() => { $('.notification-alert').remove(); }, 300); }, 4000); // 点击关闭按钮 $('.notification-close').click(function() { $(this).closest('.notification-alert').removeClass('show'); setTimeout(() => { $(this).closest('.notification-alert').remove(); }, 300); }); } // 添加通知样式 const notificationCSS = ` .notification-alert { position: fixed; top: 20px; right: 20px; min-width: 280px; max-width: 350px; background-color: white; border-radius: 8px; box-shadow: 0 5px 15px rgba(0, 0, 0, 0.15); display: flex; align-items: center; padding: 15px; transform: translateX(calc(100% + 20px)); transition: transform 0.3s ease; z-index: 9999; } .notification-alert.show { transform: translateX(0); } .notification-success { border-left: 4px solid var(--success-color); } .notification-error { border-left: 4px solid var(--danger-color); } .notification-icon { margin-right: 15px; font-size: 24px; } .notification-success .notification-icon { color: var(--success-color); } .notification-error .notification-icon { color: var(--danger-color); } .notification-message { flex: 1; font-size: 0.95rem; color: var(--text-color); } .notification-close { background: none; border: none; color: var(--text-lighter); cursor: pointer; padding: 5px; margin-left: 10px; font-size: 0.8rem; } .notification-close:hover { color: var(--text-color); } @media (max-width: 576px) { .notification-alert { top: auto; bottom: 20px; left: 20px; right: 20px; min-width: auto; max-width: none; transform: translateY(calc(100% + 20px)); } .notification-alert.show { transform: translateY(0); } } `; // 将通知样式添加到头部 $('