<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Withdrawal list</title>
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,400i,700&display=fallback">
  <link rel="stylesheet" href="/plugins/fontawesome-free/css/all.min.css">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
  <link href="//cdn.jsdelivr.net/npm/@sweetalert2/theme-dark@4/dark.css" rel="stylesheet">
  <link rel="stylesheet" href="/dist/css/adminlte.min.css">
  <link rel="stylesheet" href="/css/admin.css">
</head>

<body class="hold-transition sidebar-mini">
  <div class="wrapper">
    <%- include('nav') %>
    <div class="content-wrapper">
      <section class="content-header">
        <div class="container-fluid">
          <div class="row mb-2">
            <div class="col-sm-6">
              <h1>Withdrawal list</h1>
            </div>
          </div>
        </div>
        <!-- /.container-fluid -->
      </section>

      <div class="form-group" style="text-align: center;">
        <input type="text" id="search" placeholder="Enter account to search">
      </div>

      <!-- Main content -->
      <section class="content">
        <!-- Default box -->
        <div class="card">
          <div class="card-header">
            <h3 class="card-title">Withdrawal list</h3>
            <div class="card-tools">
              <button type="button" class="btn btn-tool" data-card-widget="collapse" title="Collapse">
                <i class="fas fa-minus"></i>
              </button>
              <button type="button" class="btn btn-tool" data-card-widget="remove" title="Remove">
                <i class="fas fa-times"></i>
              </button>
            </div>
          </div>
          <div class="card-body p-0" style="overflow-y: hidden;">
            <table class="table table-striped projects" id="table1">
              <thead>
                <tr>
                  <th class="text-center">S no.</th>
                  <th class="text-center">Mobile</th>
                  <th class="text-center">UPI ID</th>
                  <th class="text-center">Amount</th>
                  <th class="text-center">Account</th>
                  <th class="text-center">IFSC</th>
                  <th class="text-center">Transaction ID</th>
                  <th class="text-center">Time</th>
                  <th class="text-center">Status</th>
                  <!-- <th class="text-center"></th> -->
                </tr>
              </thead>
              <tbody>
               
              </tbody>
            </table>
          </div>
        </div>
        <nav aria-label="Page navigation example" style="margin-top: 20px; display: flex; justify-content: center">
         <ul class="pagination table1">
             <li class="page-item previous" id="previous">
                 <a class="page-link" href="#" tabindex="-1">Previous</a>
             </li>
             <div id="numbers" style="display: flex">
                 <li class="page-item">
                     <a class="page-link active text-white" id="text-page"></a>
                 </li>
             </div>
             <li class="page-item next" id="next">
                 <a class="page-link" href="#">Next</a>
             </li>
         </ul>
     </nav>
        </div>

      </section>
    </div>
  <script src="/plugins/jquery/jquery.min.js"></script>
  <script src="/plugins/bootstrap/js/bootstrap.bundle.min.js"></script>
  <script src="/dist/js/adminlte.min.js"></script>
  <script src="/js/admin/admin.js"></script>
  <script src="//cdn.jsdelivr.net/npm/sweetalert2@11/dist/sweetalert2.min.js"></script>
  <script src="/js/admin/tables.js"></script>
  <script>
    function formateT(params) {
        let result = (params < 10) ? "0" + params : params;
        return result;
    }
    
    function timerJoin(params = '') {
        let date = '';
        if (params) {
            date = new Date(Number(params));
        } else {
            date = new Date();
        }
        let years = formateT(date.getFullYear());
        let months = formateT(date.getMonth() + 1);
        let days = formateT(date.getDate());
        return years + '-' + months + '-' + days;
    }
    function show(params) {
      if (params.length == 0) {
        $('tbody').html(`
            <tr class="text-center">
              <td colspan="7">No data found.......</td>
            </tr>
          `);
        return;
      }
      let html = '';
      params.map((data) => {
        html += `<tr class="text-center">
                  <td id="${data.id}">
                    ${data.id}
                  </td>
                  <td>
                    <b>${data.phone}</b>
                  </td>
                  <td>
                    <b style="color: #3498db">${data.name_bank}</b>
                  </td>
                  <td>
                    <b>${formatMoney(data.money)}</b>
                  </td>
                  <td style="min-width: 190px;">
                    <b>${data.account}</b>
                  </td> 
                  <td style="min-width: 190px;">
                    <b>${data.ifsc}</b>
                  </td>
                  <td style="min-width: 190px;">
                    <b>${data.id_order}</b>
                  </td>
                  <td style="min-width: 190px;">
                    <b>${timerJoin(data.time)}</b>
                  </td>
                  <td class="project-state">
                    <span class="badge badge-${(data.status == 1) ? 'success' : 'danger'}">${(data.status == 1) ? 'Success' : 'Closed'}</span>
                  </td>
                  <!-- <td class="project-actions text-center" style="min-width: 160px;">
                    <a class="btn btn-success btn-sm confirm-btn" href="" data="${data.id}"><i class="fa fa-check"></i></a>
                    <!-- <a class="btn btn-info btn-sm" href="#"><i class="fas fa-pencil-alt"></i></a> -->
                    <a class="btn btn-danger btn-sm delete-btn" href="#" data="${data.id}"><i class="fas fa-trash"></i></a>
                   </td> -->
                 </tr>`;
               })
          $('tbody').html(html);
          $('.btn-success').click(function (e) { 
            e.preventDefault();
            let id = $(this).attr('data');
            $.ajax({
              type: "POST",
              url: "/api/webapi/admin/rechargeDuyet",
              data: {
                id: id,
                type: 'confirm'
              },
              dataType: "json",
              success: function (response) {
                Swal.fire(
                  'Good job!',
                  'You clicked the button!',
                  'success'
                )
                setTimeout(() => {
                  location.reload();
                }, 100);
              }
            });
          });
          $('.btn-danger').click(function (e) { 
            e.preventDefault();
            let id = $(this).attr('data');
            $.ajax({
              type: "POST",
              url: "/api/webapi/admin/rechargeDuyet",
              data: {
                id: id,
                type: 'delete'
              },
              dataType: "json",
              success: function (response) {
                setTimeout(() => {
                  location.reload();
                }, 100);
                Swal.fire(
                  'Good job!',
                  'You clicked the button!',
                  'success'
                )
              }
            });
          });
    }
    $.ajax({
      type: "POST",
      url: "/api/webapi/admin/recharge",
      data: {
        
      },
      dataType: "json",
      success: function (response) {
        show(response.datas4)
      }
    });
  </script>
  <script>
    const itemsPerPage = 50; 
let currentPage = 1;
let dataList = [];

function updateTableData() {
    const totalPages = Math.ceil(dataList.length / itemsPerPage);
    const startIndex = (currentPage - 1) * itemsPerPage;
    const endIndex = startIndex + itemsPerPage;
    const displayedData = dataList.slice(Math.max(0, dataList.length - endIndex), dataList.length - startIndex).reverse();

    show(displayedData);
    updatePaginationButtons();
}

function renderPaginationNumbers() {
    const totalPages = Math.ceil(dataList.length / itemsPerPage);

    $("#numbers").empty(); // Clear existing pagination numbers

    for (let i = totalPages; i >= 1; i--) {
        const liClass = (i === currentPage) ? "page-item active" : "page-item";
        $("#numbers").append(`<li class="${liClass}"><a class="page-link" href="#" onclick="changePage(${i})">${i}</a></li>`);
    }
}

function updatePaginationButtons() {
    const totalPages = Math.ceil(dataList.length / itemsPerPage);

    $("#text-page").text(`${currentPage} / ${totalPages}`);

    $("#previous").prop("disabled", currentPage === 1);
    $("#next").prop("disabled", currentPage === totalPages);
}



function changePage(page) {
    currentPage = page;
    updateTableData();
    renderPaginationNumbers();
}

$(document).ready(function () {
    $.ajax({
        type: "POST",
        url: "/api/webapi/admin/recharge",
        data: {},
        dataType: "json",
        success: function (response) {
            dataList = response.datas4;
            updateTableData();
            renderPaginationNumbers();
        }
    });
});
  </script>
  
</body>

</html>