<?php

global $db;
$query = $_REQUEST["query"];
$company_id = getCompany($_SESSION['user_id']);
$results = array();
$sql = "SELECT * FROM companies WHERE company_id <> ".$company_id;
if( !empty($query) ){
	$sql.=" AND (name LIKE '%".$query."%' ";
	$sql.=" OR company_no LIKE '%".$query."%') ";
}
$data = $db->query($sql);
foreach ($data as $key => $value) {
	if (getCompanyFriend($company_id,$value["company_id"]) == 0 && getCompanyGroup($value["company_id"]) == 1){
		$results[] = array(
			"value" => $value["name"],
			"desc" => $value["company_id"],
			"img" => showCompanyImage($value["company_id"], "company_id") ,
			"tokens" => array($query, $query . rand(1, 10))
		);
	}
}
echo json_encode($results);