<?php
// Assuming you have the database connection established already
session_start();
require("../mainconfig.php");
if (isset($_SESSION['user'])) {
  $apa = $_SESSION['user']['username'];
  // Function to calculate the total number of pages based on the number of records per page
  function getTotalPages($totalRecords, $recordsPerPage) {
    return ceil($totalRecords / $recordsPerPage);
  }

  // Function to search for topup data based on the search query
  function getTopupData($searchQuery, $offset, $recordsPerPage, &$totalRecords, $sess_username) {
    global $db;

    // Add SQL injection protection
    $searchQuery = mysqli_real_escape_string($db, $searchQuery);

    $query = "SELECT * FROM balance_history WHERE id LIKE '%$searchQuery%' AND username = '$sess_username' ORDER BY id DESC LIMIT $recordsPerPage OFFSET $offset";
    $result = mysqli_query($db, $query);

    $data = array();
    while ($row = mysqli_fetch_assoc($result)) {
      $data[] = array(
        "no" => $row['id'],
        "action" => $row['action'],
        "quantity" => $row['quantity'],
        "msg" => $row['msg'],
        "date" => $row['tanggal']
      );
    }

    // Get the total number of records that match the search query
    $totalRecordsQuery = "SELECT COUNT(*) AS total FROM balance_history WHERE username = '$sess_username' AND id LIKE '%$searchQuery%'";
    $totalRecordsResult = mysqli_query($db, $totalRecordsQuery);
    $totalRecordsRow = mysqli_fetch_assoc($totalRecordsResult);
    $totalRecords = $totalRecordsRow['total'];

    return $data;
  }

  // Function to get the appropriate label based on the action
  function getActionLabel($action) {
    return $action === "Cut Balance" ? "<span class='label label-danger'>$action</span>" : "<span class='label label-success'>$action</span>";
  }

  // Function to get the appropriate quantity label based on the action
  function getActionQuantityLabel($action) {
    return $action === "Cut Balance" ? "<span class='label label-danger'>- " : "<span class='label label-success'>+ ";
  }

  $recordsPerPage = isset($_GET['recordsPerPage']) ? intval($_GET['recordsPerPage']) : 10;
  $page = isset($_GET['page']) ? intval($_GET['page']) : 1;
  $searchQuery = isset($_GET['search']) ? $_GET['search'] : '';

  // Replace the following line with your method to get the session username
  $sess_username = $apa;

  // Calculate the OFFSET for the current page
  $offset = ($page - 1) * $recordsPerPage;

  if (!empty($searchQuery)) {
    // Perform search and get data for the current page
    $data = getTopupData($searchQuery, $offset, $recordsPerPage, $totalRecords, $sess_username);
  } else {
    // Fetch data from the 'topup' table for the current page
    $query = "SELECT * FROM balance_history WHERE username = '$sess_username' ORDER BY id DESC LIMIT $recordsPerPage OFFSET $offset";
    $result = mysqli_query($db, $query);

    $data = array();
    while ($row = mysqli_fetch_assoc($result)) {
      $data[] = array(
        "no" => $row['id'],
        "action" => $row['action'],
        "quantity" => $row['quantity'],
        "msg" => $row['msg'],
        "date" => $row['tanggal']
      );
    }

    // Get the total number of records in the 'topup' table
    $totalRecordsQuery = "SELECT COUNT(*) AS total FROM balance_history WHERE username = '$sess_username'";
    $totalRecordsResult = mysqli_query($db, $totalRecordsQuery);
    $totalRecordsRow = mysqli_fetch_assoc($totalRecordsResult);
    $totalRecords = $totalRecordsRow['total'];
  }

  // Calculate total pages
  $totalPages = getTotalPages($totalRecords, $recordsPerPage);

  // Return data as JSON
  echo json_encode(array(
    "topup" => $data,
    "totalPages" => $totalPages
  ));
}
?>
