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

    <h3>Get Contact 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="getContactInfo()">Get Contact Info</button>
    
    <div id="get-result" class="message"></div>

    <h3>Update Contact 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="home_address">Home Address:</label>
    <input type="text" id="home_address" placeholder="Enter home address">
    <br><br>
    <label for="parent_contact">Parent Contact:</label>
    <input type="text" id="parent_contact" placeholder="Enter parent contact">
    <br><br>
    <label for="student_contact">Student Contact:</label>
    <input type="text" id="student_contact" placeholder="Enter student contact">
    <br><br>
    <label for="email">Email:</label>
    <input type="email" id="email" placeholder="Enter email">
    <br><br>
    <button onclick="updateContactInfo()">Update Contact Info</button>

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

    <script>
        // Function to get contact info via GET request
        function getContactInfo() {
            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/contact_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 = `Home Address: ${data.home_address}, Parent Contact: ${data.parent_contact}, Student Contact: ${data.student_contact}, Email: ${data.email}`;
                }
            })
            .catch(error => {
                resultDiv.textContent = `Error: ${error.message}`;
            });
        }

        // Function to update contact info via POST request
        function updateContactInfo() {
            const userId = document.getElementById('user_id_post').value;
            const homeAddress = document.getElementById('home_address').value;
            const parentContact = document.getElementById('parent_contact').value;
            const studentContact = document.getElementById('student_contact').value;
            const email = document.getElementById('email').value;
            const resultDiv = document.getElementById('post-result');

            if (!userId || !homeAddress || !parentContact || !studentContact || !email) {
                resultDiv.textContent = "Please fill out all fields.";
                return;
            }

            const postData = {
                home_address: homeAddress,
                parent_contact: parentContact,
                student_contact: studentContact,
                email: email
            };

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