You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

159 lines
4.5 KiB

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 = `
<div class="student-info">
<h2>${student.name}</h2>
<p>学号:${student.student_id}</p>
<p>当前积分:${student.score}</p>
<p>被点名次数:${student.call_count}</p>
</div>
`;
}
// 更新积分
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 = '<p>暂无数据</p>';
} else {
rankingList.innerHTML = rankingData.map((student, index) => `
<div class="ranking-item">
<span class="ranking-number">第${index + 1}名</span>
<span>${student.name}</span>
<span>积分:${student.score}</span>
<span>点名:${student.call_count}次</span>
</div>
`).join('');
}
rankingDisplay.style.display = 'block';
}