<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Test Awards and Punishments 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 Awards and Punishments API</h2>

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

    <h3>Update Awards and Punishments</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="award_name">Award Name:</label>
    <input type="text" id="award_name" placeholder="Enter award name">
    <br><br>
    <label for="award_date">Award Date:</label>
    <input type="date" id="award_date">
    <br><br>
    <label for="award_organization">Award Organization:</label>
    <input type="text" id="award_organization" placeholder="Enter award organization">
    <br><br>
    <label for="discipline_date">Discipline Date (optional):</label>
    <input type="date" id="discipline_date">
    <br><br>
    <label for="discipline_issue">Discipline Issue (optional):</label>
    <input type="text" id="discipline_issue" placeholder="Enter discipline issue">
    <br><br>
    <label for="discipline_outcome">Discipline Outcome (optional):</label>
    <input type="text" id="discipline_outcome" placeholder="Enter discipline outcome">
    <br><br>
    <button onclick="updateAwardsPunishments()">Update Awards and Punishments</button>

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

    <script>
        // Function to get awards and punishments via GET request
        function getAwardsPunishments() {
            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/awards_punishments/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 = JSON.stringify(data, null, 2);
                }
            })
            .catch(error => {
                resultDiv.textContent = `Error: ${error.message}`;
            });
        }

        // Function to update awards and punishments via POST request
        function updateAwardsPunishments() {
            const userId = document.getElementById('user_id_post').value;
            const awardName = document.getElementById('award_name').value;
            const awardDate = document.getElementById('award_date').value;
            const awardOrganization = document.getElementById('award_organization').value;
            const disciplineDate = document.getElementById('discipline_date').value;
            const disciplineIssue = document.getElementById('discipline_issue').value;
            const disciplineOutcome = document.getElementById('discipline_outcome').value;
            const resultDiv = document.getElementById('post-result');

            if (!userId || !awardName || !awardDate || !awardOrganization) {
                resultDiv.textContent = "Please fill out all required fields.";
                return;
            }

            const postData = {
                award_name: awardName,
                award_date: awardDate,
                award_organization: awardOrganization,
                discipline_date: disciplineDate || null,
                discipline_issue: disciplineIssue || null,
                discipline_outcome: disciplineOutcome || null
            };

            fetch(`/ec_user/awards_punishments/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>
