<?php

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

$sql = "SELECT DISTINCT company_friend.marking_no as marking_no, companies.company_id as company_id, companies.name as name, companies.company_no as company_no, companies.contact_person as contact_person, companies.contact_person_no as contact_person_no  FROM company_friend join companies on company_friend.friend_id = companies.company_id WHERE company_friend.company_id = '". $company_id ."'";
// if( !empty($requestData['columns'][0]['search']['value']) ){   //name
	// $sql.=" AND companies.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 companies.company_id asc LIMIT ".$requestData['start']." ,".$requestData['length']."   ";
$data = $db->query($sql);
$qweqwe = array();
foreach ($data as $key => $value) {
    $nestedData = array(); 
	
	$nestedData[] = "<img style='border-radius:50% !important;width:70px !important;height:auto !important'  src='".showCompanyImage($value["company_id"], "company_id")."'  alt='' />";
	$nestedData[] = "<a class='view-detail' data-id ='". $value["company_id"] ."'>" .ucwords($value["name"]) ."</a>";
	$nestedData[] = "Contact Person : ".$value["contact_person"] . "</br>Contact Person No. : " . $value['contact_person_no'];
	$html = "";
	
	if(getStatusFriendCompany($value["company_id"]) == true){
		$nestedData[] = "Waiting for approval";
		$nestedData[] = "";
		
	}else{
		$nestedData[] = "Approved";
		$nestedData[] = "<input type='radio' name='fid' data-company_name='".ucwords($value["name"])."' id='fid' value='".$value["company_id"]."'>";
	}
	
	$nestedData[] = $html;
	$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);
