<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Test Academic Info API</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
        }
        .message {
            margin-top: 10px;
            padding: 10px;
            background-color: #f4f4f4;
            border: 1px solid #ddd;
        }
    </style>
</head>
<body>
    <h2>Test Academic Info API</h2>

    <h3>Get Academic Info</h3>
    <label for="user_id_get">User ID:</label>
    <input type="number" id="user_id_get" min="1" placeholder="Enter user ID">
    <button onclick="getAcademicInfo()">Get Academic Info</button>
    
    <div id="get-result" class="message"></div>

    <h3>Update Academic Info</h3>
    <label for="user_id_post">User ID:</label>
    <input type="number" id="user_id_post" min="1" placeholder="Enter user ID">
    <br><br>
    <label for="last_semester_score">Last Semester Score:</label>
    <input type="number" id="last_semester_score" placeholder="Enter last semester score" step="0.01">
    <br><br>
    <label for="this_semester_score">This Semester Score:</label>
    <input type="number" id="this_semester_score" placeholder="Enter this semester score" step="0.01">
    <br><br>
    <label for="class_ranking">Class Ranking:</label>
    <input type="number" id="class_ranking" placeholder="Enter class ranking">
    <br><br>
    <label for="strong_subject">Strong Subject:</label>
    <input type="text" id="strong_subject" placeholder="Enter strong subject">
    <br><br>
    <label for="weak_subject">Weak Subject:</label>
    <input type="text" id="weak_subject" placeholder="Enter weak subject">
    <br><br>
    <button onclick="updateAcademicInfo()">Update Academic Info</button>

    <div id="post-result" class="message"></div>

    <script>
        // Function to get academic info via GET request
        function getAcademicInfo() {
            const userId = document.getElementById('user_id_get').value;
            const resultDiv = document.getElementById('get-result');

            if (!userId) {
                resultDiv.textContent = "Please enter a valid user ID.";
                return;
            }

            fetch(`/ec_user/academic_info/api/`, {
                method: 'GET',
                headers: {
                    'Content-Type': 'application/json',
                    'token': '486ef8198bb10ba6878eba95771c064ff64db81a'
                },
            })
            .then(response => response.json())
            .then(data => {
                if (data.error) {
                    resultDiv.textContent = `Error: ${data.error}`;
                } else {
                    resultDiv.textContent = `Last Semester Score: ${data.last_semester_score}, This Semester Score: ${data.this_semester_score}, Class Ranking: ${data.class_ranking}, Strong Subject: ${data.strong_subject}, Weak Subject: ${data.weak_subject}`;
                }
            })
            .catch(error => {
                resultDiv.textContent = `Error: ${error.message}`;
            });
        }

        // Function to update academic info via POST request
        function updateAcademicInfo() {
            const userId = document.getElementById('user_id_post').value;
            const lastSemesterScore = document.getElementById('last_semester_score').value;
            const thisSemesterScore = document.getElementById('this_semester_score').value;
            const classRanking = document.getElementById('class_ranking').value;
            const strongSubject = document.getElementById('strong_subject').value;
            const weakSubject = document.getElementById('weak_subject').value;
            const resultDiv = document.getElementById('post-result');

            if (!userId || !lastSemesterScore || !thisSemesterScore || !classRanking || !strongSubject || !weakSubject) {
                resultDiv.textContent = "Please fill out all fields.";
                return;
            }

            const postData = {
                last_semester_score: lastSemesterScore,
                this_semester_score: thisSemesterScore,
                class_ranking: classRanking,
                strong_subject: strongSubject,
                weak_subject: weakSubject
            };

            fetch(`/ec_user/academic_info/api/`, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                    'token': '486ef8198bb10ba6878eba95771c064ff64db81a'
                },
                body: JSON.stringify(postData)
            })
            .then(response => response.json())
            .then(data => {
                if (data.error) {
                    resultDiv.textContent = `Error: ${data.error}`;
                } else {
                    resultDiv.textContent = data.message;
                }
            })
            .catch(error => {
                resultDiv.textContent = `Error: ${error.message}`;
            });
        }
    </script>
</body>
</html>
