<?php
	require_once("../system.engine/system.core.php");
	$l->setPrefix("ajax.handle.search.users");
	
	$mPong = array();
	$a = &$mPong;
	$p = &$_POST;
	
	global $systemHandler;
	$systemExecution = $systemHandler->loadHandler("EXECUTION");
	$systemHash = $systemHandler->loadHandler("HASH");
	$systemMySQLi = $systemHandler->loadHandler("MYSQLI");
	/* ### DO YOUR CODES ONLY FROM HERE ### */
	
	$mLookUp = $p["lookUp"];
	$mLookUpParts = explode(' ', $mLookUp);
	
	$a["resultContent"] = null;
	$a["resultCount"] = 0;
	
	if(count($mLookUpParts) >= 2) 
		$mLookUpQuery = $systemMySQLi->mConnection->query("SELECT id FROM web_user WHERE firstname LIKE '%".$systemMySQLi->mConnection->real_escape_string($mLookUpParts[0])."%' OR lastname LIKE '%".$systemMySQLi->mConnection->real_escape_string($mLookUpParts[1])."%'");
	else
		$mLookUpQuery = $systemMySQLi->mConnection->query("SELECT id FROM web_user WHERE firstname LIKE '%".$systemMySQLi->mConnection->real_escape_string($mLookUp)."%' OR lastname LIKE '%".$systemMySQLi->mConnection->real_escape_string($mLookUp)."%'");
	
	$mCount = 0;
	while($mRow = $mLookUpQuery->fetch_assoc()) {
		$mUser = new systemUSER($mRow["id"]);
		$mUserState = $systemExecution->runQuery("SELECT COUNT(id) AS friendCount FROM web_friends WHERE (user_one_id = '%s' AND user_two_id = '%s') OR (user_one_id = '%s' AND user_two_id = '%s')", array("{user.id}", $mUser->mUserId, $mUser->mUserId, "{user.id}"))->fetch_object()->friendCount > 0 ? 2 : ($systemExecution->runQuery("SELECT COUNT(id) AS friendWaitingCount FROM web_friendrequests WHERE (sender_id = '%s' AND receiver_id = '%s') OR (sender_id = '%s' AND receiver_id = '%s')", array("{user.id}", $mUser->mUserId, $mUser->mUserId, "{user.id}"))->fetch_object()->friendWaitingCount > 0 ? 1 : 0);
		if($mUser->mUserId !== $systemHandler->loadHandler("USER")->mUserId) {
			$a["resultContent"] .= "<a data-event=\"send-profile\" data-parameter=\"".$mUser->mUserId."\" href=\"javascript:;\" class=\"list-group-item\">
									<img class=\"profilePictureSearch\" src=\"".$mUser->mProfilePicture."\" />
												
									<div class=\"userName\">
										".$mUser->mFullname."
									</div>
												
									<div class=\"userDesc\">
										".$mUser->mFirstname." ".$l->getValue("has")." <strong>".$mUser->mFriendsCount."</strong> ".$l->getValue("friends").".
									</div>
									<div class=\"userEvents\">
										<div style=\"width: 100%\" class=\"btn-group userEventsBtnGroup\" role=\"group\" aria-label=\"userEvents\">
											<button style=\"width: 33%\" data-event=\"initialize-chat\" data-parameter=\"".$mUser->mUserId."\" type=\"button\" class=\"btn btn-default\"><i class=\"glyphicon glyphicon-comment\"></i></button>
											
											<button style=\"width: 33%\" data-event=\"send-profile\" data-parameter=\"".$mRow["id"]."\" type=\"button\" class=\"btn btn-default\"><i class=\"glyphicon glyphicon-user\"></i></button>
											
											<button style=\"width: 33%\" data-mini=\"true\" data-event=\"".($mUserState == 0 ? "add" : ($mUserState == 1 ? "remove" : "remove"))."-friend-mini\" data-parameter=\"".$mUser->mUserId."\" type=\"button\" class=\"btn btn-".($mUserState == 0 ? "primary" : ($mUserState == 1 ? "warning" : "danger"))."\"><i class=\"glyphicon glyphicon-".($mUserState == 0 ? "plus" : ($mUserState == 1 ? "time" : "minus"))."\"></i></button>
										</div>
									</div>
								</a>";
			$mCount++;
		}
	}
	
	$a["resultCount"] = $mCount;
	
	/* ### DO NOT CHANGE ANYTHING FROM HERE ### */
	header("Content-Type: application/json");
	echo json_encode($mPong);
?>