<?php

include('configi.php');
$phxcrmclient_id = $_COOKIE['phxcrmclient_id'];


//check system type

	
	 include 'configi.php';

		
		$queryphxu="SELECT * from phx_userid where idphx_userid = ".$phxcrmclient_id;

	//	echo $queryphxu;
		$resultphxu=mysqli_query($conn, $queryphxu);


		while($rowphxu=mysqli_fetch_array($resultphxu)) { 

			$comp_type			= $rowphxu['comp_type'];
			$comp_type2			= $rowphxu['comp_type2'];
			$comp_stock_type	= $rowphxu['comp_stock_type'];			
			
			
			
		}


$q2 = $_REQUEST['term'];

$q = trim(strip_tags($_REQUEST['term']));//retrieve the search term that autocomplete sends


if ($comp_type2 == "Retail On Behalf Of"){
	
	$sql = "select * from ".$phxcrmclient_id."_prod_search where prod_status = 'Active' and ( title like '%$q%' or sku like '%$q%') order by title LIMIT 15 ";

	//echo $sql;
	$i = 0;


	$result=mysqli_query($conn, $sql);

	while($row = mysqli_fetch_assoc($result))
	{
		$title				=	$row['title'];
		$uid				=	$row['sel_uid'];
		$sku				=	$row['sku'];
		$prod_retail		=	$row['price'];
		$qty_on_hand		=	$row['qty_on_hand'];
		
		$display_text = $uid ." " . $title . " Price: $ " . $prod_retail;


		$row['value']=$display_text;
		$row['uid']=(int)$uid;
		$row_set[] = $row;//build an array

	$i = $i + 1;
	
	}
	//echo $sql;
	
}else{


	$sql = "select * from ".$phxcrmclient_id."_prod_search where title like '%$q%' or sku like '%$q%' and prod_status = 'Active' order by title LIMIT 15 ";

	//echo $sql;
	$i = 0;


	$result=mysqli_query($conn, $sql);

	while($row = mysqli_fetch_assoc($result))
	{
		$title		=	$row['title'];
		$uid			=	$row['sel_uid'];
		$sku			=	$row['sku'];
		$qty_on_hand		=	$row['qty_on_hand'];
		$price			=	$row['price'];
		
		if ($comp_stock_type == 0){
			
			$display_text = $title . " [".$sku."]" . " $".$price;
			
		}else{
			
			$display_text = $title . " [".$sku."]" . " $".$price . " (" . $qty_on_hand.")";
			
		}

		

		$row['value']=$display_text;
		$row['uid']=(int)$uid;
		$row_set[] = $row;//build an array

	$i = $i + 1;
	
	}

}

echo json_encode($row_set);//format the array into json data


?>