	<?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']);
					$kodebarangs=mysqli_real_escape_string($connect, $_POST['codeproduct']);
					$hargabelis=mysqli_real_escape_string($connect, $_POST['purchase_price']);
					$hargajuals=mysqli_real_escape_string($connect, $_POST['selling_price']);
					$stoks=mysqli_real_escape_string($connect, $_POST['stock']);
					$nama_barang=mysqli_real_escape_string($connect, $_POST['name_product']);
					$minimalstok=mysqli_real_escape_string($connect, $_POST['minimalstock']);
					$deskripsi=mysqli_real_escape_string($connect, $_POST['description']);
					$id_barang=mysqli_real_escape_string($connect, $_POST['id_product']);


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


	$databarang = mysqli_query($connect,"SELECT * FROM product WHERE codeproduct='$kodebarangs' AND user='$m[phone_number]'");
	$b=mysqli_fetch_array($databarang);
	
	$databarangold = mysqli_query($connect,"SELECT * FROM product WHERE codeproduct='$kodebarangs'");
	$db=mysqli_fetch_array($databarangold);

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

	if ($jumlah > 0){
	
		
  include "../fungsi_thumb.php";
  
  $lokasi_file 		= $_FILES['gbr']['tmp_name'];
  $tipe_file   		= $_FILES['gbr']['type'];
  $nama_file   		= $_FILES['gbr']['name'];
  $acak        		= rand(1,99);
  $nama_file_unik	= $acak.$nama_file;
	
	if($kodebarang == $b['codeproduct']){
	
	
	 if (empty($lokasi_file)){
	   mysqli_query($connect,"UPDATE product SET    name_product    = '$nama_barang',
								  purchase_price		 = '$hargabeli',
								  codeproduct	 = '$kodebarangs',
								  selling_price		 = '$hargajual',
								  stock			 = '$stok',
								  minimalstock	 = '$minimalstok',
								  description		 = '$deskripsi'
                            WHERE id_product 	 = '$id_barang'");
   }else{
	    UploadImage($nama_file_unik);
	   mysqli_query($connect,"UPDATE product SET    name_product    = '$nama_barang',
								  purchase_price		 = '$hargabeli',
								  selling_price		 = '$hargajual',
								  codeproduct	 = '$kodebarangs',
								  stock			 = '$stok',
								  minimalstock	 = '$minimalstok',
								  description		 = '$deskripsi',
								  img		 	 = '$nama_file_unik'
                            WHERE id_product 	 = '$id_barang'");
	     
   }
	
	
	
	
	}else{
	
	if($db['img']==''){
		$gbr='';
	}else{
		$gbr=$db['img'];
	}
	
	
	mysqli_query($connect,"INSERT INTO product(name_product,
								  id_category,
								  codeproduct,
								  purchase_price,
								  selling_price,
								  stock,
								  minimalstock,
								  description,
								  img,
                                  user) 
                    VALUES('$nama_barang',
						   '$db[id_category]',
						   '$db[codeproduct]',
						   '$hargabeli',
						   '$hargajual',
						   '$stok',
						   '$minimalstok',
						   '$deskripsi',
						   '$gbr',
                           '$m[phone_number]')");
						   
	
	
	
	}
 
  
  

	$response = array(
    'status' => 'true',
    'errCode' => '01',
	'msg' => 'Success'
	);
	$response["data"] = array();
	
	
	$query = "SELECT * FROM product WHERE codeproduct='$kodebarangs' AND user='$m[phone_number]' ORDER BY id_product DESC";
	$hasil = mysqli_query($connect,$query);
	
	 while($x = mysqli_fetch_array($hasil)){
	  
	//ambil nama kategori
	
	$querykategori = "SELECT name_category FROM category WHERE id_category='$x[id_category]'";
	$hasilkategori = mysqli_query($connect,$querykategori);
	$k = mysqli_fetch_array($hasilkategori);
	  
    $h['id_product'] 	= $x["id_product"];
    $h['name_product'] 	= $x["name_product"];
    $h['id_category'] 	= $x["id_category"];
    $h['name_category'] = $k["name_category"];
    $h['codeproduct'] 	= $x["codeproduct"];
    $h['purchase_price'] 	= $x["purchase_price"];
    $h['selling_price'] 	= $x["selling_price"];
    $h['stock'] 			= $x["stock"];
    $h['minimalstock'] 	= $x["minimalstock"];
    $h['discount'] 		= $x["discount"];
    $h['description'] 	= $x["description"];
    $h['img'] 			= "https://$_SERVER[HTTP_HOST]/geten/images/small_$x[img]";
    array_push($response["data"], $h);
  }

  echo json_encode($response);

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

    echo json_encode($json);	
	}
	?>