133 lines
		
	
	
		
			4.4 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			133 lines
		
	
	
		
			4.4 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
// my_borrows.js
 | 
						|
 | 
						|
document.addEventListener('DOMContentLoaded', function() {
 | 
						|
    // 归还图书功能
 | 
						|
    const returnButtons = document.querySelectorAll('.return-btn');
 | 
						|
    const returnModal = document.getElementById('returnModal');
 | 
						|
    const returnBookTitle = document.getElementById('returnBookTitle');
 | 
						|
    const confirmReturnButton = document.getElementById('confirmReturn');
 | 
						|
    let currentBorrowId = null;
 | 
						|
 | 
						|
    returnButtons.forEach(button => {
 | 
						|
        button.addEventListener('click', function() {
 | 
						|
            const borrowId = this.getAttribute('data-id');
 | 
						|
            const bookTitle = this.getAttribute('data-title');
 | 
						|
 | 
						|
            currentBorrowId = borrowId;
 | 
						|
            returnBookTitle.textContent = bookTitle;
 | 
						|
 | 
						|
            // 使用 Bootstrap 的 jQuery 方法显示模态框
 | 
						|
            $('#returnModal').modal('show');
 | 
						|
        });
 | 
						|
    });
 | 
						|
 | 
						|
    confirmReturnButton.addEventListener('click', function() {
 | 
						|
        if (!currentBorrowId) return;
 | 
						|
 | 
						|
        // 发送归还请求
 | 
						|
        fetch(`/borrow/return/${currentBorrowId}`, {
 | 
						|
            method: 'POST',
 | 
						|
            headers: {
 | 
						|
                'Content-Type': 'application/json',
 | 
						|
                'X-Requested-With': 'XMLHttpRequest'
 | 
						|
            },
 | 
						|
            body: JSON.stringify({})
 | 
						|
        })
 | 
						|
        .then(response => response.json())
 | 
						|
        .then(data => {
 | 
						|
            // 隐藏模态框
 | 
						|
            $('#returnModal').modal('hide');
 | 
						|
 | 
						|
            if (data.success) {
 | 
						|
                // 显示成功消息
 | 
						|
                showAlert('success', data.message);
 | 
						|
                // 重新加载页面以更新借阅状态
 | 
						|
                setTimeout(() => window.location.reload(), 1500);
 | 
						|
            } else {
 | 
						|
                // 显示错误消息
 | 
						|
                showAlert('danger', data.message);
 | 
						|
            }
 | 
						|
        })
 | 
						|
        .catch(error => {
 | 
						|
            $('#returnModal').modal('hide');
 | 
						|
            showAlert('danger', '操作失败,请稍后重试');
 | 
						|
            console.error('Error:', error);
 | 
						|
        });
 | 
						|
    });
 | 
						|
 | 
						|
    // 续借图书功能
 | 
						|
    const renewButtons = document.querySelectorAll('.renew-btn');
 | 
						|
    const renewModal = document.getElementById('renewModal');
 | 
						|
    const renewBookTitle = document.getElementById('renewBookTitle');
 | 
						|
    const confirmRenewButton = document.getElementById('confirmRenew');
 | 
						|
 | 
						|
    renewButtons.forEach(button => {
 | 
						|
        button.addEventListener('click', function() {
 | 
						|
            const borrowId = this.getAttribute('data-id');
 | 
						|
            const bookTitle = this.getAttribute('data-title');
 | 
						|
 | 
						|
            currentBorrowId = borrowId;
 | 
						|
            renewBookTitle.textContent = bookTitle;
 | 
						|
 | 
						|
            // 使用 Bootstrap 的 jQuery 方法显示模态框
 | 
						|
            $('#renewModal').modal('show');
 | 
						|
        });
 | 
						|
    });
 | 
						|
 | 
						|
    confirmRenewButton.addEventListener('click', function() {
 | 
						|
        if (!currentBorrowId) return;
 | 
						|
 | 
						|
        // 发送续借请求
 | 
						|
        fetch(`/borrow/renew/${currentBorrowId}`, {
 | 
						|
            method: 'POST',
 | 
						|
            headers: {
 | 
						|
                'Content-Type': 'application/json',
 | 
						|
                'X-Requested-With': 'XMLHttpRequest'
 | 
						|
            },
 | 
						|
            body: JSON.stringify({})
 | 
						|
        })
 | 
						|
        .then(response => response.json())
 | 
						|
        .then(data => {
 | 
						|
            // 隐藏模态框
 | 
						|
            $('#renewModal').modal('hide');
 | 
						|
 | 
						|
            if (data.success) {
 | 
						|
                // 显示成功消息
 | 
						|
                showAlert('success', data.message);
 | 
						|
                // 重新加载页面以更新借阅状态
 | 
						|
                setTimeout(() => window.location.reload(), 1500);
 | 
						|
            } else {
 | 
						|
                // 显示错误消息
 | 
						|
                showAlert('danger', data.message);
 | 
						|
            }
 | 
						|
        })
 | 
						|
        .catch(error => {
 | 
						|
            $('#renewModal').modal('hide');
 | 
						|
            showAlert('danger', '操作失败,请稍后重试');
 | 
						|
            console.error('Error:', error);
 | 
						|
        });
 | 
						|
    });
 | 
						|
 | 
						|
    // 显示提示消息
 | 
						|
    function showAlert(type, message) {
 | 
						|
        const alertDiv = document.createElement('div');
 | 
						|
        alertDiv.className = `alert alert-${type} alert-dismissible fade show fixed-top mx-auto mt-3`;
 | 
						|
        alertDiv.style.maxWidth = '500px';
 | 
						|
        alertDiv.style.zIndex = '9999';
 | 
						|
 | 
						|
        alertDiv.innerHTML = `
 | 
						|
            ${message}
 | 
						|
            <button type="button" class="close" data-dismiss="alert" aria-label="Close">
 | 
						|
                <span aria-hidden="true">×</span>
 | 
						|
            </button>
 | 
						|
        `;
 | 
						|
 | 
						|
        document.body.appendChild(alertDiv);
 | 
						|
 | 
						|
        // 3秒后自动消失
 | 
						|
        setTimeout(() => {
 | 
						|
            alertDiv.remove();
 | 
						|
        }, 3000);
 | 
						|
    }
 | 
						|
});
 |