<?php

global $db;
$requestData= $_REQUEST;
$today = date("Y-m-d");
$columns = array(
	0 => 'user_id',
	1 => 'username',
	2 => 'password',
	3 => 'created_on',
	4 => 'groups_name',
	5 => 'status',

);
$data = $db->query("SELECT COUNT(user_id) as jumlah FROM users WHERE role <> '0'");
$count = $data[0]["jumlah"];
$totalData = $count;
$totalFiltered = $totalData;

$sql = "SELECT user_detail.first_name as first_name,users.user_id as user_id, users.username as username,  users.created_on as created_on, groups.name, users.status  FROM `users` join user_detail on users.user_id = user_detail.user_id join groups on users.group_id = groups.group_id WHERE users.role <> '0' ";
if( !empty($requestData['columns'][0]['search']['value']) ){   //name
	$sql.=" AND users.user_id LIKE '".$requestData['columns'][0]['search']['value']."%' ";
}
if( !empty($requestData['columns'][1]['search']['value']) ){   //name
	$sql.=" AND users.username 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 ( users.created_on >= '".$minRange."' AND  users.created_on <= '".$maxRange."' ) ";
}
if( !empty($requestData['columns'][6]['search']['value']) ){   //name
	$sql.=" AND users.group_id = '".$requestData['columns'][6]['search']['value']."' ";
}
if( !empty($requestData['columns'][7]['search']['value']) ){   //name
	$sql.=" AND users.status = '".$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();

	$nestedData[] = $value["user_id"];
	$nestedData[] = strtoupper($value["username"]);
	$nestedData[] = getProfileData($value["user_id"], "first_name") . " " . getProfileData($value["user_id"], "last_name");
	//$nestedData[] = $value["password"];
	$nestedData[] = date('F d, Y', strtotime($value["created_on"]));
	$nestedData[] = $value["name"];
	$nestedData[] = ($value["status"] != "0" ? "<span class='label label-success '>ACTIVE</span>" : "<span class='label label-danger'>BLOCKED</span>");
	$nestedData[] = ($value["status"] == "0" ? "<a class='btn btn-info btn-sm unban' data-id='" . $value['user_id'] . "'><i class='fa fa-thumbs-up'></i> UNBLOCK</a>" : "<a class='btn  btn-sm btn-danger ban' data-id='" . $value["user_id"] . "'><i class='fa fa-thumbs-down'></i> BLOCK</a>"). "&nbsp;<a class='btn btn-success  btn-sm godmode' data-id='".$value["user_id"]."'><i class='fa fa-sign-in'></i> LOGIN </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);
