<?php
include('../includes/session.php');
include('../includes/config.php');
include('../template/tngweb/header.php');
$fee    = $udata['dlFindfee'];
checkServiceStatus($udata, 'dlFindfee_status');
if (isset($_POST['TNG']) && $_POST['TNG'] == "thenextgenapi") {
    $name = $_POST['Name'] ?? null;
    $dob = $_POST['dob'] ?? null; 
    $MobileNumber = $_POST['MobileNumber'] ?? null;
    $name_cleaned = trim($name);  
    $name_encoded = urlencode($name_cleaned);
    $name_encoded = str_replace('+', '%20', $name_encoded);  
    $username = $udata['phone'];
    $wallet_amount = $udata['balance'];
    
    if ($wallet_amount > $fee) {
        
        $request = [
        'name' => $name_encoded,
        'dob' => $dob,
        ];

        $resdata = dl_find_verification_api_run($request);

        if ($resdata['error']) {
            ?>
            <script>
                $(function(){
                    Swal.fire(
                        '<?php echo $resdata['error']; ?>',
                        'Please Try After Sometime',
                        'warning'
                    )
                });
                window.setTimeout(function(){
                    window.location.href='#';
                }, 20000);
            </script>
            <?php
        } elseif ($resdata['sampleCode'] == "sampleCode") {
            $debit_fee = $wallet_amount - $fee;
$parentPhone = $udata['parent'] ?? null;
$appliedby = $udata['phone'] ?? null;
        addCommission($tng_conn, $appliedby, $fee, $parentPhone, "Name Dob To DL Find Verification");
            $sql = "update users SET balance= balance - $fee where phone='" . $udata[ 'phone' ] . "'";
            $abs = mysqli_query( $tng_conn, $sql );
            $updatehistory = mysqli_query($tng_conn,"INSERT INTO `wallethistory`(`userid`, `amount`, `balance`, `purpose`, `status`, `type`) VALUES ('$username','$fee','$debit_fee','Name Dob To DL Find','1','Debit')");
                ?>
                <script>
                    $(function(){
                        Swal.fire(
                            'DL Found Successfully ',
                            'Status: <?php echo $resdata['Message']; ?>!',
                            'success'
                        )
                    })
                    setTimeout(() => {
                        window.location='#';
                    }, 12000);
                </script>
                <?php
            
        } 
    } else {
        ?>
        <script>
            $(function(){
                Swal.fire(
                    'Opps',
                    'Wallet Balance Insufficient! Please Recharge',
                    'error'
                )
            });
            window.setTimeout(function(){
                window.location.href='wallet.php';
            }, 10000);
        </script>
        <?php
    }
}
?>

    <!-- Start page wrapper -->
    <div class="page-wrapper">
        <div class="page-content">
            <div class="main">
                <div class="container-fluid">
                    <div class="row">
                        <div class="col-md-6">
                            <div class="main-content">
                                <div class="card card-default">
                                    <div class="card-header bg-warning">
                                        <div class="card-title">
                                                <div class="mb-4">
        <a href="javascript:history.back()" class="back-btn">
            <i class="bi bi-arrow-left-circle"></i> Go Back
        </a>
    </div>
                                            <h3><strong>DRIVING LICENCE DETAILS FIND INSTANT</strong></h3>
                                        </div>
                                    </div>
                                </div>
                                <div class="card col-12">
                                    <hr>
                                    <div class="row dgnform">
                                        <form name="" action="" method="post" id="rasan_print">
                                            <div class="card-body">
                                                <div class="col-md-12">
                                                    <div class="form-group">
                                                        <label class="card-title" for="findRequired">Select Details <span class="required-mark text-red" style="color:red;">*</span></label>
                                                        <input type="hidden" name="TNG" value="thenextgenapi">
                                                        <select name="findRequired" id="findRequired" required="" class="form-control">
                                                            <option value="0">-Select Method-</option>
                                                            <option hidden value="MobileNo">Mobile Number</option>
                                                            <option value="nameDob">Name & DOB</option>
                                                        </select>
                                                    </div>
                                                </div>
                                                <div id="clientNameField" style="display: none;">
                                                    <div class="col-md-12">
                                                        <div class="form-group">
                                                            <label class="card-title" for="Name">Name <span class="required-mark text-red" style="color:red;">*</span></label>
                                                            <input type="text" class="form-control" name="Name" id="Name" placeholder="Enter Full Name">
                                                        </div>
                                                    </div>
                                                </div>
                                                <div id="clientdobField" style="display: none;">
                                                    <div class="col-md-12">
                                                        <div class="form-group">
                                                            <label class="card-title" for="dob">Date Of Birth <span class="required-mark text-red" style="color:red;">*</span></label>
                                                            <input class="form-control mt-2" name="dob" placeholder="DD-MM-YYYY" type="text" maxlength="10" pattern="\d{2}-\d{2}-\d{4}" title="Please enter dob in DD-MM-YYYY format">
                                                        </div>
                                                    </div>
                                                </div>
                                                <div id="MobileField" style="display: none;">
                                                    <div class="col-md-12">
                                                        <div class="form-group">
                                                            <label class="card-title" for="MobileNumber">Mobile Number</label>
                                                            <input type="number" class="form-control" name="MobileNumber" id="MobileNumber" placeholder="Enter 10 Digit Mobile Number">
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                            <div class="col-12 ml-2">
                                                <h5 class="text-warning">Application Fee: 
                                                    <?php  
                                                    echo "₹" .$fee;
                                                    ?>
                                                </h5>
                                            </div>
                                            <div class="row row-sm mg-t-20">
                                                <div class="col">
                                                    <button type="submit" class="btn btn-primary w-100"><i class="fa fa-check-circle"></i> Submit</button>
                                                </div>
                                            </div>
                                        </form>
                                    </div>    
                                </div>
                            </div>
                        </div>
                        <div class="col-md-6">
                             <?php if (isset($resdata) && $resdata['sampleCode'] == "200") : ?>
                            <!-- Table will be displayed here if there is any data -->
                                <div class="table-responsive">
                                    <table class="table table-bordered table-striped">
                                        <thead>
                                            <tr>
                                                <th>DL No</th>
                                                <th>Name</th>
                                                <th>Father's Name</th>
                                                <th>DOB</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <?php foreach ($resdata['result'] as $row): ?>
                                                <tr>
                                                    <td><?= $row['dlno']; ?></td>
                                                    <td><?= $row['name']; ?></td>
                                                    <td><?= $row['fathersname']; ?></td>
                                                    <td><?= $row['dob']; ?></td>
                                                </tr>
                                            <?php endforeach; ?>
                                        </tbody>
                                    </table>
                                </div>
                        </div>  <?php endif; ?>
                    </div>
                </div>
            </div>
        </div>
    </div>
	<script src="../template/tngweb/assets/js/bootstrap.bundle.min.js"></script>
	<!--plugins-->
	<!-- <script src="../template/tngweb/assets/js/jquery.min.js"></script> -->
	<script src="../template/tngweb/assets/plugins/simplebar/js/simplebar.min.js"></script>
	<script src="../template/tngweb/assets/plugins/metismenu/js/metisMenu.min.js"></script>
	<script src="../template/tngweb/assets/plugins/perfect-scrollbar/js/perfect-scrollbar.js"></script>
	<!--app JS-->
	<script src="../template/tngweb/assets/js/app.js"></script>
    <script>
        // Handle select option change
        document.getElementById('findRequired').addEventListener('change', function() {
            var selectedValue = this.value;
            document.getElementById('clientNameField').style.display = 'none';
            document.getElementById('clientdobField').style.display = 'none';
            document.getElementById('MobileField').style.display = 'none';

            if (selectedValue === 'MobileNo') {
                document.getElementById('MobileField').style.display = 'block';
            } else if (selectedValue === 'nameDob') {
                document.getElementById('clientNameField').style.display = 'block';
                document.getElementById('clientdobField').style.display = 'block';
            }
        });
    </script>
</body>
</html>


		<?php 
		include('../template/tngweb/footer.php');
		?>