/**
* 班级操行分管理系统 - 管理端首页JS
*
* 开发者: Canglan
* 版权归属: Sea Network Technology Studio
*
* 版权所有 © Sea Network Technology Studio
*/
(function() {
'use strict';
let totalStudents = 0;
async function loadDashboard() {
// 并行加载学生数据和学期信息
const [studentsRes, semesterRes] = await Promise.all([
apiGet('/api/admin/students'),
apiGet('/api/semester/active')
]);
let statsHtml = '';
if (studentsRes && studentsRes.success) {
statsHtml += `
学生总数
${studentsRes.data.total || 0}
`;
}
// 显示学期信息和当前周数
if (semesterRes && semesterRes.success && semesterRes.data) {
const sem = semesterRes.data;
const weekNum = sem.current_week;
let semesterInfo = escapeHtml(sem.semester_name);
if (weekNum && weekNum > 0) {
semesterInfo += ` · 第${weekNum}周`;
}
statsHtml += `
`;
}
document.getElementById('dashboardStats').innerHTML = statsHtml;
let quickActions = '';
if (hasPermission('manage_conduct')) {
quickActions += '';
}
if (hasPermission('manage_homework')) {
quickActions += '';
}
if (hasPermission('manage_attendance')) {
quickActions += '';
}
if (hasPermission('manage_students')) {
quickActions += '';
}
if (hasPermission('manage_semesters')) {
quickActions += '';
}
if (hasPermission('export_data')) {
quickActions += '';
}
if (hasPermission('export_students')) {
quickActions += '';
}
document.getElementById('quickActions').innerHTML = quickActions || '暂无快捷操作
';
await loadRanking();
}
async function loadRanking() {
const type = document.getElementById('rankingType').value;
const params = { limit: 100 };
if (type === 'monthly') {
const monthInput = document.getElementById('rankingMonth');
if (monthInput.value) params.month = monthInput.value;
}
const rankingRes = await apiGet('/api/student/ranking', params);
const head = document.getElementById('rankingHead');
if (type === 'monthly') {
head.innerHTML = '排名 | 学号 | 姓名 | 加分 | 减分 | 月度分 | ';
} else {
head.innerHTML = '排名 | 学号 | 姓名 | 操行分 | ';
}
if (!rankingRes || !rankingRes.success) return;
totalStudents = rankingRes.data.total_students || 0;
let html = '';
rankingRes.data.ranking.forEach((student, index) => {
const rank = index + 1;
if (type === 'monthly') {
const score = student.month_score || 0;
const cls = score > 0 ? 'plus' : (score < 0 ? 'minus' : '');
html += `
| ${rank} |
${escapeHtml(student.student_no)} |
${escapeHtml(student.name)} |
+${student.bonus || 0} |
${student.deduction || 0} |
${score > 0 ? '+' : ''}${score} |
`;
} else {
html += `
| ${rank} |
${escapeHtml(student.student_no)} |
${escapeHtml(student.name)} |
${student.total_points} |
`;
}
});
if (rankingRes.data.ranking.length === 0) {
const cols = type === 'monthly' ? 6 : 4;
html = `| 暂无数据 |
`;
}
document.getElementById('rankingList').innerHTML = html;
}
function onRankingTypeChange() {
const isMonthly = document.getElementById('rankingType').value === 'monthly';
const monthInput = document.getElementById('rankingMonth');
monthInput.style.display = isMonthly ? '' : 'none';
if (isMonthly && !monthInput.value) {
const now = new Date();
monthInput.value = now.getFullYear() + '-' + String(now.getMonth() + 1).padStart(2, '0');
}
resetPercentileFilter();
loadRanking();
}
async function exportRanking() {
const type = document.getElementById('rankingType').value;
const params = { limit: 1000 };
let label = '总分排行';
if (type === 'monthly') {
const month = document.getElementById('rankingMonth').value;
if (!month) {
showToast('请先选择月份', 'warning');
return;
}
params.month = month;
label = '月度排行_' + month;
}
const res = await apiGet('/api/student/ranking', params);
if (!res || !res.success) {
showToast('导出失败', 'error');
return;
}
const ranking = res.data.ranking || [];
if (ranking.length === 0) {
showToast('没有可导出的数据', 'warning');
return;
}
let csv = type === 'monthly'
? '排名,学号,姓名,加分,减分,月度分\n'
: '排名,学号,姓名,操行分\n';
ranking.forEach(function(s, index) {
if (type === 'monthly') {
csv += [index + 1, s.student_no, s.name, s.bonus || 0, s.deduction || 0, s.month_score || 0]
.map(csvEscape).join(',') + '\n';
} else {
csv += [index + 1, s.student_no, s.name, s.total_points]
.map(csvEscape).join(',') + '\n';
}
});
downloadCsv('排行榜_' + label + '_' + new Date().toISOString().slice(0,10) + '.csv', csv);
showToast('导出成功,共' + ranking.length + '人');
}
function applyPercentileFilter() {
const input = document.getElementById('percentileFilter');
const percentile = parseInt(input.value);
if (isNaN(percentile) || percentile < 1 || percentile > 100) {
showToast('请输入 1-100 之间的整数', 'error');
return;
}
const rows = document.getElementById('rankingList').querySelectorAll('tr');
if (rows.length === 0) return;
const showCount = Math.max(1, Math.floor(totalStudents * (percentile / 100)));
rows.forEach(function(row, index) {
row.style.display = index < showCount ? '' : 'none';
});
}
function resetPercentileFilter() {
document.getElementById('percentileFilter').value = 100;
const rows = document.getElementById('rankingList').querySelectorAll('tr');
rows.forEach(function(row) {
row.style.display = '';
});
}
document.getElementById('percentileFilter').addEventListener('keypress', function(e) {
if (e.key === 'Enter') applyPercentileFilter();
});
loadDashboard();
window.loadDashboard = loadDashboard;
window.loadRanking = loadRanking;
window.onRankingTypeChange = onRankingTypeChange;
window.exportRanking = exportRanking;
window.applyPercentileFilter = applyPercentileFilter;
window.resetPercentileFilter = resetPercentileFilter;
})();