<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>回答问题</title>
    <style>
        body {
            font-family: 'Arial', sans-serif;
            height: 100vh;
            margin: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            background: linear-gradient(135deg, #6e45e2, #88d3ce);
            color: white;
        }
        .container {
            text-align: center;
            background-color: rgba(255, 255, 255, 0.1);
            padding: 30px;
            border-radius: 15px;
            box-shadow: 0 8px 30px rgba(0, 0, 0, 0.2);
        }
        #question {
            font-size: 2em;
            margin-bottom: 20px;
        }
        button {
            padding: 12px 30px;
            font-size: 1.1em;
            border: none;
            border-radius: 50px;
            background-color: #ff5722;
            color: white;
            cursor: pointer;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
            transition: background-color 0.3s ease, box-shadow 0.3s ease;
            margin: 10px;
        }
        button:hover {
            background-color: #ff3d00;
            box-shadow: 0 8px 20px rgba(0, 0, 0, 0.3);
        }
    </style>
</head>
<body>
    
    <div class="container">
        <div id="question">请重复问题,选择正确或错误:</div>
        <button id="repeatCorrectButton">准确重复</button>
        <button id="repeatWrongButton">未准确重复</button>
        
        <div id="questionResult"></div> <!-- 显示重复问题的结果 -->

        <div id="answerContainer" style="display:none;">
            <p>现在回答问题:</p>
            <button id="correctButton">回答正确</button>
            <button id="partiallyCorrectButton">部分正确</button>
            <button id="wrongButton">回答错误</button>
        </div>
        
        <button class="back-button" onclick="window.location.href='select.html'">返回点名页面</button>
        <div id="result"></div>
        <div id="studentInfo"></div> <!-- 显示学生信息 -->
    </div>

    <script>
        async function updateScore(student_id, points) {
            try {
                const response = await fetch('/api/update-score', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({ student_id, points }),
                });

                if (!response.ok) {
                    throw new Error('网络响应不正常');
                }
                
                const data = await response.text(); // 使用 text() 解析纯数字响应
                console.log('更新后的分数:', data); // data 是字符串形式的分数
                return data; // 返回更新后的分数字符串
            } catch (error) {
                console.error('分数更新失败:', error);
                return null;
            }
}


        const urlParams = new URLSearchParams(window.location.search);
        const student_name = urlParams.get('student_name');
        const student_id = urlParams.get('student_id');

        const repeatCorrectButton = document.querySelector('#repeatCorrectButton');
        const repeatWrongButton = document.querySelector('#repeatWrongButton');
        const questionResultDisplay = document.querySelector('#questionResult');
        const correctButton = document.querySelector('#correctButton');
        const partiallyCorrectButton = document.querySelector('#partiallyCorrectButton');
        const wrongButton = document.querySelector('#wrongButton');
        const resultDisplay = document.querySelector('#result');
        const answerContainer = document.querySelector('#answerContainer');
        const studentInfoDisplay = document.querySelector('#studentInfo');

        // 显示学生姓名和学号
        studentInfoDisplay.textContent = `学生: ${student_name} 学号: ${student_id}`;

        // 正确重复问题处理 +0.5
        repeatCorrectButton.addEventListener('click', () => {
    updateScore(student_id, 0.5)
        .then(newscore => {
            if (newscore !== null) {
                questionResultDisplay.textContent = `重复正确!${student_id} 的当前积分: ${newscore}`; // 直接使用字符串
                answerContainer.style.display = 'block'; // 显示回答问题部分
            } else {
                console.error('更新分数失败');
                questionResultDisplay.textContent = `更新分数失败,请重试。`;
            }
        })
        .catch(error => {
            console.error('请求失败:', error);
            questionResultDisplay.textContent = `更新分数失败,请重试。`;
        });
});

        // 未准确重复问题处理 -1
        repeatWrongButton.addEventListener('click', () => {
    updateScore(student_id, -1)
        .then(newscore => {
            if (newscore !== null) {
                questionResultDisplay.textContent = `重复错误!${student_id} 的当前积分: ${newscore}`; // 直接使用字符串
                answerContainer.style.display = 'block'; // 显示回答问题部分
            } else {
                console.error('更新分数失败');
                questionResultDisplay.textContent = `更新分数失败,请重试。`;
            }
        })
        .catch(error => {
            console.error('请求失败:', error);
            questionResultDisplay.textContent = `更新分数失败,请重试。`;
        });
});

        // 回答问题的处理
        correctButton.addEventListener('click', () => {
    updateScore(student_id, 3)
        .then(newscore => {
            if (newscore !== null) {
                resultDisplay.textContent = `回答完全正确!${student_id} 的当前积分: ${newscore}`; // 直接使用字符串
            } else {
                console.error('更新分数失败');
                resultDisplay.textContent = `更新分数失败,请重试。`;
            }
        })
        .catch(error => {
            console.error('请求失败:', error);
            resultDisplay.textContent = `更新分数失败,请重试。`;
        });
});


partiallyCorrectButton.addEventListener('click', () => {
    updateScore(student_id, 1.5)
        .then(newscore => {
            if (newscore !== null) {
                resultDisplay.textContent = `回答部分正确!${student_id} 的当前积分: ${newscore}`; // 直接使用字符串
            } else {
                console.error('更新分数失败');
                resultDisplay.textContent = `更新分数失败,请重试。`;
            }
        })
        .catch(error => {
            console.error('请求失败:', error);
            resultDisplay.textContent = `更新分数失败,请重试。`;
        });
});


wrongButton.addEventListener('click', () => {
    updateScore(student_id, 0.5)
        .then(newscore => {
            if (newscore !== null) {
                resultDisplay.textContent = `回答基本正确!${student_id} 的当前积分: ${newscore}`; // 直接使用字符串
            } else {
                console.error('更新分数失败');
                resultDisplay.textContent = `更新分数失败,请重试。`;
            }
        })
        .catch(error => {
            console.error('请求失败:', error);
            resultDisplay.textContent = `更新分数失败,请重试。`;
        });
});

    </script>
</body>
</html>