<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Daily Salary Eligibility</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
      rel="stylesheet"
      href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css"
      integrity="sha512-DTOQO9RWCH3ppGqcWaEA1BIZOC6xxalwEsw9c2QQeAIftl+Vegovlnee1c9QX4TctnWMn13TZye+giMm8e2LwA=="
      crossorigin="anonymous"
      referrerpolicy="no-referrer"
    />
    <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>
  <div class="wrapper">
    <div id="loadEligibility">
      <div class="loadingPlaceholder"></div>
    </div>
  <body class="hold-transition sidebar-mini">
      <!-- Include navigation -->
      <%- 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>Daily Salary Eligibility</h1>
              </div>
            </div>
          </div>
        </section>

        <!-- Main content -->
        <section class="content">
          <div class="card">
            <div class="card-body">
              <h5 class="card-title">Salary Eligibility Table</h5>
              <table class="table" id="tableget">
                <thead>
                  <tr>
                    <!-- <th>#</th> -->
                    <th class="text-center">Phone</th>
                    <th class="text-center">Level 1 Eligibility</th>
                    <th class="text-center">All Levels Eligibility</th>
                    <th class="text-center">Total Recharge All levels</th>
                    <th class="text-center">Time</th>
                  </tr>
                </thead>
                <tbody>
                  <!-- Add your table rows dynamically here -->
                </tbody>
              </table>
            </div>
          </div>
        </section>
      </div>
    </div>

    <!-- Include scripts -->
    <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>
      function formateT(params) {
        let result = params < 10 ? "0" + params : params;
        return result;
      }
      function timerJoin(params = "", addHours = 0) {
        let date = "";
        if (params) {
          date = new Date(Number(params));
        } else {
          date = new Date();
        }

        date.setHours(date.getHours() + addHours);

        let years = formateT(date.getFullYear());
        let months = formateT(date.getMonth() + 1);
        let days = formateT(date.getDate());

        let hours = date.getHours() % 12;
        hours = hours === 0 ? 12 : hours;
        let ampm = date.getHours() < 12 ? "AM" : "PM";

        let minutes = formateT(date.getMinutes());
        let seconds = formateT(date.getSeconds());

        return (
          years +
          "-" +
          months +
          "-" +
          days +
          " " +
          hours +
          ":" +
          minutes +
          ":" +
          seconds +
          " " +
          ampm
        );
      }

      $.ajax({
        type: "GET",
        url: "/api/webapi/admin/listCheckSalaryEligibility",
        dataType: "json",
        success: function (response) {
          //   console.log("Response data:", response);

          updateTable(response);
          $("#loadEligibility").fadeOut(0);
        },
        error: function (xhr, status, error) {
          console.log("Error fetching data:", error);
        },
      });

      function updateTable(data) {
        if (Array.isArray(data.rows) && data.rows.length > 0) {
          var tableBody = $("#tableget").find("tbody");
          tableBody.empty();

          data.rows.forEach(function (item) {
            var row =
              "<tr>" +
              '<td class="text-center">' +
              item.phone +
              "</td>" +
              '<td class="text-center">' +
              item.level1ReferralsEligibility +
              "</td>" +
              '<td class="text-center">' +
              item.userAllLevelsEligibility +
              "</td>" +
              '<td class="text-center">' +
              item.totalRechargeCount +
              "</td>" +
              '<td class="text-center">' +
              timerJoin() +
              "</td>" +
              "</tr>";
            tableBody.append(row);
          });
        } else {
          console.log("No data found or invalid response format");
        }
      }
    </script>
  </body>
</html>
