<?php
include '../backend/db/db.php';
if(isset($_REQUEST['term'])) {
	$keyword = htmlspecialchars(strip_tags(trim($_REQUEST['term'])), ENT_QUOTES, 'UTF-8');

	$sugg_json = array();
	$json_row = array();
	$key = preg_replace('/\s+/', ' ', $keyword);

	
	$query = "SELECT id, `item_name` as val FROM items WHERE item_name LIKE '%$key%' GROUP BY item_name 
	UNION ALL 
	SELECT id, `item_code` as val FROM items WHERE item_code LIKE '%$key%' GROUP BY item_code   
	
	LIMIT 20";
	
	$stmt = mysqli_query($conn, $query);

	while($recResult = mysqli_fetch_assoc($stmt)) {
		//$json_row["id"] = 'booksdetail?flg=1&bid='.$recResult['id'];
		$json_row["value"] = $recResult['val'];
		$json_row["label"] = $recResult['val'];
		array_push($sugg_json, $json_row);
	}
	
	//$json_row["id"] = "#";
	// $json_row["value"] = $key;
	// $json_row["label"] = "Click here to send an order request.";
	// array_push($sugg_json, $json_row);

	$jsonOutput = json_encode($sugg_json, JSON_UNESCAPED_SLASHES); 
	print $jsonOutput;
}
?>