<?php
include('../includes/session.php');
include('../includes/config.php');
include('../template/ahkweb/header.php');


$price = mysqli_fetch_assoc(mysqli_query($ahk_conn, "SELECT * FROM pricing WHERE service_name='ifsc_lookup'"));
$fee = $price['price'] ?? 0;

$resdata = [];

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

    if ($udata['balance'] >= $fee) {

        $url = "$tng_apiurl/ifscCode_to_Bank_verification?IfscCode=$ifscCode&apikey=$tng_apikey";
        $ch = curl_init();
        curl_setopt_array($ch, [
            CURLOPT_URL => $url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_TIMEOUT => 30
        ]);
        $response = curl_exec($ch);
        $err = curl_error($ch);
        curl_close($ch);

        if ($err || !$response) {
            echo "<script src='https://cdn.jsdelivr.net/npm/sweetalert2@11'></script>
            <script>
            Swal.fire({icon:'error', title:'Error', text:'$err', timer:4000, timerProgressBar:true});
            </script>";
        } else {
            $resdata = json_decode($response, true);
            if ($resdata['sampleCode'] === "200") {
                // Debit wallet
                $parentPhone = $udata['parent'] ?? null;
        addCommission($ahk_conn, $appliedby, $fee, $parentPhone, "IFSC Lookup Verification");
                mysqli_query($ahk_conn, "UPDATE users SET balance=balance-$fee WHERE phone='$appliedby'");
                mysqli_query($ahk_conn, "INSERT INTO `wallethistory`(`userid`,`amount`,`balance`,`purpose`,`status`,`type`) VALUES ('$appliedby','$fee','$debit_fee','IFSC Lookup','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="alert alert-danger" role="alert">
                                                         All State
                                                       <h5>IFSC Code Lookup</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="IfscCode" class="form-label">Enter IFSC Code</label>
                        <input type="text" name="IfscCode" id="IfscCode" class="form-control" placeholder="e.g. IDFB0060321" 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") { ?>
        <div class="col-lg-6">
            <div class="card p-3 shadow-sm bg-light">
                <h5>Bank Details</h5>
                <table class="table table-bordered">
                    <tbody>
                        <tr><th>Branch Name</th><td><?php echo htmlspecialchars($resdata['BranchName']); ?></td></tr>
                        <tr><th>Bank Name</th><td><?php echo htmlspecialchars($resdata['BankName']); ?></td></tr>
                        <tr><th>Bank Code</th><td><?php echo htmlspecialchars($resdata['BankCode']); ?></td></tr>
                        <tr><th>IFSC Code</th><td><?php echo htmlspecialchars($resdata['IfscCode']); ?></td></tr>
                        <tr><th>ISO3166</th><td><?php echo htmlspecialchars($resdata['ISO3166']); ?></td></tr>
                        <tr><th>Center</th><td><?php echo htmlspecialchars($resdata['Center']); ?></td></tr>
                        <tr><th>District</th><td><?php echo htmlspecialchars($resdata['District']); ?></td></tr>
                        <tr><th>City</th><td><?php echo htmlspecialchars($resdata['City']); ?></td></tr>
                        <tr><th>State</th><td><?php echo htmlspecialchars($resdata['State']); ?></td></tr>
                        <tr><th>Contact</th><td><?php echo htmlspecialchars($resdata['Contact']); ?></td></tr>
                        <tr><th>MICR</th><td><?php echo htmlspecialchars($resdata['Micr']); ?></td></tr>
                        <tr><th>Address</th><td><?php echo htmlspecialchars($resdata['Address']); ?></td></tr>
                    </tbody>
                </table>
            </div>
        </div>
        <?php } ?>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <br><br><br><br>
            </div>
        </div>
    </div>
</div>
<?php include('../template/ahkweb/footer.php'); ?>
<script src="../template/ahkweb/assets/js/bootstrap.bundle.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>
<script src="../template/ahkweb/assets/js/app.js"></script>
</html>
