<?php

global $db;
ini_set("display_errors", "0");
error_reporting(E_ALL);
$requestData= $_REQUEST;
if($_SESSION['group_id'] == 2){
	$columns = array( 
		0 => 'first_name',
		1 => 'position',
		2 => 'branch_name'
		
	);
}else{
	
	$columns = array( 
		0 => 'first_name',
		1 => 'position'
		
	);
}
$data = $db->query("SELECT COUNT(company_id) as jumlah FROM user_company join user_detail on user_company.user_id = user_detail.user_id  WHERE user_company.user_id <> '" . $_SESSION['user_id'] . "' and user_company.company_id = '" . getCompany($_SESSION['user_id']) . "'");
$count = $data[0]["jumlah"];
$totalData = $count;
$totalFiltered = $totalData;

$sql = "SELECT branches.branch_name,branches.branch_id,user_company.id, user_company.position, user_detail.first_name, user_detail.last_name FROM user_company join user_detail on user_company.user_id = user_detail.user_id join branches on user_company.branch_id = branches.branch_id WHERE user_company.user_id <> '" . $_SESSION['user_id'] . "' and user_company.company_id = '" . getCompany($_SESSION['user_id']) . "'";
// if( !empty($requestData['columns'][0]['search']['value']) ){   //name
	// $sql.=" AND user_id.user_id LIKE '".$requestData['columns'][0]['search']['value']."%' ";
// }
// if( !empty($requestData['columns'][1]['search']['value']) ){   //name
	// $sql.=" AND user_id.uname LIKE '%".$requestData['columns'][1]['search']['value']."%' ";
// }
// if( !empty($requestData['columns'][2]['search']['value']) && !empty($requestData['columns'][3]['search']['value'])){   //name
	// $minRange = $requestData['columns'][2]['search']['value'];
	// $maxRange = $requestData['columns'][3]['search']['value'];
	// $sql.=" AND ( user_id.register_date >= '".$minRange."' AND  user_id.register_date <= '".$maxRange."' ) ";
// }
// if( !empty($requestData['columns'][4]['search']['value']) ){   //name
	// $sql.=" AND parent_detail.uname LIKE '%".$requestData['columns'][4]['search']['value']."%' ";
// }
// if( !empty($requestData['columns'][5]['search']['value']) ){   //name
	// $sql.=" AND spons_detail.uname LIKE '%".$requestData['columns'][5]['search']['value']."%' ";
// }
// if( !empty($requestData['columns'][6]['search']['value']) ){   //name
	// $sql.=" AND user_id.product = '".$requestData['columns'][6]['search']['value']."' ";
// }
// if( !empty($requestData['columns'][7]['search']['value']) ){   //name
	// $sql.=" AND user_id.banned = '".$requestData['columns'][7]['search']['value']."' ";
// }

$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(); 
	if ($value["position"] == 1){
		$position = "Admin";
	}elseif($value["position"] == 2){		
		$position = "Admin Branch";
	}else{
		$position = "Staff";
	}
	$nestedData[] = $value["first_name"] . " " . $value["last_name"];
	$nestedData[] = $position;
	
	if($_SESSION['group_id'] == 2){
	$nestedData[] = $value["branch_name"];
	}
	$nestedData[] = "<a class='btn green btn-sm btn-circle edit' data-id ='". $value["id"] ."'> Edit </a><br/><a class='btn red btn-sm btn-circle delete' data-id ='". $value["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);
