// 图书列表页面脚本 $(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 = `