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

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

    <h3>Update Family 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="family_member">Family Members (JSON format):</label>
    <textarea id="family_member" placeholder='Enter family member info as JSON (e.g., [{"name": "John", "relationship": "Father"}])'></textarea>
    <br><br>
    <label for="economic_status">Economic Status:</label>
    <input type="text" id="economic_status" placeholder="Enter economic status">
    <br><br>
    <button onclick="updateFamilyInfo()">Update Family Info</button>

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

    <script>
        // Function to get family info via GET request
        function getFamilyInfo() {
            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/family_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 = `Family Members: ${JSON.stringify(data.family_member)}, Economic Status: ${data.economic_status}`;
                }
            })
            .catch(error => {
                resultDiv.textContent = `Error: ${error.message}`;
            });
        }

        // Function to update family info via POST request
        function updateFamilyInfo() {
            const userId = document.getElementById('user_id_post').value;
            const familyMember = document.getElementById('family_member').value;
            const economicStatus = document.getElementById('economic_status').value;
            const resultDiv = document.getElementById('post-result');

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

            try {
                // Convert the family_member field to JSON
                const familyMemberJSON = JSON.parse(familyMember);

                const postData = {
                    family_member: familyMemberJSON,
                    economic_status: economicStatus
                };

                fetch(`/ec_user/family_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}`;
                });
            } catch (error) {
                resultDiv.textContent = "Invalid JSON format for family members.";
            }
        }
    </script>
</body>
</html>
