<?php
	header('Content-Type: application/json');
	include("includes/dbconfig.php");
	
	session_write_close();
	
	$err = "";
	
	$server_response = array();
	
	$itemsearchtext = trim($_POST['itemsearchtext']);

	if(isset($_SESSION['connecteduserid']))
	{
		if($_SESSION["connectedusergroup"] == 1 || $_SESSION["connectedusergroup"] == 2)
		{
			if($itemsearchtext == "")
			{
				$err = $err."Item Identification Required!<br>";
			}
			else
			{				
				$itemstockdata = DB::query("SELECT item_stock_level.id, stock_item.item_name, stock_item.item_code, stock_item.item_image_file, stock_item.bar_code AS bar_code FROM item_stock_level INNER JOIN stock_item ON item_stock_level.item = stock_item.id AND stock_item.item_code=%s WHERE item_stock_level.shop IS NULL UNION SELECT item_stock_level.id, stock_item.item_name, stock_item.item_code, stock_item.item_image_file, stock_item.bar_code AS bar_code FROM item_stock_level INNER JOIN stock_item ON item_stock_level.item = stock_item.id AND stock_item.item_name LIKE %ss WHERE item_stock_level.shop IS NULL", $itemsearchtext, $itemsearchtext);
					
				if(DB::count() > 0)
				{
					
				}
				else
				{
					$err = $err."Item Not Identified!<br>";
				}
			}
		}
		else
		{
			$err = $err."Limited Access!<br>";
		}
	}
	else
	{
		$err = $err."Un Authenticated User Access Detected!<br>";
	}
	
	if($err == "")
	{
		$searchresults = '<table id="stockitemstable" class="table table-striped">
								<thead>
									<tr>
										<th>Image</th>
										<th>Name</th>
										<th>Item Code</th>
										<th>Barcode</th>
										<th class="text-center">Actions</th>
									</tr>
								</thead>
								<tfoot>
									<tr>
										<th>Image</th>
										<th>Name</th>
										<th>Item Code</th>
										<th>Barcode</th>
										<th class="text-center">Actions</th>
									</tr>
								</tfoot>
								<tbody>';
								
		foreach($itemstockdata as $row)
		{
			$itemimage = ($row['item_image_file'] != "") ? "images/stockitemimage/" . $row['item_image_file'] : "images/stockitemimage/noimage.gif";
			
			$searchresults .= '<tr>
									<td style="width: 6%; vertical-align:middle;">
										<img class="img-thumbnail" id="logo" src="'.$itemimage.'" alt="..." style="width: 100%;max-height: 100%">
									</td>
									<td style="vertical-align:middle;">'.$row["item_name"].'</td>
									<td style="vertical-align:middle;">'.$row["item_code"].'</td>
									<td style="vertical-align:middle;">'.$row["bar_code"].'</td>
									<td style="vertical-align:middle;" class="text-center">
										<a href="javascript:void(0);" onclick="addtocart(\''.$row["id"].'\', \''.addslashes($row["item_name"]).'\', \''.$row["bar_code"].'\')" data-toggle="tooltip" title="Add Item To Cart" class="tooltips"><span class="label label-primary" ><i class="fa fa-plus"></i></span></a>
									</td>
								</tr>';
		}
		
		$searchresults .= '</tbody>
						</table>
						<!-- AdminLTE App -->
						<script src="dist/js/app.min.js" type="text/javascript"></script>
						<!-- AdminLTE for demo purposes -->
						<script src="dist/js/demo.js" type="text/javascript"></script>
						<script>
							addtocart = function(itemid, itemname, barcode)
							{
								var itemrow = "<tr><td style=\"display:none;\">"+itemid+"</td><td>"+itemname+"</td><td>"+barcode+"</td><td class=\"text-right\">1</td><td class=\"text-right\"><a href=\"javascript:void(0);\" onclick=\"itemupdate(this)\" data-toggle=\"tooltip\" title=\"Edit Cart Item\" class=\"tooltips\"><span class=\"label label-primary\" ><i class=\"fa fa-edit\"></i></span></a> <a href=\"javascript:void(0);\" onclick=\"itemdelete(this)\" data-toggle=\"tooltip\" title=\"Delete Cart Item\" class=\"tooltips\"><span class=\"label label-danger\" ><i class=\"fa fa-trash\"></i></span></a></td></tr>";
								
								$("#cartitemstable > tbody:last-child").append(itemrow);
								document.getElementById("itemcode").value = "";
								
								$("#cartitemslist").slimscroll({scrollTo: "350px"});
								
								var numberofcartitems = 0.00;
									
								$("#cartitemstable").children("tbody").children("tr").each(function(i) 
								{
									$tr = $(this);
									var useindex = 0;

									$("td", $tr).each(function() 
									{
										if(useindex == 3)
										{
											numberofcartitems += parseFloat($(this).text().replace(/[^0-9\.]/g,""));
										}
										
										useindex +=1;
									});
								});
								
								$("#itemscount").html("<b>"+numberofcartitems+"</b>");
							}
						</script>';
		
		$server_response[] = array('isdataobtained' => 1, 'message' => $searchresults);
		echo json_encode($server_response);
	}
	else
	{
		$errormessage = "<div class=\"alert alert-danger\"><button type=\"button\" class=\"close\" data-dismiss=\"alert\"aria-hidden=\"true\">&times;</button>" . $err . "</div>";
		$server_response[] = array('isdataobtained' => 0, 'message' => $errormessage);
		echo json_encode($server_response);
	}
?>