<?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 =>'id'
);
// 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 pricing_policy_tbl") or die("ajax-list-service-pricing.php: get total number of service pricing");
$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 pricing_policy_tbl";
    $sql.=" WHERE id LIKE '%".$requestData['search']['value']."%' ";    // $requestData['search']['value'] contains search parameter
    $sql.=" OR serviceType LIKE '%".$requestData['search']['value']."%' ";
    $sql.=" OR senderCountry LIKE '%".$requestData['search']['value']."%' ";
    $query=mysqli_query($dbc, $sql) or die("ajax-list-pricing-policy.php: get Pricing Policy");
    $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-pricing-policy.php: get Pricing Policy"); // again run query with limit

} else {
    $sql = "SELECT * ";
    $sql.=" FROM pricing_policy_tbl";
    $sql.=" ORDER BY ". $columns[$requestData['order'][0]['column']]."   ".$requestData['order'][0]['dir']."   LIMIT ".$requestData['start']." ,".$requestData['length']."   ";
    $query=mysqli_query($dbc, $sql) or die("jax-list-pricing-policy.php: get Pricing Policy");

}

$data = array();

while( $row=mysqli_fetch_array($query) ) {  // preparing an array
    $nestedData=array();

    $user = get_user_by_id($dbc,$row['pricing_by']);
    $senderGroup = get_uGroup_by_id($dbc,$row['senderUGroup']);
    $recipientGroup = get_uGroup_by_id($dbc,$row['recipientUGroup']);
    $senderAccType = get_accountType_by_id($dbc,$row['senderAccountType']);
    $recipientAccType = get_accountType_by_id($dbc,$row['recipientAccountType']);
    $service = get_service_pricing_by_id($dbc,$row['serviceType']);

    $nestedData[] = $row["id"];
    $nestedData[] = $service["serviceName"];
    $nestedData[] = $senderGroup["groupName"];
    $nestedData[] = $recipientGroup["groupName"];
    $nestedData[] = $senderAccType["accountType"];
    $nestedData[] = $recipientAccType["accountType"];
    $nestedData[] = $row["priceType"];
    $nestedData[] = $row["price_in_percent_absolute"];
    $nestedData[] = $row["senderCountry"];
    $nestedData[] = $row["recipientCountry"];
    $nestedData[] = $row["sysCommission"];
    $nestedData[] = $row["senderCommission"];
    $nestedData[] = $row["recipientCommission"];
    $nestedData[] = "<a href='main-pricing.php?edit_pricing={$row["id"]}'><i class='fa fa-edit'></i></a>";
    $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