<?php
include_once "../includes/connectConfig.php";

if($_POST)
{
	if (isset($_POST['search_result'])) 
	{
	$serr = $_POST['search'];

	$word = test_input($serr);
	$word = htmlentities($word);
	
	$src = "SELECT  psell,purl,pdiscount,pqrcode,ksp_id, pname, delete_status FROM kmartstockprds WHERE pname LIKE '%".$word."%'  ";
	
	/*if(!empty($search_ct))
	{
		$cat_id = gtOne('category'," url = '$search_ct'",'id');
		
		if(!empty($cat_id))
		{
			$src .= " AND cat_id = '$cat_id' ";
		}	
	}*/
	
	$src .= " AND delete_status = 0 ORDER BY ksp_id";
	$sql_search = $db_con->query($src);
	$sql_search->execute();
	$count_src = $sql_search->rowCount();
	
	$end_result = '<ul class="prdsz">';	
	if($count_src > 0) {
		
		$src_result = $sql_search->fetchAll();
		
			foreach($src_result as $pdz)
			{
				$result = $pdz['pname'];
				$bold = '<b>' . $word . '</b>';
				$prd_id = $pdz['ksp_id'];
				
				//$ds_c = '<span class="sale">- '.number_format((float)((($pds_dis / $prd['prd_sellprice'])*100)),0).' %</span>';
				
				$gt_imgs = gtOne("kprdw_imgs"," prd_rec = '$pdz[pqrcode]' ORDER BY kmg_id ASC","pjm_pic");
				$prd_img = (!empty($gt_imgs) && file_exists('prdimgs/'.$gt_imgs)) ? "prdimgs/".$gt_imgs : 'img/kmart-icon.png';
				
				$sl_pc = $pdz['psell'];   $ds_pc = $pdz['pdiscount'];
				$shw_ds = (!empty($ds_pc) && $ds_pc > 0) ?  ($sl_pc - $ds_pc) : $sl_pc ;
				$shw_dsc = (!empty($ds_pc) && $ds_pc > 0) ?  '<del><span class="price-amount"><span class="currencySymbol">GH₵</span>'.$sl_pc.'</span></del>' : '' ;
				
			$end_result    .= '
				 <li>
					<div class="minicart-item">
						<div class="thumb">
							<a href="product?kmt='.$pdz['purl'].'" target="_blank"><img src="'.$prd_img.'" width="90" height="90" alt="Cart Icon"></a>
						</div>
						<div class="left-info">
							<div class="product-title"><a href="product?kmt='.$pdz['purl'].'" target="_blank" class="product-name">'.str_ireplace($word, $bold, $result).'</a></div>
							<div class="price">
								<ins><span class="price-amount"><span class="currencySymbol">GH₵</span>'.$shw_ds.'</span></ins>
								<?= $shw_dsc; ?>
							</div>
						</div>
					</div>
				</li>
				';
						
			}	
		$end_result .= '</ul>';
		
	echo $end_result; 
	}
	 else {
			echo '<h3 class="text-center minicart-empty"> No result found</h3>';
		}

	}

}
?>