<?php
if($_POST['t']=='delete'){
	$q=mysqli_query($connect, "DELETE FROM `admin_kritik` WHERE `ID`='$_POST[id]'");
	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 .=" ( a.kritik LIKE '".$params['search']['value']."%' ";
		$where .=" OR b.user_nama LIKE '".$params['search']['value']."%' ";
		$where .=" OR a.date_create LIKE '".$params['search']['value']."%' ) ";
	}
	$sql = "SELECT a.*,b.user_nama FROM `admin_kritik` a,`users` b WHERE b.ID=a.ID_user";
	$sqlTot .= $sql;
	$sqlRec .= $sql;
	if(isset($where) && $where != '') {

		$sqlTot .= $where;
		$sqlRec .= $where;
	}
	$sqlRec .=  " order by a.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.='<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>';
		$items[] = array($i,$b['user_nama'],$b['date_create'],strip_tags($b['kritik']),$aksi);
	}	
	$json_data = array(
			"draw"            => intval( $params['draw'] ),   
			"recordsTotal"    => intval( $totalRecords ),  
			"recordsFiltered" => intval($totalRecords),
			"data"            => $items
			);
	echo json_encode($json_data);
}