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

<script src="https://cdn.jsdelivr.net/npm/sweetalert2@10"></script>
<?php
    
if ($_POST['aadhar']) {
    $aadhar = $_POST['aadhar'];
    $language = $_POST['language_Code'] ?? 'en';
    $appliedby = $udata['phone'];
    $debit_fee =  $udata['balance'] - $fee;

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

        $resdata = agri_farmer_card_pdf_verification_api_run($request);

        $mobileNumber = $resdata['mobileNumber'] ?? '';
        $name = $resdata['fullname'] ?? '';
        $pdf = $resdata['pdf'] ?? '';
        $statusCode = $resdata['sampleCode'] ?? '';
        $errore = $resdata['error'] ?? '';

        if ($errore) {
            echo "<script>$(function(){ Swal.fire('$errore', 'Someting Wrong.', 'error') }); setTimeout(() => { window.location=''; }, 5000);</script>";
        } elseif ($resdata['sampleCode'] === "200") {
              date_default_timezone_set('Asia/Kolkata');
                $timestamp = date("Y-m-d H:i:s");
                $parentPhone = $udata['parent'] ?? null;
        addCommission($tng_conn, $appliedby, $fee, $parentPhone, "Agri Farmer Card  PDF Verification");
             $debit = mysqli_query($tng_conn, "UPDATE users SET balance=balance-$fee WHERE phone='$appliedby'");
           $database = mysqli_query($tng_conn, "INSERT INTO `agri_farmer_pdf`(`name`, `username`, `aadhar`, `pdf`, `date`) VALUES ('{$resdata['fullname']}','{$udata['phone']}','{$resdata['aadhar']}','{$resdata['pdf']}','$timestamp')");
            $updatehistory = mysqli_query($tng_conn, "INSERT INTO `wallethistory`(`userid`, `amount`, `balance`, `purpose`, `status`, `type`) VALUES ('$appliedby','$fee','$debit_fee','Agri Farmer Card  PDF','1','Debit')");
     
            
        } 
    } else {
        echo "<script>$(function(){ Swal.fire('Wallet Balance is Low!', 'Please Recharge Now!', 'error') }); setTimeout(() => { window.location='wallet.php'; }, 1200);</script>";
    }
}
?>
<div class="content-wrap">
    <div class="main">
        <div class="col-md-12">
            <div class="main-content">
<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="alert alert-danger" role="alert">
                                                        <a href="" class="alert-link">Agri Farmer (All State) PDF........</a>
                                                    </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" maxlength="12" minlength="12" placeholder="Enter 12 Digit Aadhar Number" class="form-control" required>
                                                                </div>
                                                                <hr>
                                                               <div class="col-md-12 mb-3">
                                                                 <label for="language_Code" class="form-label">Select Language:</label>
                                                                 <select name="language_Code" id="language_Code" class="form-control" required>
                                                                   <option value="">Select Local Language</option>
                                                                 </select>
                                                               </div>
                                                                <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 ($resdata['sampleCode'] === "200") { ?>
                                            <div class="col-lg-8 col-md-6 col-sm-6">
                                                <div class="card" style="background-color: #FCF3CF;" id="printSection">
                                                    <div class="card-body">
                                                          <h5> Farmer Pdf
  <a href="AgriFarmerCardPdf_list.php" style="font-size:14px; margin-left:10px; text-decoration:none; color:blue;">
    (View List)
  </a>
</h5>
                                                        <h5 class="mb-3">Farmer Details</h5>
                                                        <table class="table table-bordered table-striped">
                                                            <tbody>
                                                                <tr>
                                                                    <th>UID Number</th>
                                                                    <td><?php echo htmlspecialchars($aadhar); ?></td>
                                                                </tr>
                                                                <tr>
                                                                    <th> Name</th>
                                                                    <td><?php echo htmlspecialchars($name); ?></td>
                                                                <tr>
                                                                   <th>VIEW PDF</th>
                                                                   <td>
                                                                       <a href="<?php echo htmlspecialchars($resdata['pdf']); ?>" 
                                                                          download="<?php echo htmlspecialchars($aadhar); ?>.pdf" 
                                                                          class="btn btn-sm btn-success">
                                                                           Download PDF
                                                                       </a>
                                                                   </td>
                                                               </tr>
                                                            </tbody>
                                                        </table>
                                                    </div>
                                                </div>
                                            </div>
                                        <?php } ?>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <br><br><br><br>
            </div>
        </div>
    </div>
</div>

<?php include('../template/tngweb/footer.php'); ?>
<script>
  document.addEventListener("DOMContentLoaded", function () {
    const languages = [
      { code: "hi", name: "Hindi" },
      { code: "bn", name: "Bengali" },
      { code: "ta", name: "Tamil" },
      { code: "te", name: "Telugu" },
      { code: "mr", name: "Marathi" },
      { code: "gu", name: "Gujarati" },
      { code: "kn", name: "Kannada" },
      { code: "ml", name: "Malayalam" },
      { code: "pa", name: "Punjabi" },
      { code: "ur", name: "Urdu" },
      { code: "as", name: "Assamese" },
      { code: "or", name: "Odia" }
    ];

    const stateSelect = document.getElementById("language_Code");

    languages.forEach(function(language) {
      const option = document.createElement("option");
      option.value = language.code;
      option.textContent = language.name;
      stateSelect.appendChild(option);
    });
  });
</script>

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