<?php

global $db;
ini_set("display_errors", "0");
error_reporting(E_ALL);
$requestData= $_REQUEST;
$columns = array( 
	0 => 'branch_id',
	1 => 'branch_name',
	2 => 'branch_address',
	3 => 'branch_city', 
	4 => 'branch_country'
	
);
$data = $db->query("SELECT COUNT(company_id) as jumlah FROM branches WHERE branch_name <> 'HQ' and company_id = '" . getCompany($_SESSION['user_id']) . "'");
$count = $data[0]["jumlah"];
$totalData = $count;
$totalFiltered = $totalData;

$sql = "SELECT * FROM branches WHERE branch_name <> 'HQ' and company_id = '" . getCompany($_SESSION['user_id']) . "'";


$data = $db->query($sql);
$totalFiltered = count($data);
$sql.=" ORDER BY ". $columns[$requestData['order'][0]['column']]."   ".$requestData['order'][0]['dir']."  LIMIT ".$requestData['start']." ,".$requestData['length']."   ";
$data = $db->query($sql);
$qweqwe = array();
foreach ($data as $key => $value) {
    $nestedData = array(); 
	
	$nestedData[] = $value["branch_id"];
	$nestedData[] = $value["branch_name"];
	$nestedData[] = $value["branch_address"]. ", " . $value["branch_zip"]. " ". $value["branch_city"] . ", ". $value["branch_state"] . ", " . $value["branch_country"];
	$nestedData[] = $value["branch_city"]. "<br>". $value["warehouse_state"];
	$nestedData[] = $value["branch_country"];
	$nestedData[] = $value["contact_person"];
	$nestedData[] = $value["contact_person_no"];
	$nestedData[] = "<a class='btn green btn-sm btn-circle edit' data-id ='". $value["branch_id"] ."'> Edit </a><br/><a class='btn red btn-sm btn-circle delete' data-id ='". $value["branch_id"] ."'> Delete </a>";
	
	$qweqwe[] = $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"            => $qweqwe   // total data array
			);
echo json_encode($json_data);
