<?php
include('../includes/session.php');
include('../includes/config.php');
include('../template/tngweb/header.php');
$fee    = $udata['challan_fee'];
checkServiceStatus($udata, 'challan_fee_status');
if (isset($_POST['rcno'])) {
    $bikeNumber = $_POST['rcno'] ?? null;
    $username = $udata['phone'];
    $wallet = $udata['balance'];

    if ($wallet > $fee) {
       
        $request = [
        'rcno' => $bikeNumber,
        ];

        $resdata = vehicle_chalan_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['Message'] == "Success") {
             $debit_fee = $wallet - $fee;
            $parentPhone = $udata['parent'] ?? null;
        addCommission($tng_conn, $username, $fee, $parentPhone, "Challan Details Find");
            $debit = mysqli_query($tng_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($tng_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
    }
}
?>

    <!-- 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">
                                                <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>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 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>
                </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/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>
</body>
</html>


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