<?php
include('../includes/session.php');
include('../includes/config.php');
include('../template/tngweb/header.php');
$fee    = $udata['upi_id_to_details_fee'];
checkServiceStatus($udata, 'upi_id_to_details_status');

$resdata = [];

if ($_POST['upi_id']) {
    $upi_id = strtoupper(trim($_POST['upi_id']));
    $appliedby = $udata['phone'];
    $debit_fee = $udata['balance'] - $fee;

    if ($udata['balance'] >= $fee) {
        
        $request = [
        'upi_id' => $upi_id,
        ];

        $resdata = upi_to_bank_details_verification_api_run($request);
            if ($resdata['sampleCode'] === "200") {
                
                $parentPhone = $udata['parent'] ?? null;
        addCommission($tng_conn, $appliedby, $fee, $parentPhone, "Upi Id To Bank Details Verification");
                mysqli_query($tng_conn, "UPDATE users SET balance=balance-$fee WHERE phone='$appliedby'");
                mysqli_query($tng_conn, "INSERT INTO `wallethistory`(`userid`,`amount`,`balance`,`purpose`,`status`,`type`) VALUES ('$appliedby','$fee','$debit_fee','Upi Id To Bank Details Verification','1','Debit')");
            } else {
                echo "<script src='https://cdn.jsdelivr.net/npm/sweetalert2@11'></script>
                <script>
                Swal.fire({icon:'error', title:'Error', text:'".$resdata['Message']."', timer:4000, timerProgressBar:true});
                </script>";
            }
    } else {
        echo "<script src='https://cdn.jsdelivr.net/npm/sweetalert2@11'></script>
        <script>
        Swal.fire({icon:'error', title:'Wallet Low', text:'Please Recharge!', timer:3000, timerProgressBar:true});
        </script>";
    }
}
?>

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<div class="content-wrap">
 <div class="main">
<div class="page-wrapper">
    <div class="page-content">
        <div class="mobile-menu-overlay"></div>
        <div class="main-container">
            <div class="col-lg-12">
                <div class="card" style="margin-left: 10px; padding-left: 30px; padding-top: 12px; box-shadow: 1px 5px 5px 5px;">
                    <div class="stat-widget-two">
                        <div class="stat-content">
                            <div class="stat-text">
                                <div class="container-fluid">
                                    <div class="row">
                                        <div class="col-lg-4 col-md-6 col-sm-6">
                                            <div class="card">
                                                <div class="card-body">
                                                     <div class="mb-4">
  <a href="javascript:history.back()" class="back-btn">
    <i class="bi bi-arrow-left-circle"></i> Go Back
  </a>
</div>
                                                    <div class="alert alert-danger" role="alert">
                                                       
                                                       <h5>UPI ID To Bank Details</h5>
                                                    </div>
                                                    <form action="" method="POST" class="row g-3">
                                                        <div class="card-body">
                                                            <div class="col-md-12">
                                                                <div class="form-group">
                                                                   <label for="upi_id" class="form-label">Enter UPI ID</label>
                        <input type="text" name="upi_id" id="upi_id" class="form-control" placeholder="e.g. example@upi" required> </div>
                                                                <hr>
                                                                <div class="row mt-3">
                                                                  <div class="col-md-6">
                                                                       <input class="form-control" value="Fee ₹ <?php echo $fee; ?>" readonly>
                                                                   </div>
                                                                   <div class="col-md-6 text-end">
                                                                       <button class="btn btn-success" name="submit" id="submit"><i class="fa fa-check-circle"></i> Submit</button>
                                                                   </div>
                                                               </div>
                                                            </div>
                                                        </div>
                                                    </form>
                                                </div>
                                            </div>
                                        </div>
                                        <?php if (!empty($resdata) && $resdata['sampleCode'] === "200") {
    $data = $resdata['data'];
?>
<div class="col-lg-6">
    <div class="card p-3 shadow-sm bg-light" id="bankDetails" style="width:100%;">
        <h5>Bank / UPI Details</h5>
        <table class="table table-bordered">
            <tbody>
                <tr><th>Name</th><td><?php echo htmlspecialchars($data['name']); ?></td></tr>
                <tr><th>UPI ID</th><td><?php echo htmlspecialchars($data['upi_id']); ?></td></tr>
                <tr><th>IFSC</th><td><?php echo htmlspecialchars($data['ifsc']); ?></td></tr>
                <tr><th>Bank</th><td><?php echo htmlspecialchars($data['bank']); ?></td></tr>
                <tr><th>Branch</th><td><?php echo htmlspecialchars($data['branch']); ?></td></tr>
                <tr><th>Mobile</th><td><?php echo htmlspecialchars($data['mobile']); ?></td></tr>
            </tbody>
        </table>
        <button id="downloadBtn" class="btn btn-primary mt-2">Download as Image</button>
    </div>
</div>

<!-- Include html2canvas library -->
<script src="https://cdn.jsdelivr.net/npm/html2canvas@1.4.1/dist/html2canvas.min.js"></script>
<script>
document.getElementById("downloadBtn").addEventListener("click", function() {
    html2canvas(document.getElementById("bankDetails")).then(function(canvas) {
        // Convert canvas to image and download
        let link = document.createElement("a");
        link.download = "bank_details.png";
        link.href = canvas.toDataURL("image/png");
        link.click();
    });
});
</script>
<?php } ?>

                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <br><br><br><br>
            </div>
        </div>
    </div>
</div>
<script>
document.addEventListener("DOMContentLoaded", function () {
    const form = document.querySelector("form");
    const submitBtn = document.getElementById("submit");

    let isSubmitting = false;

    form.addEventListener("submit", function (e) {
        if (isSubmitting) {
            e.preventDefault();
            return;
        }
        isSubmitting = true;       
        submitBtn.disabled = true; 
    });
});
</script>
<?php include('../template/tngweb/footer.php'); ?>
<script src="../template/tngweb/assets/js/bootstrap.bundle.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>
<script src="../template/tngweb/assets/js/app.js"></script>
</html>
