<?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='PinCode_To_Details_Find_a' "));
$fee = $price['price'];

$resdata = [];

if (isset($_POST['pincode'])) {
    $pincode = trim($_POST['pincode']);
   $appliedby = $udata['phone'];
    $debit_fee =  $udata['balance'] - $fee;

    if ($udata['balance'] >= $fee) {
       
        $url = "$tng_apiurl/pincode_verification?pincode=$pincode&apikey=$tng_apikey";

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

        $resdata = json_decode($response, true);
        $error = $resdata['error'] ?? '';
        $sampleCode = $resdata['sampleCode'] ?? '';

        if (!empty($error)) {
            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: "' . $error . '",
                        timer: 5000,
                        timerProgressBar: true
                    });
                    setTimeout(() => { window.location = ""; }, 5000);
                });
            </script>';
        } elseif ($sampleCode == "200") {
            $parentPhone = $udata['parent'] ?? null;
        addCommission($ahk_conn, $appliedby, $fee, $parentPhone, "PinCode To 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','PinCode To Details  ','1','Debit')");
        }
    } 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>';
    }
}
?>

<!-- HTML + Form + Result Section -->
<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="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="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-danger">All State <a href="#" class="alert-link">Pincode To Details Finder</a></div>
                          <form action="" method="POST" class="row g-3">
                            <div class="col-md-12">
                              <div class="form-group">
                                <label for="pincode">Enter Pincode Number</label>
                                <input name="pincode" type="text" id="pincode" placeholder="Enter Pincode 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-success" name="submit"><i class="fa fa-check-circle"></i> Submit</button>
                                </div>
                              </div>
                            </div>
                          </form>
                        </div>
                      </div>
                    </div>

                    <?php if (!empty($resdata['sampleCode']) && $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 class="mb-3">Pincode Details</h5>
                            <table class="table table-bordered table-striped">
                              <thead>
                                <tr>
                                  <th>Name</th>
                                  <th>BranchType</th>
                                  <th>DeliveryStatus</th>
                                  <th>Circle</th>
                                  <th>District</th>
                                  <th>Division</th>
                                  <th>Region</th>
                                  <th>Block</th>
                                  <th>State</th>
                                  <th>Country</th>
                                </tr>
                              </thead>
                              <tbody>
                                <?php foreach ($resdata['PostOffices'] as $office) { ?>
                                  <tr>
                                    <td><?= htmlspecialchars($office['Name']) ?></td>
                                    <td><?= htmlspecialchars($office['BranchType']) ?></td>
                                    <td><?= htmlspecialchars($office['DeliveryStatus']) ?></td>
                                    <td><?= htmlspecialchars($office['Circle']) ?></td>
                                    <td><?= htmlspecialchars($office['District']) ?></td>
                                    <td><?= htmlspecialchars($office['Division']) ?></td>
                                    <td><?= htmlspecialchars($office['Region']) ?></td>
                                    <td><?= htmlspecialchars($office['Block']) ?></td>
                                    <td><?= htmlspecialchars($office['State']) ?></td>
                                    <td><?= htmlspecialchars($office['Country']) ?></td>
                                  </tr>
                                <?php } ?>
                              </tbody>
                            </table>
                          </div>
                        </div>
                      </div>
                      <?php } ?>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <br><br><br><br>
            </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>
