/**
* 班级操行分管理系统 - 操行分管理页JS
*
* 开发者: Canglan
* 版权归属: Sea Network Technology Studio
*
* 版权所有 © Sea Network Technology Studio
*/
(function() {
'use strict';
const canViewPersonalQuery = hasPermission('view_all_records');
const canManageConduct = hasPermission('manage_conduct');
async function loadStudents() {
const res = await apiGet('/api/admin/students', {page_size: 1000});
if (res && res.success) {
let html = '';
res.data.students.forEach(student => {
const detailUrl = canViewPersonalQuery
? `/admin/record_query.php?student_id=${student.student_id}`
: `/admin/history.php?student_id=${student.student_id}`;
const pointsButton = canManageConduct
? ``
: '-';
html += `
|
${escapeHtml(student.student_no)} |
${escapeHtml(student.name)} |
${student.total_points} |
${pointsButton} |
`;
});
if (res.data.students.length === 0) {
html = '| 暂无学生数据 |
';
}
document.getElementById('studentList').innerHTML = html;
}
}
function initPointsLimitHint() {
const hintEl = document.getElementById('pointsLimitHint');
if (hintEl) hintEl.textContent = describeScoreLimits();
}
async function showExportMoralityModal() {
await loadSemesterOptions('exportSemesterSelect', { includeAll: true });
document.getElementById('exportMoralityModal').style.display = 'flex';
}
function buildExportRow(student, semesterLabel, records, basePoints) {
const positiveRecords = records.filter(r => r.points_change > 0);
const negativeRecords = records.filter(r => r.points_change < 0);
const bonus = positiveRecords.reduce((sum, r) => sum + r.points_change, 0);
const deduction = negativeRecords.reduce((sum, r) => sum + r.points_change, 0);
return {
student_no: student.student_no,
name: student.name,
semester: semesterLabel,
base: basePoints,
bonus: bonus,
deduction: deduction,
score: records.length ? basePoints + bonus + deduction : (student.total_points || 0),
current: student.total_points || 0,
positive: positiveRecords.map(r => `${r.reason}(+${r.points_change})`).join('; '),
negative: negativeRecords.map(r => `${r.reason}(${r.points_change})`).join('; ')
};
}
async function exportMoralityRecords() {
const select = document.getElementById('exportSemesterSelect');
const semesterId = select ? select.value : '';
const selectedOption = select && select.selectedIndex >= 0 ? select.options[select.selectedIndex] : null;
const semesterName = selectedOption ? selectedOption.textContent.replace(/((当前|已归档))$/, '') : '全部学期';
showToast('正在导出德育分记录...', 'info');
try {
const studentsRes = await apiGet('/api/admin/students', { page_size: 1000 });
if (!studentsRes || !studentsRes.success) {
showToast('获取学生列表失败', 'error');
return;
}
const students = studentsRes.data.students;
if (students.length === 0) {
showToast('没有找到学生', 'warning');
return;
}
const params = { is_revoked: 0 };
if (semesterId) params.semester_id = semesterId;
const allRecords = await apiGetAll('/api/admin/conduct/history', params);
const basePoints = parseInt(window.STUDENT_INITIAL_POINTS || 60, 10) || 60;
// 按 学生 + 学期 分组
const groups = {};
allRecords.forEach(record => {
const label = semesterId ? semesterName : (record.semester_name || '未关联学期');
const key = record.student_id + '|' + label;
if (!groups[key]) groups[key] = [];
groups[key].push(record);
});
const rows = [];
students.forEach(student => {
if (semesterId) {
const key = student.student_id + '|' + semesterName;
rows.push(buildExportRow(student, semesterName, groups[key] || [], basePoints));
} else {
const prefix = student.student_id + '|';
const labels = Object.keys(groups).filter(k => k.indexOf(prefix) === 0);
if (labels.length === 0) {
rows.push(buildExportRow(student, '-', [], basePoints));
} else {
labels.forEach(key => {
rows.push(buildExportRow(student, key.substring(prefix.length), groups[key], basePoints));
});
}
}
});
let csv = '学号,姓名,学期,基础分,加分合计,减分合计,学期得分,当前总分,加分历史,减分记录\n';
rows.forEach(r => {
csv += [r.student_no, r.name, r.semester, r.base, r.bonus, r.deduction, r.score, r.current, r.positive, r.negative]
.map(csvEscape).join(',') + '\n';
});
const safeSemester = (semesterName || '全部学期').replace(/[\\/:*?"<>|]/g, '_');
downloadCsv(`德育分记录_${safeSemester}_${new Date().toISOString().slice(0,10)}.csv`, csv);
closeModal('exportMoralityModal');
showToast(`导出成功,共${rows.length}条学生记录`);
} catch (err) {
showToast('导出失败:' + err.message, 'error');
console.error('导出失败:', err);
}
}
// 宿舍集体加分相关
var dormitoryStudentIds = [];
async function showDormitoryPointsModal() {
dormitoryStudentIds = [];
document.getElementById('dormitorySelect').innerHTML = '';
document.getElementById('dormitoryStudentsGroup').style.display = 'none';
document.getElementById('dormitoryStudentsList').innerHTML = '';
document.getElementById('dormitoryPointsChange').value = '';
document.getElementById('dormitoryPointsReason').value = '';
// 加载宿舍列表
const res = await apiGet('/api/admin/students/dormitories');
if (res && res.success && res.data.dormitories) {
const select = document.getElementById('dormitorySelect');
res.data.dormitories.forEach(d => {
const option = document.createElement('option');
option.value = d;
option.textContent = d;
select.appendChild(option);
});
}
document.getElementById('dormitoryPointsModal').style.display = 'flex';
}
async function onDormitorySelected() {
const dormitory = document.getElementById('dormitorySelect').value;
const studentsGroup = document.getElementById('dormitoryStudentsGroup');
const studentsList = document.getElementById('dormitoryStudentsList');
const studentsCount = document.getElementById('dormitoryStudentsCount');
dormitoryStudentIds = [];
studentsList.innerHTML = '';
if (!dormitory) {
studentsGroup.style.display = 'none';
return;
}
// 加载该宿舍的学生
const res = await apiGet('/api/admin/students', { dormitory_number: dormitory, page_size: 1000 });
if (res && res.success && res.data.students) {
const students = res.data.students;
if (students.length === 0) {
studentsList.innerHTML = '该宿舍暂无学生
';
studentsCount.textContent = '';
} else {
students.forEach(s => {
dormitoryStudentIds.push(s.student_id);
const div = document.createElement('div');
div.style.cssText = 'display: flex; justify-content: space-between; padding: 4px 0; border-bottom: 1px solid var(--color-border);';
div.innerHTML = `${escapeHtml(s.name)}${escapeHtml(s.student_no)}`;
studentsList.appendChild(div);
});
studentsCount.textContent = `共 ${students.length} 人`;
}
studentsGroup.style.display = 'block';
} else {
studentsList.innerHTML = '加载失败
';
studentsGroup.style.display = 'block';
}
}
async function submitDormitoryPoints() {
if (dormitoryStudentIds.length === 0) {
showToast('该宿舍没有学生', 'warning');
return;
}
const pointsChange = parseInt(document.getElementById('dormitoryPointsChange').value);
const reason = document.getElementById('dormitoryPointsReason').value;
if (isNaN(pointsChange) || pointsChange === 0) {
showToast('分值不能为0', 'error');
return;
}
if (!reason.trim()) {
showToast('请填写原因', 'error');
return;
}
const data = {
student_ids: dormitoryStudentIds,
points_change: pointsChange,
reason: reason
};
const res = await apiPost('/api/admin/conduct/add', data);
if (res && res.success) {
showToast(`操作成功: ${res.data.success_count} 人成功`);
closeModal('dormitoryPointsModal');
loadStudents();
initPointsLimitHint();
} else {
showToast(res?.message || '操作失败', 'error');
}
}
loadStudents();
window.loadStudents = loadStudents;
window.showExportMoralityModal = showExportMoralityModal;
window.exportMoralityRecords = exportMoralityRecords;
window.showDormitoryPointsModal = showDormitoryPointsModal;
window.onDormitorySelected = onDormitorySelected;
window.submitDormitoryPoints = submitDormitoryPoints;
})();