/** * 班级操行分管理系统 - 历史记录页JS * * 开发者: Canglan * 版权归属: Sea Network Technology Studio * * 版权所有 © Sea Network Technology Studio */ (function() { 'use strict'; const role = window.PAGE_CONFIG.role; const currentUserId = window.PAGE_CONFIG.userId; let currentHistoryPage = 1; let totalHistoryPages = 1; async function loadStudentsForSelect() { const res = await apiGet('/api/admin/students', {page_size: 1000}); if (res && res.success) { let html = ''; res.data.students.forEach(s => { html += ``; }); document.getElementById('historyStudentId').innerHTML = html; } } async function loadHistory(page = 1) { currentHistoryPage = page; const startDate = document.getElementById('historyStartDate').value; const endDate = document.getElementById('historyEndDate').value; const studentId = document.getElementById('historyStudentId').value; const relatedType = document.getElementById('historyRelatedType').value; const isGrouped = document.getElementById('historyGrouped').checked; const params = { page, page_size: 20, start_date: startDate, end_date: endDate }; if (studentId) params.student_id = studentId; if (relatedType && !isGrouped) params.related_type = relatedType; if (isGrouped) params.grouped = true; const res = await apiGet('/api/admin/conduct/history', params); if (res && res.success) { let headHtml = ''; if (isGrouped) { headHtml = '时间原因分值操作人涉及学生'; } else { headHtml = '时间学生分数变动原因操作人'; if (role === '班主任' || role === '班长' || role === '考勤委员') { headHtml += '操作'; } } document.getElementById('historyTableHead').innerHTML = headHtml; let html = ''; if (isGrouped) { res.data.records.forEach(record => { const pointsClass = record.points_change > 0 ? 'plus' : 'minus'; const names = record.student_names ? record.student_names.split(', ') : []; let tagsHtml = '
'; names.forEach(name => { tagsHtml += `${escapeHtml(name)}`; }); tagsHtml += '
'; html += ` ${formatDateTime(record.created_at)} ${escapeHtml(record.reason)} ${record.points_change > 0 ? '+' : ''}${record.points_change}×${record.student_count} ${escapeHtml(record.recorder_name || '')} ${tagsHtml} `; }); if (res.data.records.length === 0) { html = '暂无记录'; } } else { res.data.records.forEach(record => { const pointsClass = record.points_change > 0 ? 'plus' : 'minus'; const revokedStyle = record.is_revoked == 1 ? ' style="opacity:0.5; text-decoration:line-through;"' : ''; html += ` ${formatDateTime(record.created_at)} ${escapeHtml(record.student_name)} ${record.points_change > 0 ? '+' : ''}${record.points_change} ${escapeHtml(record.reason)} ${escapeHtml(record.recorder_name)}`; if (role === '班主任') { if (record.is_revoked == 1) { const revokerInfo = record.revoker_name ? `由 ${escapeHtml(record.revoker_name)} 撤销` : '已撤销'; html += `${revokerInfo}`; } else { html += ``; } } else if (role === '班长') { if (record.is_revoked == 1) { const revokerInfo = record.revoker_name ? `由 ${escapeHtml(record.revoker_name)} 撤销` : '已撤销'; html += `${revokerInfo}`; } else { html += ``; } } else if (role === '考勤委员') { if (record.is_revoked == 1) { html += `已撤销`; } else if (record.recorder_id == currentUserId) { html += ``; } else { html += `-`; } } html += ``; }); if (res.data.records.length === 0) { const colSpan = (role === '班主任' || role === '班长' || role === '考勤委员') ? 6 : 5; html = `暂无记录`; } } document.getElementById('historyList').innerHTML = html; totalHistoryPages = res.data.total_pages || 1; renderHistoryPagination(); } } function renderHistoryPagination() { renderSmartPagination('historyPagination', currentHistoryPage, totalHistoryPages, function(page) { loadHistory(page); }); } async function exportHistoryRecords() { const startDate = document.getElementById('historyStartDate').value; const endDate = document.getElementById('historyEndDate').value; const studentId = document.getElementById('historyStudentId').value; showToast('正在导出历史记录...', 'info'); try { const relatedType = document.getElementById('historyRelatedType').value; const params = { page: 1, page_size: 1000 }; if (startDate) params.start_date = startDate; if (endDate) params.end_date = endDate; if (studentId) params.student_id = studentId; if (relatedType) params.related_type = relatedType; const res = await apiGet('/api/admin/conduct/history', params); if (res && res.success && res.data.records) { const records = res.data.records; if (records.length === 0) { showToast('没有找到记录', 'warning'); return; } let csv = '\uFEFF'; csv += '时间,学号,姓名,分数变动,原因,操作人\n'; records.forEach(r => { csv += `${r.created_at || ''},${r.student_no || ''},${r.student_name || ''},${r.points_change > 0 ? '+' : ''}${r.points_change},${(r.reason || '').replace(/,/g, ';')},${r.recorder_name || ''}\n`; }); const blob = new Blob([csv], { type: 'text/csv;charset=utf-8;' }); const url = URL.createObjectURL(blob); const link = document.createElement('a'); link.href = url; link.download = `历史记录_${new Date().toISOString().slice(0,10)}.csv`; document.body.appendChild(link); link.click(); document.body.removeChild(link); URL.revokeObjectURL(url); showToast(`导出成功,共${records.length}条记录`); } else { showToast('导出失败:' + (res?.message || '未知错误'), 'error'); } } catch (err) { showToast('导出失败:' + err.message, 'error'); } } loadStudentsForSelect().then(() => { const urlParams = new URLSearchParams(window.location.search); const preStudentId = urlParams.get('student_id'); if (preStudentId) { document.getElementById('historyStudentId').value = preStudentId; loadHistory(); } else { loadHistory(); } }); window.loadHistory = loadHistory; window.loadStudentsForSelect = loadStudentsForSelect; window.exportHistoryRecords = exportHistoryRecords; })();