<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>NID and DOB Data Fetcher</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            background-color: #f4f4f4;
        }
        .container {
            width: 100%;
            max-width: 600px;
            padding: 20px;
            box-sizing: border-box;
        }
        .form-container, .result-container {
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            padding: 20px;
            border-radius: 10px;
            background-color: #fff;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        }
        .form-container {
            margin-bottom: 20px;
        }
        label {
            display: block;
            margin-bottom: 8px;
            font-weight: bold;
        }
        input {
            padding: 8px;
            margin-bottom: 12px;
            width: 100%;
            max-width: 300px;
            box-sizing: border-box;
        }
        button {
            padding: 10px 15px;
            background-color: #007BFF;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            width: 100%;
            max-width: 300px;
        }
        button:hover {
            background-color: #0056b3;
        }
        .result-container img {
            max-width: 150px;
            display: block;
            margin-bottom: 10px;
            margin-left: auto;
            margin-right: auto;
        }
        .result-container p {
            font-size: 14px;
            margin: 5px 0;
        }
    </style>
</head>
<body>

<div class="container">
    <h1 style="text-align: center;">Nid Information</h1>

    <div id="form-container" class="form-container">
        <form id="dataForm">
            <label for="nid">National ID (NID):</label>
            <input type="text" id="nid" name="nid" required>

            <label for="dob">Date of Birth (DOB):</label>
            <input placeholder="YYYY-MM-DD" type="text" id="dob" name="dob" required>

            <button type="submit">Verify NID</button>
        </form>
    </div>

    <div class="result-container" id="resultContainer" style="display: none;">
        <img id="photo" src="" alt="Photo" style="display: none;">
        <p><strong>Name (Bengali):</strong> <span id="nameBn"></span></p>
        <p><strong>Name (English):</strong> <span id="nameEn"></span></p>
        <p><strong>Gender:</strong> <span id="gender"></span></p>
        <p><strong>Father:</strong> <span id="father"></span></p>
        <p><strong>Mother:</strong> <span id="mother"></span></p>
        <p><strong>Spouse:</strong> <span id="spouse"></span></p>
        <p><strong>Blood Group:</strong> <span id="bloodGroup"></span></p>
        <p><strong>Occupation:</strong> <span id="occupation"></span></p>
        <p><strong>National ID:</strong> <span id="nationalId"></span></p>
        <p><strong>Nid (13 Digit):</strong> <span id="pin"></span></p>
        <p><strong>Date of Birth:</strong> <span id="dateOfBirth"></span></p>
        <p><strong>Permanent Address:</strong> <span id="permanentAddress"></span></p>
        <p><strong>Present Address:</strong> <span id="presentAddress"></span></p>
        <br>
        <button class="reload-btn" onclick="reload()" type="submit">Check Another</button>

    </div>
</div>
<style>
    .reload-btn{
        display: flex;
        justify-content: center;
        margin-inline: auto;
        background-color: #ff6457;
    }
</style>
<script>
    function reload(){
        location.reload();
    }
</script>
<script>
    document.addEventListener('DOMContentLoaded', () => {
        const form = document.getElementById('dataForm');
        const resultContainer = document.getElementById('resultContainer');
        const photoElement = document.getElementById('photo');
        const formContainer = document.getElementById('form-container'); // Form container element

        // Helper function to check if value is missing and return 'N/A'
        function checkValue(value) {
            return value ? value : 'N/A';
        }

        form.addEventListener('submit', async (e) => {
            e.preventDefault();

            const nid = document.getElementById('nid').value;
            const dob = document.getElementById('dob').value;

            try {
                const response = await fetch(`https://alphasierra.com.bd/nid-proxy/proxy.php?nid=${encodeURIComponent(nid)}&dob=${encodeURIComponent(dob)}`);
                if (response.ok) {
                    const data = await response.json();
                    console.log(data);
                    document.getElementById('nameBn').textContent = checkValue(data.data.name);
                    document.getElementById('nameEn').textContent = checkValue(data.data.nameEn);
                    document.getElementById('gender').textContent = checkValue(data.data.gender);
                    document.getElementById('father').textContent = checkValue(data.data.father);
                    document.getElementById('mother').textContent = checkValue(data.data.mother);
                    document.getElementById('spouse').textContent = checkValue(data.data.spouse);
                    document.getElementById('bloodGroup').textContent = checkValue(data.data.bloodGroup);
                    document.getElementById('occupation').textContent = checkValue(data.data.occupation);
                    document.getElementById('nationalId').textContent = checkValue(data.data.nationalId);
                    document.getElementById('pin').textContent = checkValue(data.data.pin);
                    document.getElementById('dateOfBirth').textContent = checkValue(data.data.dateOfBirth);
                    document.getElementById('permanentAddress').textContent = checkValue(data.data.permanentAddress.address_line);
                    document.getElementById('presentAddress').textContent = checkValue(data.data.presentAddress.address_line);
                    // Set the photo URL from the API response
                    if (data.data.photoUrl) {
                        photoElement.src = data.data.photoUrl;
                        photoElement.style.display = 'block';
                    } else {
                        photoElement.style.display = 'none';
                    }

                    resultContainer.style.display = 'block';

                    // Hide the form container after successful data retrieval
                    formContainer.style.display = 'none';
                } else {
                    alert('Failed to fetch data. Please check the inputs.');
                }
            } catch (error) {
                console.error('Error:', error);
                alert('An error occurred while fetching data.');
            }
        });
    });
</script>


</body>
</html>
