let currentStudent = null; // 随机点名 document.getElementById('randomBtn').addEventListener('click', function() { fetch('/random_pick') .then(response => response.json()) .then(data => { if (data.error) { alert(data.error); return; } displayStudent(data); currentStudent = data; document.getElementById('scoreControls').style.display = 'block'; }) .catch(error => { console.error('Error:', error); alert('点名失败,请重试'); }); }); // 顺序点名 document.getElementById('sequentialBtn').addEventListener('click', function() { fetch('/sequential_pick') .then(response => response.json()) .then(data => { if (data.error) { alert(data.error); return; } displayStudent(data); currentStudent = data; document.getElementById('scoreControls').style.display = 'block'; }) .catch(error => { console.error('Error:', error); alert('点名失败,请重试'); }); }); // 显示学生信息 function displayStudent(student) { const display = document.getElementById('studentDisplay'); display.innerHTML = `

${student.name}

学号:${student.student_id}

当前积分:${student.score}

被点名次数:${student.call_count}

`; } // 更新积分 function updateScore(scoreChange) { if (!currentStudent) { alert('请先点名'); return; } fetch('/update_score', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ student_id: currentStudent.student_id, score_change: scoreChange }) }) .then(response => response.json()) .then(data => { if (data.success) { alert('积分更新成功!'); // 更新显示 currentStudent.score += scoreChange; displayStudent(currentStudent); } }) .catch(error => { console.error('Error:', error); alert('积分更新失败'); }); } // 导入Excel document.getElementById('importBtn').addEventListener('click', function() { const fileInput = document.getElementById('excelFile'); const file = fileInput.files[0]; if (!file) { alert('请选择Excel文件'); return; } const formData = new FormData(); formData.append('file', file); fetch('/import_excel', { method: 'POST', body: formData }) .then(response => response.json()) .then(data => { const messageDiv = document.getElementById('importMessage'); if (data.success) { messageDiv.textContent = data.message; messageDiv.className = 'success'; } else { messageDiv.textContent = data.error; messageDiv.className = 'error'; } }) .catch(error => { console.error('Error:', error); alert('导入失败'); }); }); // 导出数据 document.getElementById('exportBtn').addEventListener('click', function() { window.open('/export_data', '_blank'); }); // 查看排名 document.getElementById('rankingBtn').addEventListener('click', function() { fetch('/get_ranking') .then(response => response.json()) .then(data => { displayRanking(data); }) .catch(error => { console.error('Error:', error); alert('获取排名失败'); }); }); // 显示排名 function displayRanking(rankingData) { const rankingList = document.getElementById('rankingList'); const rankingDisplay = document.getElementById('rankingDisplay'); if (rankingData.length === 0) { rankingList.innerHTML = '

暂无数据

'; } else { rankingList.innerHTML = rankingData.map((student, index) => `
第${index + 1}名 ${student.name} 积分:${student.score} 点名:${student.call_count}次
`).join(''); } rankingDisplay.style.display = 'block'; }