	<?php
	/**
	* Created by PhpStorm.
	* User: ahmad
	* Date: 20/08/2019
	* Time: 11:48 AM
	*/

	require_once("../connection.php");
	
	$key=mysqli_real_escape_string($connect, $_GET['key']);
	$id_category=mysqli_real_escape_string($connect, $_GET['id_category']);
	
	if($id_category==''){
		$data_category= "%$id_category%";
	}else{
		$data_category= $id_category;
	}

	$getusers	= mysqli_query($connect,"SELECT * FROM users WHERE id_session='$key'");
	$u	   		= mysqli_fetch_array($getusers);
	$jumlah   	= mysqli_num_rows($getusers);
	
	$masterakun = mysqli_query($connect,"SELECT * FROM users WHERE phone_number='$u[master]'");
	$m=mysqli_fetch_array($masterakun);

	if ($jumlah > 0){
	

	$sql  = mysqli_query($connect,"SELECT a.user,a.id_product,a.tax,a.name_product,a.id_category,a.unit,a.codeproduct,a.selling_price,a.stock,a.have_stock,a.description,a.folder,a.img,a.showing,a.alertstock,b.id_category,b.name_category,b.status FROM product as a JOIN category as b ON a.id_category=b.id_category AND a.id_category LIKE '$data_category' AND b.status='0' AND a.showing='0' AND a.user='$m[phone_number]'");

	if(mysqli_num_rows($sql) > 0 ){
  $response = array(
    'status' 	=> 'true',
    'errCode' 	=> '01',
	'msg' 		=> 'Success'
   );
  $response["data"] = array();
  while($x = mysqli_fetch_array($sql)){
	 
    $h['id_product'] 	= $x["id_product"];
    $h['posisi'] 		= true;
    $h['name_product'] 	= $x["name_product"];
    $h['unit'] 	= $x["unit"];
    $h['id_category'] 	= $x["id_category"];
    $h['name_category'] = $x["name_category"];
    $h['codeproduct'] 	= $x["codeproduct"];
    $h['tax'] 	= $x["tax"];
    $h['selling_price'] 	= $x["selling_price"];
    $h['stock'] 			= $x["stock"];
    $h['have_stock'] 		= $x["have_stock"];
    $h['description'] 	= $x["description"];
    $h['img'] 			= "http://$_SERVER[HTTP_HOST]/geten/images/$x[folder]/small_$x[img]";
	$h['alertstock'] 	= $x["alertstock"];
    array_push($response["data"], $h);
  }
  echo json_encode($response);
	}else {
  $response = array(
    'status' 	=> 'true',
    'errCode' 	=> '02',
	'msg' 		=> 'no data'
   );
  echo json_encode($response);
	}


	}else{
	
	
 $json = array(
    'status' 	=> 'true',
    'errCode' 	=> '00',
	'msg' 		=> 'User Not Found'
   );

    echo json_encode($json);	
	}
	?>
