<?php
include('../includes/session.php');
include('../includes/config.php');
include('../template/ahkweb/header.php');
if (isset($_POST['rcno'])) {
    $bikeNumber = $_POST['rcno'] ?? null;
    
    $price = mysqli_fetch_assoc(mysqli_query($ahk_conn, "SELECT * FROM pricing WHERE service_name='challan_fee'"));
    $fee = $price['price'];
    $username = $udata['phone'];
    $wallet = $udata['balance'];

    if ($wallet > $fee) {
        $debit_fee = $wallet - $fee;

       
        $url = "$tng_apiurl/vehicle_chalan_verification?rcno=$bikeNumber&apikey=$tng_apikey";

        $curl = curl_init();
        curl_setopt_array($curl, [
            CURLOPT_URL => $url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 30,
            CURLOPT_TIMEOUT => 30,
            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['Message'] == "Success") {
            $parentPhone = $udata['parent'] ?? null;
        addCommission($ahk_conn, $username, $fee, $parentPhone, "Challan Details Find");
            $debit = mysqli_query($ahk_conn, "UPDATE `users` SET balance='$debit_fee' WHERE phone='$username'");
            date_default_timezone_set('Asia/Kolkata');
            $timestamp = date("Y-m-d H:i:s");      
            $updatehistory = mysqli_query($ahk_conn,"INSERT INTO `wallethistory`(`userid`, `amount`, `balance`, `purpose`, `status`, `type`) VALUES ('$username','$fee','$debit_fee','Challan Details Find','1','Debit')");


            ?>
            <script>
                $(function(){
                    Swal.fire(
                        '<?php echo $resdata['message']; ?>',
                        'Status: <?php echo $resdata['Message']; ?>!',
                        'success'
                    )
                })
                setTimeout(() => {
                    window.location='#';
                }, 12000);
            </script>
            <?php

        } 
    } else {
        ?>
        <script>
            $(function(){
                Swal.fire(
                    'Oops',
                    '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-4">
                            <div class="main-content">
                                <div class="card card-default">
                                    <div class="card-header bg-warning">
                                        <div class="card-title">
                                            <h3><strong>VEHICLE CHALLAN 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="rcno">Vehical Number <span class="required-mark text-red" style="color:red;">*</span></label>
                                                <input type="text" class="form-control" name="rcno" id="rcno" placeholder="Enter Vehical Number">
                                            </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='challan_fee'")); 
                                                    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>
                </div>
                    <div class="col-md-12">
                        <?php if (isset($resdata) && $resdata['Message'] == "Success") : ?>
                            <div class="table-responsive">
                                <table class="table table-bordered table-striped">
                                    <thead>
                                       
                                       <tr>
                                           <th class="text-center">Vehicle No</th>
                                           <th class="text-center">Owner Name</th>
                                           <th class="text-center">Model</th>
                                           <th class="text-center">Company</th>
                                           <th class="text-center">Type</th>
                                           <th class="text-center">Fuel</th>
                                           <?php if (!empty($resdata['challans'])) : ?>
                                               <th class="text-center">Challan No</th>
                                               <th class="text-center">Amount</th>
                                               <th class="text-center">Status</th>
                                               <th class="text-center">Penalty</th>
                                           <?php endif; ?>
                                       </tr>
                                    </thead>
                                    <tbody>
                                        <?php 
                                        // Display vehicle details
                                        $vehicle = $resdata['vehicle'];
                                        if (!empty($resdata['challans'])) :
                                            foreach ($resdata['challans'] as $challan): 
                                        ?>
                                            <tr>
                                                <td><?= $vehicle['registration_number']; ?></td>
                                                <td><?= $challan['owner_name']; ?></td>
                                                <td><?= $vehicle['model_name']; ?></td>
                                                <td><?= $vehicle['vehicle_name']; ?></td>
                                                <td><?= $vehicle['vehicle_type_v2']; ?></td>
                                                <td><?= $vehicle['fuel_type']; ?></td>
                                                <td><B><?= $challan['challan_number']; ?></B></td>
                                                <td><button class="btn btn-primary"><B> ₹ <?= $challan['amount']; ?> </B></button></td>
                                                <td>
                                                    <?php if ($challan['challan_status'] == 'PAID'): ?>
                                                        <button class="btn btn-success"><B>PAID </B></button>
                                                    <?php else: ?>
                                                        <button class="btn btn-warning"><B>UNPAID</B></button>
                                                    <?php endif; ?>
                                                </td>
                                                <td><B><?= $challan['penalty']; ?></B></td>
                                            </tr>
                                        <?php 
                                            endforeach; 
                                        else: 
                                        ?>
                                            <tr>
                                                <td><?= $vehicle['registration_number']; ?> <button class="btn btn-success"><B>CHALLAN NOT FOUND</B></button></td>
                                                <td><?= $vehicle['owner_name']; ?></td>
                                                <td><?= $vehicle['model_name']; ?></td>
                                                <td><?= $vehicle['vehicle_name']; ?></td>
                                                <td><?= $vehicle['vehicle_type_v2']; ?></td>
                                                <td><?= $vehicle['fuel_type']; ?></td>
                                            </tr>
                                        <?php endif; ?>
                                    </tbody>
                                </table>
                            </div>
                        <?php endif; ?>
                    </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>
</body>
</html>


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