<?php
if($_POST['t']=='create'){
	$qck=mysqli_query($connect, "select ID from `produk_stok_toko` where ID_toko='$_POST[toko]' and ID_produk='$_POST[produk]' and ID_user='$_SESSION[id_user]'");
	$cek = mysqli_num_rows($qck);
	if($cek>0){
		$qc=mysqli_fetch_array($qck);
		$stok = ($_POST['unlimited'])?-1:$_POST['stok'];
		if($stok>0){
			$q=mysqli_query($connect, "UPDATE `produk_stok_toko` SET stok = stok+'$stok' where ID='$qc[ID]' and ID_user='$_SESSION[id_user]'");
		}else{
			$q=mysqli_query($connect, "UPDATE `produk_stok_toko` SET stok = '$stok' where ID='$qc[ID]' and ID_user='$_SESSION[id_user]'");
		}
	}else{
		$stok = ($_POST['unlimited'])?-1:$_POST['stok'];
		$q=mysqli_query($connect, "INSERT INTO `produk_stok_toko` (`ID_user`, `ID_produk`, `ID_toko`, `stok`)
												VALUES ('$_SESSION[id_user]', '$_POST[produk]', '$_POST[toko]', '$stok');");
	}
	if(!$q){
		$data = array('status'=>0,'msg'=>mysqli_error($connect));
	}else{
		$data = array('status'=>1);
	}
	echo json_encode($data);
}elseif($_POST['t']=='update'){
	$stok = ($_POST['unlimited'])?-1:$_POST['stok'];
	$q=mysqli_query($connect, "UPDATE `produk_stok_toko` SET stok = '$stok' where ID='$_POST[id]' and ID_user='$_SESSION[id_user]'");
	if(!$q){
		$data = array('status'=>0,'msg'=>mysqli_error($connect));
	}else{
		$data = array('status'=>1);
	}
	echo json_encode($data);
}elseif($_POST['t']=='delete'){
	$q=mysqli_query($connect, "DELETE FROM `produk_stok_toko` WHERE `ID`='$_POST[id]' and ID_user='$_SESSION[id_user]'");
	if(!$q){
		$data = array('status'=>0,'msg'=>mysqli_error($connect));
	}else{
		$data = array('status'=>1);
	}
	echo json_encode($data);
}else{
	$params = $columns = $totalRecords = $data = array();
	$params = $_POST;
	$where = $sqlTot = $sqlRec = "";
	if( !empty($params['search']['value']) ) {   
		$where .=" AND ";
		$where .=" ( c.nama_toko LIKE '".$params['search']['value']."%' ";
		$where .=" OR b.nama_produk LIKE '".$params['search']['value']."%' ";
		$where .=" OR a.stok LIKE '".$params['search']['value']."%' ) ";
	}
	$sql = "SELECT a.ID,c.nama_toko,b.nama_produk,a.stok FROM `produk_stok_toko` a, `produk` b, `toko` c WHERE b.ID_user=c.ID_user and a.ID_user=c.ID_user and c.ID=a.ID_toko and a.ID_produk=b.ID and a.ID_user='$_SESSION[id_user]'";
	$sqlTot .= $sql;
	$sqlRec .= $sql;
	if(isset($where) && $where != '') {

		$sqlTot .= $where;
		$sqlRec .= $where;
	}
	$sqlRec .=  " order by ID asc LIMIT ".$params['start']." ,".$params['length']." ";
	$queryTot = mysqli_query($connect, $sqlTot) or die("database error:". mysqli_error($connect));
	$totalRecords = mysqli_num_rows($queryTot);
	$queryRecords = mysqli_query($connect, $sqlRec) or die("error to fetch $p");
	$items = array();
	$i=0;
	while( $b = mysqli_fetch_array($queryRecords) ) {
		$i++;
		//$aksi ='<a href="'.getConfig($connect, 'base_url').'?page='.$p.'&edit='.$b['ID'].'" class="btn btn-xs bg-blue-grey waves-effect" data-tooltip="true" data-placement="top" title="Edit"><i class="material-icons">edit</i></a>&nbsp;';
		//$aksi.='<button data-delconfirm="be careful, deleting this data may affect other data<br>because this data is interconnected with other data" class="btn bg-red waves-effect btn-xs hapus_ajax" data-id="'.$b['ID'].'" data-p="'.$p.'" data-t="delete" data-token="'.getToken().'" title="Hapus"><i class="material-icons">delete</i></button>';
		$stok = ($b['stok']>=0)?$b['stok']:'<small><i>unlimited</i></small>';
		$items[] = array($i,$b['nama_toko'],$b['nama_produk'],$stok,$aksi);
	}	
	$json_data = array(
			"draw"            => intval( $params['draw'] ),   
			"recordsTotal"    => intval( $totalRecords ),  
			"recordsFiltered" => intval($totalRecords),
			"data"            => $items  // total data array
			);
	echo json_encode($json_data);
}