<?php
//Ajax Init
include "ajax-init.php";
// storing  request (ie, get/post) global array to a variable
$requestData= $_REQUEST;
$columns = array(
// datatable column index  => database column name
    0 =>'init_date',
    1 =>'transaction_uid',
    2 =>'hubtel_txn',
    3 =>'phoneNumber',

);
// getting total number records without any search
//$sql = "SELECT employee_name, employee_salary, employee_age ";
//$sql.=" FROM tree_tbl";
$query=mysqli_query($dbc, "SELECT count(*) as total from hub_transaction_tbl WHERE transactionType = 'PUSH'") or die("ajax-list-customers.php: get total number of customers");
$totalData = mysqli_fetch_assoc($query);
$totalFiltered = $totalData['total'];  // when there is no search parameter then total number rows = total number filtered rows.

if( !empty($requestData['search']['value']) ) {
    // if there is a search parameter
    $sql = "SELECT * ";
    $sql.=" FROM hub_transaction_tbl";
    $sql.=" WHERE transactionType = 'PUSH' AND (transaction_uid LIKE '%".$requestData['search']['value']."%' ";    // $requestData['search']['value'] contains search parameter
    $sql.=" OR phoneNumber LIKE '%".$requestData['search']['value']."%' ";
    $sql.=" OR hubtel_txn LIKE '%".$requestData['search']['value']."%' ";
    $sql.=" OR init_date LIKE '%".$requestData['search']['value']."%' ";
    $sql.=" OR status LIKE '%".$requestData['search']['value']."%' )";
    $query=mysqli_query($dbc, $sql) or die("ajax-list-customers.php: get customers");
    $totalFiltered = mysqli_num_rows($query); // when there is a search parameter then we have to modify total number filtered rows as per search result without limit in the query
    $sql.=" ORDER BY ". $columns[$requestData['order'][0]['column']]."   ".$requestData['order'][0]['dir']."   LIMIT ".$requestData['start']." ,".$requestData['length']."   "; // $requestData['order'][0]['column'] contains colmun index, $requestData['order'][0]['dir'] contains order such as asc/desc , $requestData['start'] contains start row number ,$requestData['length'] contains limit length.
    $query=mysqli_query($dbc, $sql) or die("ajax-list-customers.php: get Customers"); // again run query with limit

} else {
    $sql = "SELECT * ";
    $sql.=" FROM hub_transaction_tbl WHERE transactionType = 'PUSH'";
    $sql.=" ORDER BY ". $columns[$requestData['order'][0]['column']]."   ".$requestData['order'][0]['dir']."   LIMIT ".$requestData['start']." ,".$requestData['length']."   ";
    $query=mysqli_query($dbc, $sql) or die("ajax-push-transaction.php: get all push". mysqli_error($dbc));

}

$data = array();

while( $row=mysqli_fetch_array($query) ) {  // preparing an array
    $customerData = get_customer_by_id($dbc,$row['customerID']);
    $nestedData = array();
    $nestedData[] = $row["init_date"];
    $nestedData[] = $row["transaction_uid"];
    $nestedData[] = $row["hubtel_txn"];
    $nestedData[] = $customerData["fullName"]." (".$customerData['companyName'].") ".$customerData['phoneNum'];
    $nestedData[] = $row["phoneNumber"];
    $nestedData[] = $row["transactionType"];
    $nestedData[] = $row["amount"];
    $nestedData[] = $row["fee"];
    $nestedData[] = $row["status"];
    $nestedData[] = $row["balance"];
    $nestedData[] = $row["deviceType"];
    $data[] = $nestedData;
}

$json_data = array(
    "draw"            => intval( $requestData['draw'] ),   // for every request/draw by clientside , they send a number as a parameter, when they recieve a response/data they first check the draw number, so we are sending same number in draw.
    "recordsTotal"    => intval( $totalData ),  // total number of records
    "recordsFiltered" => intval( $totalFiltered ), // total number of records after searching, if there is no searching then totalFiltered = totalData
    "data"            => $data   // total data array
);
echo json_encode($json_data);  // send data as json format