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

$fee = $price['price'] ?? 0;
$resdata = null; // Response data initialize

if (isset($_POST['aadhar'])) {
    $aadhar = $_POST['aadhar'];
    $appliedby = $udata['phone'];

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

        // Make sure function name matches your functions.php
        $resdata = aadhar_to_pan_status_run($request); 

        // Check if API failed or status is not 100
        if (!$resdata || $resdata['status'] != 100) {
            $errore = $resdata['statusMessage'] ?? $resdata['message'] ?? 'Unable to connect to API';
            echo '
            <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
            <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
            <script>
                $(function(){
                    Swal.fire({
                        icon: "error",
                        title: "Error",
                        text: "' . $errore . '",
                        timer: 5000,
                        timerProgressBar: true
                    });
                });
            </script>';
        }
    } else {
         echo '
        <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
        <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
        <script>
            $(function(){
                Swal.fire({
                    icon: "error",
                    title: "Wallet Balance is Low!",
                    text: "Please Recharge Now!",
                    timer: 2000,
                    timerProgressBar: true
                });
                setTimeout(() => { window.location = "wallet.php"; }, 1200);
            });
        </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: 25px; box-shadow: 1px 5px 15px rgba(0,0,0,0.1); border-radius: 10px;">
                            <div class="container-fluid">
                                <div class="row">
                                    <div class="col-lg-4 col-md-6 col-sm-12">
                                        <div class="mb-4">
                                            <a href="javascript:history.back()" class="btn btn-sm btn-outline-secondary">
                                                <i class="bi bi-arrow-left-circle"></i> Go Back
                                            </a>
                                        </div>
                                        <div class="alert alert-danger" role="alert" style="font-weight: bold;">
                                            All State Aadhar To Pan Link Status Find
                                        </div>
                                        
                                        <form action="" method="POST" class="row g-3">
                                            <div class="col-md-12">
                                                <div class="form-group mb-3">
                                                    <label for="aadhar">Enter Aadhar Number</label>
                                                    <input name="aadhar" type="text" id="aadhar" placeholder="12 Digit Aadhar Number" class="form-control" required maxlength="12">
                                                </div>
                                                <hr>
                                                <div class="row mt-3">
                                                    <div class="col-6">
                                                        <input class="form-control" value="Fee ₹ <?php echo $fee; ?>" readonly>
                                                    </div>
                                                    <div class="col-6 text-end">
                                                        <button class="btn btn-success w-100" name="submit" id="submit">
                                                            <i class="fa fa-check-circle"></i> Submit
                                                        </button>
                                                    </div>
                                                </div>
                                            </div>
                                        </form>
                                    </div>

                                    <?php if (isset($resdata['status']) && $resdata['status'] == 100) { ?>
                                    <div class="col-lg-8 col-md-6 col-sm-12">
                                        <div class="card" style="background-color: #FCF3CF; border: 1px solid #F7DC6F;" id="printSection">
                                            <div class="card-body">
                                                <h5 class="mb-3 text-dark"><i class="fa fa-info-circle"></i> Result Details</h5>
                                                <div class="table-responsive">
                                                    <table class="table table-bordered table-striped bg-white">
                                                        <tbody>
                                                            <tr>
                                                                <th width="30%">Aadhar Number</th>
                                                                <td style="font-weight: bold;"><?php echo htmlspecialchars($resdata['uidNumber']); ?></td>
                                                            </tr>
                                                            <tr>
                                                                <th>Message</th>
                                                                <td class="text-danger" style="font-weight: bold;">
                                                                    <?php echo htmlspecialchars($resdata['message']); ?>
                                                                </td>
                                                            </tr>
                                                            <tr>
                                                                <th>Status</th>
                                                                <td class="text-success"><?php echo htmlspecialchars($resdata['statusMessage']); ?></td>
                                                            </tr>
                                                        </tbody>
                                                    </table>
                                                </div>
                                                <div class="mt-2 text-end">
                                                    <button onclick="window.print()" class="btn btn-sm btn-primary">Print Result</button>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                    <?php } ?>

                                </div> </div> </div> <br><br><br><br>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<?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>

</body>
</html>