<?php
include('includes/header.php');
include('includes/navbar.php');
?>
<?php include 'actions/wpin_action.php'; ?>

<div class="container-fluid">

    <div class="card shadow mb-4">
        <div class="card-header py-3">

            <div class="btn btn-primary">
    Withdrawal Pin Request
    </div>
        </div>
        <div class="card-body">
            <div class="table-responsive">
              <?php
                  $query = "SELECT * FROM transaction WHERE transaction_type = 'Withdrawal Pin' ORDER BY transaction_id DESC";
                  $query_run = mysqli_query($connection, $query);
              ?>
                  <table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
                      <thead>
                          <tr>
                              <th> S/N</th>
                              <th> Name</th>
                              <th> Email </th>
                              <th> Amount </th>
                              <th> Method </th>
                              <th> Wallet Address </th>
                              <th> Proof </th>
                              <th> Status </th>
                              <th> Date </th>
                              <th> Action </th>
                          </tr>
                      </thead>
                      <tbody>
                          <?php
                          $i = 1;
                          if(mysqli_num_rows($query_run) > 0)
                          {
                              while($row = mysqli_fetch_assoc($query_run))
                              {
                          ?>
                              <tr>
                                  <td><?php  echo $i++; ?></td>
                                  <td>
                                    <?php
                                      $trnx_user =  $row['transaction_user_id'];
                                      $sql_user = "SELECT * FROM users WHERE user_id = '$trnx_user'";
                                      $result_user = $connection->query($sql_user);
                                      while ($row1 = $result_user->fetch_assoc()) {
                                          $dbusername = $row1['username'];
                                          $dbfull_name = $row1['full_name'];
                                          $useremail = $row1['email'];
                                          $currency = $row1['currency'];
                                      }

                                      if ($currency == "USD") {
                                          $currency_code = "$";
                                      }elseif ($currency == "GBP") {
                                          $currency_code = "£";
                                      }elseif ($currency == "EUR") {
                                          $currency_code = "€";
                                      }elseif ($currency == "YEN") {
                                          $currency_code = "¥";
                                      }elseif ($currency == "INR") {
                                          $currency_code = "₹";
                                      }

                                      echo "$dbfull_name";
                                    ?>
                                  </td>
                                  <td><?php  echo $useremail ?></td>
                                  <td>
                                    <?php
                                      $trxn_amount = $row['transaction_amount'];
                                      $d_amount = number_format("$trxn_amount",);
                                      echo $currency_code.''.$d_amount;
                                    ?>
                                  </td>
                                  <td style="text-transform:"><?php  echo $row['t_mode']; ?></td>
                                 <td><?php  echo $row['wallet_address']; ?></td>
                                 <td>
                                   <a href="../images/<?php echo $row['proof_upload'] ?>">
                                     <img src="../images/<?php echo $row['proof_upload'] ?>" alt="" style="max-width:40%">
                                   </a>
                                </td>
                                  <td>
                                    <?php
                                      $status = $row['transaction_status'];
                                      if ($status == "success"):
                                     ?>
                                     <span style="color: green; font-weight: bold;">Success</span>
                                   <?php elseif ($status == "pending"): ?>
                                     <span style="color: orange; font-weight: bold;">Pending</span>
                                   <?php elseif ($status == "cancel"): ?>
                                     <span style="color: red; font-weight: bold;">Declined</span>
                                   <?php endif; ?>
                                   </td>
                                   <td><?php  echo $row['transaction_date']; ?></td>

                                   <td>
                                     <?php if ($status == "success" || $status == "cancel"): ?>
                                       <span>No Action</span>
                                       <?php else: ?>
                                         <form action="" method="post" onsubmit="return(confirm('Confirm you want to approve this deposit transaction'))">
                                           <input type="hidden" name="tranx_id" value="<?php  echo $row['trans_ref']; ?>">
                                             <button type="submit" name="approve" class="btn btn-success"> <i class="fa-solid fa-check"></i></button>
                                         </form>
                                         <br>
                                         <form action="" method="post" onsubmit="return(confirm('Confirm you want to decline this deposit transaction'))">
                                           <input type="hidden" name="tranx_id" value="<?php  echo $row['trans_ref']; ?>">
                                             <button type="submit" name="cancel" class="btn btn-warning"> <i class="fa-solid fa-xmark"></i></button>
                                         </form>
                                     <?php endif; ?>
                                   </td>

                              </tr>
                          <?php
                              }
                          }
                          else {
                              echo "No Record Found";
                          }
                          ?>
                      </tbody>
                  </table>

            </div>
        </div>
    </div>



</div>
<!-- /.container-fluid -->
<p id="error" style='display: none'><?php echo $error ?></p>
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
<script>
    var error = document.getElementById('error');

    if (error.textContent == "cancel_success") {
        swal("SUCCESS!", "Payment was cancel successfully", "success");
        setTimeout(() => {
            window.location.href = ''
        }, 1500);

    }else if (error.textContent == "approve_success") {
      swal("SUCCESS!", "Payment approved successfully", "success");
      setTimeout(() => {
          window.location.href = ''
      }, 1500);

    }else if (error.textContent == "error") {
        swal("ERROR!", "Sorry an error occurred. Please try again later", "error");
        setTimeout(() => {
            window.location.href = ''
        }, 3000);
    }

</script>
<?php
include('includes/scripts.php');
include('includes/footer.php');
?>
