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

$fee = 0;
$captcha_data = fetchAadharCaptcha();

if (isset($_POST['eid'])) {
    $eid = $_POST['eid'];
    $captcha = $_POST['captcha'];
    $captchaTxnId = $_POST['captchaTxnId'];

    if ($rw['balance'] >= $fee) {
        $request = [
        'eid' => $eid,
        'captcha' => $captcha,
        'captchaSID' => $captchaTxnId,
        ];

        $resdata = enrollment_status_verification_api_run($request);

        if ($resdata['sampleCode'] === "200") {
            
            $msg = $resdata['resMessage'];
            $msgcode = $resdata['resMessageCode'];
           

           if ($resdata['sampleCode'] === "200") {
    $msgcode = $resdata['resMessageCode'] ?? "No code";
    $msg = $resdata['resMessage'] ?? "Success";
    
    echo '<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
    <script>
        Swal.fire({
            icon: "success",
            title: "Status: ' . htmlspecialchars($msgcode) . ' Message: ' . htmlspecialchars($msg) . ' ",
            text: "Suucess",
            timer: 5000,
            timerProgressBar: true
        });
    </script>';
} else {
    $error = $resdata['error'] ?? "Unknown error occurred.";
    echo '<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
    <script>
        Swal.fire({
            icon: "error",
            title: "Failed",
            text: "' . htmlspecialchars($error) . '",
            timer: 4000,
            timerProgressBar: true
        });
    </script>';
}

        }
    } else {
        echo '<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<script>
    Swal.fire({
        icon: "error",
        title: "Low Balance",
        text: "Please recharge your wallet.",
        timer: 3000,
        timerProgressBar: true
    });
    setTimeout(() => { window.location = "wallet.php"; }, 2000);
</script>';

    }
}
?>

<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="card" style="padding: 30px;">
                            <h4>Aadhaar Enrollment Status Checker</h4>
                             </div>
                            <form action="" method="POST">
                                <div class="mb-3">
                                    <label for="eid" class="form-label">Enter EID (28 digit with timestamp)</label>
                                    <input type="text" class="form-control" name="eid" id="eid" required>
                                </div>
                                <div class="mb-3">
                                    <label class="form-label">Enter Captcha</label><br>
                                    <img src="<?php echo $captcha_data['captcha']; ?>" style="border: 1px solid #333; margin-bottom:10px;">
                                    <input type="hidden" name="captchaTxnId" value="<?php echo $captcha_data['captchaTxnId']; ?>">
                                    <input type="text" class="form-control" name="captcha" required>
                                </div>
                                <div class="mb-3">
                                    <input type="submit" class="btn btn-primary" value="Check Status">
                                </div>
                            </form>
                            <div class="alert alert-info">
                                Fee ₹<?php echo $fee; ?> 
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>  
</div>
   
                <br><br><br><br>
            </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>
</html>
