<?php
require_once('../includes/sysinit.php');

$searchString = $_POST['input_id'] ?? 0;

$wstmnt = "where (client_name like '%".$searchString."%') or (client_surname like '%".$searchString."%') or 
(client_cell like '%".$searchString."%') or (client_phone like '%".$searchString."%')";
$where = $searchString ? $wstmnt : null;

$sql = "select client.client_id as VAL, concat(client_name, ' ', client_surname, ' [', client_cell, ']') as DESCR 
		from client 
		inner join building on building.client_id = client.client_id ".$where."
		group by building.client_id  
		order by concat(client_name, ' ', client_surname, ' [', client_cell, ']')";

$rows = $sableDB->getRows($sql);

$html = "";
if (empty($rows))
{
	$html = '<option>No results...</option>';
}
else
{
	foreach($rows as $row)
	{
		$html .= '<option value="'.SableSystem::sableEncode($row['VAL']).'">'.trim($row['DESCR']).'</option>';
	}
}
SableSystem::ajaxFillField($html, $_POST['field_id']);