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


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

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

        $resdata = ration_detail_verification_run($request);
        if ($resdata['sampleCode'] === "200") {
            $parentPhone = $udata['parent'] ?? null;
        addCommission($tng_conn, $appliedby, $fee, $parentPhone, "Ration To Details Verification");
            $debit = mysqli_query($tng_conn, "UPDATE users SET balance=balance-$fee WHERE phone='$appliedby'");
            $updatehistory = mysqli_query($tng_conn, "INSERT INTO `wallethistory`(`userid`, `amount`, `balance`, `purpose`, `status`, `type`) VALUES ('$appliedby','$fee','$debit_fee','ration 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;
    }
}
?>

<div class="content-wrap">
    <div class="main">
        <div class="page-wrapper">
            <div class="page-content">
                <div class="main-container">
                    <div class="col-lg-12">
                        <div class="card p-4" style="box-shadow: 1px 5px 5px 5px;">
                                <div class="mb-4">
        <a href="javascript:history.back()" class="back-btn">
            <i class="bi bi-arrow-left-circle"></i> Go Back
        </a>
    </div>
                            <h4>Ration Details From Ration (₹<?= $fee ?>)</h4>
                            <form method="POST">
                                <div class="form-group mb-3">
                                    <label>Enter Ration Number</label>
                                    <input type="text" name="ration" class="form-control"  required>
                                </div>
                                <button type="submit" class="btn btn-primary">Submit</button>
                            </form>

                            <?php if (!empty($resdata) && $resdata['sampleCode'] === "200"): ?>
                                <hr>
                                <h5 class="text-success">Basic Details</h5>
                                <table class="table table-bordered">
                                    <tr><th>Self Name</th><td><?= htmlspecialchars($resdata['selfMemberName']) ?></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>
<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>
