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

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

    <h3>Add Hobbies and Interests</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="interests">Interests:</label>
    <textarea id="interests" placeholder="Enter interests"></textarea>
    <br><br>
    <label for="extracurricular_activities">Extracurricular Activities:</label>
    <textarea id="extracurricular_activities" placeholder="Enter extracurricular activities"></textarea>
    <br><br>
    <button onclick="addHobbiesInterests()">Add Hobbies and Interests</button>

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

    <script>
        // Function to get hobbies and interests via GET request
        function getHobbiesInterests() {
            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/hobbies_interests/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 {
                    let resultText = '';
                    data.forEach(entry => {
                        resultText += `Interests: ${entry.interests}, Extracurricular Activities: ${entry.extracurricular_activities}<br>`;
                    });
                    resultDiv.innerHTML = resultText;
                }
            })
            .catch(error => {
                resultDiv.textContent = `Error: ${error.message}`;
            });
        }

        // Function to add hobbies and interests via POST request
        function addHobbiesInterests() {
            const userId = document.getElementById('user_id_post').value;
            const interests = document.getElementById('interests').value;
            const extracurricularActivities = document.getElementById('extracurricular_activities').value;
            const resultDiv = document.getElementById('post-result');

            if (!userId || !interests || !extracurricularActivities) {
                resultDiv.textContent = "Please fill out all fields.";
                return;
            }

            const postData = {
                interests: interests,
                extracurricular_activities: extracurricularActivities
            };

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