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

	require_once("../connection.php");
	
	$key=mysqli_real_escape_string($connect, $_POST['key']);
	$hargabelis=mysqli_real_escape_string($connect, $_POST['hargabeli']);
	$hargajuals=mysqli_real_escape_string($connect, $_POST['hargajual']);
	$stoks=mysqli_real_escape_string($connect, $_POST['stok']);
	$nama_barang=mysqli_real_escape_string($connect, $_POST['nama_barang']);
	$kodebarangs=mysqli_real_escape_string($connect, $_POST['kodebarang']);

	$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);


	$querybarang = mysqli_query($connect,"SELECT * FROM product WHERE user='$m[phone_number]' ORDER BY id_product DESC");
	$p=mysqli_fetch_array($querybarang);


	$hargabeli	= preg_replace("/[^0-9]/", "", $hargabelis);
	$hargajual	= preg_replace("/[^0-9]/", "", $hargajuals);
	$stok		= preg_replace("/[^0-9]/", "", $stoks);

	if($kodebarangs==''){
	$kodebarang =$p['id_product'];
	}else{
	$kodebarang =$kodebarangs;
	}

	if ($jumlah > 0){
	
	mysqli_query($connect,"INSERT INTO product(name_product,
								  id_category,
								  codeproduct,
								  purchase_price,
								  selling_price,
								  stock,
								  img,
                                  user) 
                    VALUES('$nama_barang',
						   '52',
						   '$kodebarang',
						   '$hargabeli',
						   '$hargajual',
						   '$stok',
						   'no_product.jpg',
                           '$m[no_telp]')");
  
  

  $response = array(
    'status' 	=> 'true',
    'errCode' 	=> '01',
	'msg' 		=> 'Success'
   );
  $response["data"] = array();
	
	
	$query = "SELECT * FROM barang WHERE kodebarang='$kodebarang' AND user='$m[no_telp]' ORDER BY id_barang DESC";
	$hasil = mysqli_query($connect,$query);
	
	 while($x = mysqli_fetch_array($hasil)){
	  
	//ambil nama kategori
	
	$querykategori = "SELECT nama_kategori FROM kategori WHERE id_kategori='$x[id_kategori]'";
	$hasilkategori = mysqli_query($querykategori);
	$k = mysqli_fetch_array($hasilkategori);
	  
    $h['id_barang'] 	= $x["id_barang"];
    $h['nama_barang'] 	= $x["nama_barang"];
    $h['id_kategori'] 	= $x["id_kategori"];
    $h['nama_kategori'] = $k["nama_kategori"];
    $h['kodebarang'] 	= $x["kodebarang"];
    $h['hargabeli'] 	= $x["hargabeli"];
    $h['hargajual'] 	= $x["hargajual"];
    $h['stok'] 			= $x["stok"];
    $h['minimalstok'] 	= $x["minimalstok"];
    $h['diskon'] 		= $x["diskon"];
    $h['deskripsi'] 	= $x["deskripsi"];
    $h['gbr'] 			= "https://$_SERVER[HTTP_HOST]/api/images/small_$x[gbr]";
    array_push($response["data"], $h);
	}

	echo json_encode($response);

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

    echo json_encode($json);	
	}
	?>