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

// fetch fee from databasee 
$price = mysqli_fetch_assoc(mysqli_query($ahk_conn, "SELECT * FROM pricing WHERE service_name='aadhar_to_ration_find_fee' "));
$fee = $price['price'];


if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['aadhar'])) {
    $aadhar = $_POST['aadhar'];
    $appliedby = $udata['phone'];
    $debit_fee =  $udata['balance'] - $fee;

    if ($udata['balance'] >= $fee) {
      
        $url = "$tng_apiurl/uid_to_ration_details_verification.php?uid=$aadhar&apikey=$tng_apikey";

        $curl = curl_init();
        curl_setopt_array($curl, [
            CURLOPT_URL => $url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_TIMEOUT => 60,
            CURLOPT_CUSTOMREQUEST => "GET",
        ]);

        $response = curl_exec($curl);
        curl_close($curl);

        $resdata = json_decode($response, true);

        if ($resdata['sampleCode'] === "200") {
            $parentPhone = $udata['parent'] ?? null;
        addCommission($ahk_conn, $appliedby, $fee, $parentPhone, "Aadhar To Ration Details Verification");
            $debit = mysqli_query($ahk_conn, "UPDATE users SET balance=balance-$fee WHERE phone='$appliedby'");
            $updatehistory = mysqli_query($ahk_conn, "INSERT INTO `wallethistory`(`userid`, `amount`, `balance`, `purpose`, `status`, `type`) VALUES ('$appliedby','$fee','$debit_fee','Aadhar To Ration Details Find ','1','Debit')");
        } else {
            $error = $resdata['error'] ?? "Unknown error";
            echo <<<HTML
            <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: "Failed",
                        text: "$error",
                        timer: 3000,
                        timerProgressBar: true
                    });
                    setTimeout(() => { window.location = ""; }, 3000);
                });
            </script>
HTML;
        }
    } else {
        echo <<<HTML
        <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: "Low Wallet Balance!",
                    text: "Please Recharge to Continue.",
                    timer: 2000,
                    timerProgressBar: true
                });
                setTimeout(() => { window.location = "wallet.php"; }, 1500);
            });
        </script>
HTML;
    }
}
?>

<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-info" role="alert">
                                                    Aadhar to   Ration  Verification
                                                    </div>
                                                    <form action="" method="POST" class="row g-3">
                                                        <div class="card-body">
                                                            <div class="col-md-12">
                                                                <div class="form-group">
                                                                    <label for="aadhar">Enter Aadhar Number</label>
                                                                    <input name="aadhar" type="text" id="aadhar" placeholder="Enter Aadhar Number" class="form-control" 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-primary" 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-8 col-md-6 col-sm-6">
                                                <div class="card" style="background-color: #E8F6F3;">
                                                    <div class="card-body">
                                                        <h5 class="mb-3">Aadhar To Ration  Details</h5>
                                                        <table class="table table-bordered table-striped">
                                    <tr><th>Aadhaar</th><td><?= htmlspecialchars($resdata['aadhar']) ?></td></tr>
                                    <tr><th>RC ID</th><td><?= htmlspecialchars($resdata['rcId']) ?></td></tr>
                                    <tr><th>District</th><td><?= htmlspecialchars($resdata['homeDistName']) ?></td></tr>
                                    <tr><th>State</th><td><?= htmlspecialchars($resdata['homeStateName']) ?></td></tr>
                                    <tr><th>Address</th><td><?= htmlspecialchars($resdata['address']) ?></td></tr>
                                </table>

                                <h5 class="text-info">Family Members</h5>
                                <table class="table table-striped table-bordered">
                                    <thead>
                                        <tr>
                                            <th>#</th>
                                            <th>Name</th>
                                            <th>Member ID</th>
                                            <th>Relation</th>
                                            <th>Aadhaar Linked</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php foreach ($resdata['memberDetailsList'] as $member): ?>
                                            <tr>
                                                <td><?= $member['sl_no'] ?></td>
                                                <td><?= htmlspecialchars($member['memberName']) ?></td>
                                                <td><?= htmlspecialchars($member['memberId']) ?></td>
                                                <td><?= htmlspecialchars($member['releationship_name']) ?></td>
                                                <td><?= $member['aadhaar'] ?></td>
                                            </tr>
                                        <?php endforeach; ?>
                                    </tbody>
                                </table>
                            <?php endif; ?>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>  
</div>

<?php include('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>
