<?php

global $db;
$requestData= $_REQUEST;
$today = date("Y-m-d");
$columns = array( 
	0 => 'user_id',
	1 => 'first_name',
	2 => 'username',
	3 => 'groups_name'
	
);
$data = $db->query("SELECT COUNT(user_id) as jumlah FROM user_friend WHERE user_id = '". $_SESSION['user_id'] ."'");
$count = $data[0]["jumlah"];
$totalData = $count;
$totalFiltered = $totalData;

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

$data = $db->query($sql);
$totalFiltered = count($data);
$sql.=" ORDER BY users.user_id asc LIMIT ".$requestData['start']." ,".$requestData['length']."   ";
$data = $db->query($sql);
$qweqwe = array();
foreach ($data as $key => $value) {
    $nestedData = array(); 
	
	$nestedData[] = "<div class='mt-checkbox-list'><label class='mt-checkbox mt-checkbox-outline'><input type='checkbox' id='contact' name='contact[]' value=".$value["user_id"]."><span></span></label></div>";
	$nestedData[] = "<img style='border-radius:50% !important;width:70px !important;height:auto !important'  src='".showImage($value["user_id"])."'  alt='' />";
	$nestedData[] = "<a class='view-detail' data-id ='". $value["user_id"] ."'>" .ucwords($value["first_name"]) ."</a>";
	$nestedData[] = $value["username"];
	
	$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);
