<?php
include('../includes/session.php');
include('../includes/config.php');
include('../template/ahkweb/header.php');
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);  
    

    $price = mysqli_fetch_assoc(mysqli_query($ahk_conn, "SELECT * FROM pricing WHERE service_name='dlFindfee'"));
    $fee = $price['price'];
    $username = $udata['phone'];
    $wallet_amount = $udata['balance'];
    
    if ($wallet_amount > $fee) {
        $debit_fee = $wallet_amount - $fee;
        
      
        $url = "$tng_apiurl/dl_find_verification?name=$name_encoded&dob=$dob&apikey=$tng_apikey";
        $curl = curl_init();
        curl_setopt_array($curl, array( 
            CURLOPT_URL => $url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 40,
            CURLOPT_TIMEOUT => 40,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'GET',
        ));

        $response = curl_exec($curl);
        curl_close($curl);
        $resdata = json_decode($response, true);

        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") {
            
$parentPhone = $udata['parent'] ?? null;
$appliedby = $udata['phone'] ?? null;
        addCommission($ahk_conn, $appliedby, $fee, $parentPhone, "Name Dob To DL Find Verification");
            $sql = "update users SET balance= balance - $fee where phone='" . $udata[ 'phone' ] . "'";
            $abs = mysqli_query( $ahk_conn, $sql );
            $updatehistory = mysqli_query($ahk_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
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@10"></script>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>
<body>
    <!-- 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">
                                            <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  
                                                    $price = mysqli_fetch_assoc(mysqli_query($ahk_conn,"SELECT price FROM pricing WHERE service_name='dlFindfee'")); 
                                                    echo "₹" .$price['price'];
                                                    ?>
                                                </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/ahkweb/assets/js/bootstrap.bundle.min.js"></script>
	<!--plugins-->
	<!-- <script src="../template/ahkweb/assets/js/jquery.min.js"></script> -->
	<script src="../template/ahkweb/assets/plugins/simplebar/js/simplebar.min.js"></script>
	<script src="../template/ahkweb/assets/plugins/metismenu/js/metisMenu.min.js"></script>
	<script src="../template/ahkweb/assets/plugins/perfect-scrollbar/js/perfect-scrollbar.js"></script>
	<!--app JS-->
	<script src="../template/ahkweb/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/ahkweb/footer.php');
		?>