<?php
//include html header
require_once("includes/html-header.php");
?>

<body>
    <!--  Body Wrapper -->
    <div class="page-wrapper" id="main-wrapper" data-layout="vertical" data-navbarbg="skin6" data-sidebartype="full" data-sidebar-position="fixed" data-header-position="fixed">

        <?php
        //include sidebar
        require_once("includes/sidebar.php");
        ?>

        <!--  Main wrapper -->
        <div class="body-wrapper">
            <?php
            //include header
            require_once("includes/header.php");
            ?>

            <div class="container-fluid">
            <!-- notice -->
            <marquee class="alert alert-dark" onmouseover="this.stop()" onmouseout="this.start()" role="alert" style="font-size: 1rem;">
                <?php $se = "SELECT * FROM notice";
                $data = mysqli_query($link, $se);
                $d = mysqli_fetch_assoc($data);
                echo $d['notice']; ?>
            </marquee>
            <!-- notice end -->

                <?php
                $id = $_SESSION['alogin'];
                $select = "SELECT * FROM control";
                $g = mysqli_query($link, $select);
                $data = mysqli_fetch_assoc($g);
                $cc_server_unofficial_price = $data['cc_sign2server_price'];
                $sql = "SELECT * FROM users WHERE email='$id'";
                $result = mysqli_query($link, $sql);
                $row = mysqli_fetch_array($result);
                $email = $row['email'];
                $balance = $row['balance'];

                function image($api)
                {
                    // Initialize cURL session
                    $curl = curl_init();
                    curl_setopt($curl, CURLOPT_URL, $api);
                    curl_setopt($curl, CURLOPT_TIMEOUT, 60);
                    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
                    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
                    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
                    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);

                    // Execute cURL and fetch the response
                    $contents = curl_exec($curl);
                    curl_close($curl);

                    // Decode the base64 image data
                    $decodedImage = base64_decode(preg_replace(
                        '/^data:image\/(jpg|jpeg|png);base64,/',
                        '',
                        $contents
                    ));

                    // Generate a unique filename
                    $img = 'image/' . md5($decodedImage) . '.png';

                    // Save the image to the server
                    file_put_contents($img, $decodedImage);

                    return $img;
                }

                if (isset($_POST['nid'], $_POST['dob'])) {
                    // Ensure sufficient balance
                    if ($balance >= $cc_server_unofficial_price) {
                        // Replace with your NID API endpoint
                        $nid = $_POST['nid'];
                        $dob = $_POST['dob'];

                        $url = "http://myal1.bestpoint.site/rhn.php?nid={$nid}&dob={$dob}";
                        $ch = curl_init($url);
                        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
                        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

                        $response = curl_exec($ch);
                        curl_close($ch);

                        $nidInfo = json_decode($response);

                        if ($nidInfo && isset($nidInfo->data)) {
                            $data = $nidInfo->data;

                            if (
                                !empty($data->nameBangla) &&
                                !empty($data->nameEnglish)
                            ) {
                                // Extract required information
                                $userImg = $data->photo;
                                $signatureImg = $data->sign;
                                $nameBangla = $data->nameBangla;
                                $nameEnglish = $data->nameEnglish;
                                $nid = $data->nationalid;
                                $pin = $data->pin;
                                $fatherName = $data->fatherName;
                                $spouseName = $data->spouse ?? 'N/A';
                                $motherName = $data->motherName;
                                $birthPlace = $data->permanentAddress->district;

                                $dateOfBirth = date("d M Y", strtotime($_POST['dob']));
                                $bloodGroup = $data->bloodGroup;
                                $address = "বাসা/হোল্ডিং: " . $data->permanentAddress->homeHolding . ", গ্রাম/রাস্তা: " .
                                    $data->permanentAddress->villageOrRoad . ", ডাকঘর: " . $data->permanentAddress->postOffice .
                                    ",  - " . $data->permanentAddress->postalCode . ", " . $data->permanentAddress->upozila . ", " .
                                    $data->permanentAddress->district;

                                $gender = $data->gender;
                                $maritalStatus = $data->maritalStatus ?? 'Unknown';
                                $religion = $data->religion;

                                // Update user balance
                                $sql2 = "UPDATE users SET balance = balance - $cc_server_unofficial_price WHERE email = '$email'";
                                mysqli_query($link, $sql2);

                                echo '<script>alert("Information successfully loaded!");</script>';
                            } else {
                                echo '<script>alert("Failed to load info, Try again!");</script>';
                            }
                        } else {
                            echo '<script>alert("Error fetching data from server!");</script>';
                        }
                    } else {
                        echo '<script>alert("আপনার পর্যাপ্ত পরিমাণ ব্যালেন্স নাই প্লিজ রিচার্জ করুন");</script>';
                    }
                }
                ?>

                <div class="card">
                    <div class="card-body">
                        <form method="POST" action="#">
                            <div class="mb-3">
                                <label class="form-label">আইডি নাম্বারঃ</label>
                                <input type="number" class="form-control" value="" name="nid" />
                            </div>
                            <div class="mb-3">
                                <label class="form-label">জন্ম তারিখঃ</label>
                                <input type="text" class="form-control" value="" name="dob" />
                            </div>
                            <div class="alert alert-info" role="alert">অনুসন্ধানের জন্য আপনার একাউন্ট থেকে <?php echo $cc_server_unofficial_price; ?> টাকা কাটা হবে।</div>
                            <button type="submit" class="btn btn-primary text-center d-block mx-auto">অনুসন্ধান করুন</button>
                        </form>
                    </div>
                </div>

                <div class="card">
                    <div class="card-body">
                        <form method="POST" action="nid-bn.php" id="nidBN" target="_blank" enctype="multipart/form-data">
                            <div class="mb-3">
                                <label for="exampleInputPassword1" class="form-label">ছবি এবং সিগনাচার</label>
                                <br/>
                                <div class="d-sm-block text-center d-md-flex d-lg-flex justify-content-between align-items-center">
                                    <img style="max-width: 150px; max-height: 150px" id="ImgPreview_1" class="img-thumbnail rounded" src="<?php echo isset($userImg) ? $userImg : ''; ?>" alt="" />
                                    <input type="hidden" value="<?php echo isset($userImg) ? $userImg : ''; ?>" name="imageUrl12"/>
                                    <input type="file" id="Img_1" style="margin: 10px 0;" name="imageUrl1"  onchange="validateImage(1)"/>
                                    <img style="max-width: 100%; max-height: 100px" id="ImgPreview_2" class="img-thumbnail rounded" src="<?php echo isset($signatureImg) ? $signatureImg : ''; ?>" />
                                    <input type="hidden" id="imageUrl22" value="<?php echo isset($signatureImg) ? $signatureImg : ''; ?>" name="imageUrl22" />
                                    <input type="file" id="Img_2" style="margin: 10px 0;" name="imageUrl2" onchange="validateImage(2)"/>
                                    <script>
                                        function validateImage(id) {
                                            const input = document.getElementById(`Img_${id}`);
                                            const preview = document.getElementById(`ImgPreview_${id}`);
                                            const file = input.files[0];

                                            // Check if a file is selected
                                            if (file) {
                                                const fileType = file.type;

                                                // Validate the file type
                                                const validImageTypes = ['image/jpeg', 'image/png', 'image/gif', 'image/webp'];
                                                if (validImageTypes.includes(fileType)) {
                                                    // If valid, create a URL for the image and set it as the src for the preview
                                                    const imageUrl = URL.createObjectURL(file);
                                                    preview.src = imageUrl;
                                                    preview.style.display = 'block'; // Show the preview
                                                } else {
                                                    // If invalid, reset the input and hide the preview
                                                    input.value = ''; // Clear the input
                                                    preview.src = ''; // Clear the preview src
                                                    preview.style.display = 'none'; // Hide the preview
                                                    alert('Please select a valid image file (JPEG, PNG, GIF, or WEBP).');
                                                }
                                            } else {
                                                // If no file selected, hide the preview
                                                preview.src = '';
                                                preview.style.display = 'none';
                                            }
                                        }
                                    </script>
                                </div>
                            </div>

                            <div class="mb-3">
                                <label class="form-label">নামঃ (বাংলা)</label>
                                <input type="text" class="form-control" value="<?php echo isset($nameBangla) ? $nameBangla : ''; ?>" name="nameBangla" />
                            </div>

                            <div class="mb-3">
                                <label class="form-label">নামঃ (ইংরেজী)</label>
                                <input type="text" class="form-control" value="<?php echo isset($nameEnglish) ? $nameEnglish : ''; ?>" name="nameEnglish" />
                            </div>
                            <div class="mb-3">
                                <label class="form-label">আইডি নাম্বারঃ</label>
                                <input type="number" class="form-control" value="<?php echo isset($nid) ? $nid : ''; ?>" name="nid" />
                            </div>

                            <div class="mb-3">
                                <label class="form-label">পিন নাম্বারঃ</label>
                                <input type="number" class="form-control" value="<?php echo isset($pin) ? $pin : ''; ?>" name="pin" />
                            </div>
                            <div class="mb-3">
                                <label class="form-label">পিতার নামঃ</label>
                                <input type="text" class="form-control" value="<?php echo isset($fatherName) ? $fatherName : ''; ?>" name="nameFather" />
                            </div>
                            <div class="mb-3">
                                <label class="form-label">স্বামী অথবা স্ত্রীর নামঃ</label>
                                <input type="text" class="form-control" name="spouseName" value="<?php echo isset($spouseName) ? $spouseName : ''; ?>" />
                            </div>

                            <div class="mb-3">
                                <label class="form-label">মাতার নামঃ</label>
                                <input type="text" class="form-control" value="<?php echo isset($motherName) ? $motherName : ''; ?>" name="nameMother" />
                            </div>

                            <div class="mb-3">
                                <label class="form-label">জন্ম স্থানঃ</label>
                                <input type="text" class="form-control" value="<?php echo isset($birthPlace) ? $birthPlace : ''; ?>" name="birthPlace" />
                            </div>
                            <div class="mb-3">
                                <label class="form-label">জন্ম তারিখঃ</label>
                                <input type="text" class="form-control" value="<?php echo isset($dateOfBirth) ? $dateOfBirth : ''; ?>" name="dob" />
                            </div>
                            <div class="mb-3">
                                <label class="form-label">প্রধানের তারিখঃ</label>
                                <input type="text" class="form-control" value="<?php $newDate = date('d/m/Y'); echo $newDate; ?>" name="issued" />
                            </div>
                            <div class="mb-3">
                                <label class="form-label">রক্তের গ্রপঃ</label>
                                <input type="text" class="form-control" value="<?php echo isset($bloodGroup) ? $bloodGroup : ''; ?>" name="bloodGroup" />
                            </div>
                            <div class="mb-3">
                                <label class="form-label">ঠিকানাঃ</label>
                                <textarea name="fulladdress" id="" class="form-control" cols="30" rows="3"><?php echo isset($address) ? convertToBanglaNumerals(str_replace(['  ', '-'], [' ', '- '], $address)) : ''; ?></textarea>
                            </div>

                            <input type="hidden" name="gender" value="<?php echo isset($gender) ? $gender : ''; ?>">
                            <input type="hidden" name="maritalStatus" value="<?php echo isset($maritalStatus) ? $maritalStatus : ''; ?>">
                            <input type="hidden" name="religion" value="<?php echo isset($religion) ? $religion : ''; ?>">

                            <div class="alert alert-info" role="alert">
                                আপনার একাউন্ট থেকে
                                <?php
                                $sql = "SELECT * FROM control WHERE id='1'";
                                if ($resulttt = mysqli_query($link, $sql)) {
                                    if (mysqli_num_rows($resulttt) > 0) {
                                        while ($row = mysqli_fetch_array($resulttt)) {
                                            echo $row['cc_card_make_price'];
                                        }
                                    }
                                }
                                ?> টাকা কাটা হবে।
                            </div>
                            <button type="submit" class="btn btn-primary text-center d-block mx-auto">
                                ডাউনলোড করুন
                            </button>
                        </form>
                        <script>
                            document.getElementById('nidBN').addEventListener('submit', function (e) {
                                e.preventDefault();
                                const imageUrl = document.getElementById('imageUrl22').value.trim();
                                const imageFile = document.getElementById('Img_2').files[0];
                                if (imageUrl || imageFile) {
                                    this.submit();
                                } else {
                                    alert('Please select sing image file.');
                                }
                            });
                        </script>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script src="assets/libs/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
    <script src="assets/js/sidebarmenu.js"></script>
    <script src="assets/js/app.min.js"></script>
    <script src="assets/libs/simplebar/dist/simplebar.js"></script>

    <script>
        var signCopy = $("#signCopyBox");
        var detailsForm = $(".detailsForm");

        signCopy.change(function(e) {
            var formData = new FormData();
            var fileInput = $("#signCopyBox")[0].files[0];
            formData.append("pdf", fileInput);

            detailsForm.empty();
            detailsForm.append('<div class=\"alert alert-warning text-center\" role=\"alert\"><img src=\"https://media.tenor.com/wpSo-8CrXqUAAAAj/loading-loading-forever.gif\" style="width: 20px;margin-right:15px">ডাটা লোড হচ্ছে।</div>');

            $.ajax({
                url: 'nid.php',
                type: 'POST',
                data: formData,
                processData: false,
                contentType: false,
                success: function(data) {
                    detailsForm.empty();
                    $("#signCopyBox").val(null);
                    detailsForm.append(data);
                }
            });
        });
    </script>
</body>

</html>
