	<?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_product=mysqli_real_escape_string($connect, $_GET['id_product']);

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

	if ($count > 0){
	$query = "SELECT * FROM history_stock WHERE id_product='$id_product' AND id_store='$u[id_store]' ORDER BY id_history_stock DESC";
	$hasil = mysqli_query($connect,$query);


	if(mysqli_num_rows($hasil) > 0 ){
  $response = array(
    'status' 	=> 'true',
    'errCode' 	=> '01',
	'msg' 		=> 'Success'
   );
  $response["data"] = array();
  while($x = mysqli_fetch_array($hasil)){
	$databarang = mysqli_query($connect,"SELECT * FROM product WHERE id_product='$x[id_product]'");
	$b=mysqli_fetch_array($databarang);
	
	$datastore = mysqli_query($connect,"SELECT * FROM store WHERE id_store='$x[id_store]'");
	$s=mysqli_fetch_array($datastore);
	  
    $h['name_product'] = $b["name_product"];
    $h['date'] = $x["date"];
    $h['detail'] = "$x[detail] by $u[full_name] ";
    $h['stock'] = $x['stock'];
    $h['status'] = $x['status'];
    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);	
	}
	?>