<?php
if($_POST['t']=='create'){
	if($_POST['gambar_produk']){
		$foto = simpanGambar($_POST['gambar_produk'],'assets/images/produk/',75);
	}else{
		$foto = NULL;
	}
	$q=mysqli_query($connect, "INSERT INTO `produk` (`ID_user`, `jenis`, `barcode`, `nama_produk`, `date_create`, `harga_beli`, `harga_jual`, `gambar_produk`, `posted_by`, `status`, `hit`)
								  VALUES ('$_SESSION[id_user]', '$_POST[jenis_produk]', '$_POST[barcode]', '$_POST[nama_produk]', '$now', '$_POST[harga_beli]', '$_POST[harga_jual]', '$foto', '$_SESSION[id_user]', '1', '0');");
	if(!$q){
		$data = array('status'=>0,'msg'=>mysqli_error($connect));
	}else{
		$data = array('status'=>1);
	}
	echo json_encode($data);
}elseif($_POST['t']=='update'){
	$q=mysqli_query($connect, "UPDATE `produk` SET `nama_produk`='$_POST[nama_produk]',`jenis`='$_POST[jenis_produk]', `barcode`='$_POST[barcode]',`harga_beli`='$_POST[harga_beli]',`harga_jual`='$_POST[harga_jual]',`status`='$_POST[status]',`date_create`='$now' WHERE `ID`='$_POST[id]' and `ID_user`='$_SESSION[id_user]';");	
	if(!$_POST['old_img']){
		$foto = simpanGambar($_POST['gambar_produk'],'assets/images/produk/',50);
		$q=mysqli_query($connect, "UPDATE `produk` set `gambar_produk`='$foto' 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, "select gambar_produk from produk where ID_user='$_SESSION[id_user]' and ID='$_POST[id]'");
	$b=mysqli_fetch_array($q);
	if(is_file('assets/images/produk/'.$b['gambar_produk'])){
		unlink('assets/images/produk/'.$b['gambar_produk']);
	}
	$q=mysqli_query($connect, "DELETE FROM `produk` 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 .=" ( nama_produk LIKE '".$params['search']['value']."%' ";
		$where .=" OR jenis LIKE '".$params['search']['value']."%' ";
		$where .=" OR harga_jual LIKE '".$params['search']['value']."%' ";
		$where .=" OR barcode LIKE '".$params['search']['value']."%' ";
		$where .=" OR harga_beli LIKE '".$params['search']['value']."%' ) ";
	}
	$sql = "SELECT * FROM `produk` WHERE 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.'&lihat='.$b['ID'].'" class="btn btn-xs bg-grey waves-effect" data-tooltip="true" data-placement="top" title="Lihat"><i class="material-icons">list</i></a>&nbsp;';
		//$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>';
		$gambar='<img src="assets/images/placeholder.png" data-src="'.getConfig($connect, 'base_url').'/image.php?t=produk&id='.$b['ID'].'&s=10" style="height:30px;">';
		$items[] = array($i,$gambar,ucwords($b['jenis']),$b['nama_produk'],$b['barcode'],number_format($b['harga_beli'],0,',','.'),number_format($b['harga_jual'],0,',','.'),$aksi);
	}	
	$json_data = array(
			"draw"            => intval( $params['draw'] ),   
			"recordsTotal"    => intval( $totalRecords ),  
			"recordsFiltered" => intval($totalRecords),
			"data"            => $items  // total data array
			);
	echo json_encode($json_data);
}