<?php
if(count(get_included_files()) ==1) exit("Direct access not permitted.");
header("Content-type: application/json");
if(isset($_POST['ikdata'])){
	if (!isset($_SESSION)) session_start();
	if(!isset($_SESSION['_loadplugin'])) exit;
	if(!in_array($cl,$_SESSION['_loadplugin'])) exit;
	
	$db=pDatabase::getInstance();
	$datafungsi=[
		"datacari01"=>"datacari01",
		"getdatastatus"=>"getdatastatus"
	];
	
	$data=$_POST['ikdata'];
	$ex=false;
	if(isset($datafungsi[$data[0]])) $ex=$datafungsi[$data[0]]($data,$cl);
	echo $ex;
}

function datacari01($data=array())
{
	$mode=0;
	$kunci=array("nama","kategori");
	$alias=array("Nama","Kategori");
	$kolom=array("t1.nama","t2.nama");
	$jenis=array();
	
	$hasil=array("kunci"=>$kunci,"kolom"=>$kolom,"alias"=>$alias,"jenis"=>$jenis);
	if(sizeof($data)>0){
		$mode=$data[1];
	}
	if($mode==0){
		return $hasil;
	}else{
		return json_encode($hasil);
	}
}

function getdatastatus($data){
	$db=pDatabase::getInstance();
	$cari=$data['cari'];
	$arrcr=explode("&", $cari);
	
	$datapush=array();
	$wh="";
	if(sizeof($arrcr)>0){
		$arrkunci0=explode("=", $arrcr[0]);
		if(sizeof($arrcr)==1 && sizeof($arrkunci0)==1){
			$cr="%$cari%";
			$wh.="AND t1.nama LIKE ? ";
			array_push($datapush,$cr);
		}else{
			$datacari=$db->datacari(datacari01(),$arrcr);
			$datapush=$datacari['datapush'];
			$wh=$datacari['query'];
		}
	}
	
	if($data['idkategori']>0){
		$wh.="and t1.idkategori=? ";
		array_push($datapush,$data['idkategori']);
	}
	
	$posisihalaman=($data['halaman']-1)*$data['jumlah'];
	array_push($datapush,$posisihalaman,$data['jumlah']);
	
	$query="SELECT  
				SQL_CALC_FOUND_ROWS t1.id,
				t1.`id`,
				t1.`idkategori`,
				t1.`nama`,
				t1.`urutan`,
				t1.`edit`,
				t1.`hapus`,
				t1.`update_revis`,
				t1.`bukakunci`,
				t1.`bg`,
				t1.`warna`,
				t1.`status`,
				t2.nama as kategori
			FROM tb_master_status as t1
		inner join tb_kategori_status as t2 on t1.idkategori=t2.id
		WHERE t1.id>0 and t2.status=1 ".$wh."
		order BY t1.idkategori asc,t1.id asc LIMIT ?,?";
	
	$rs=$db->loadAll($query,$datapush);
	
	$qrow="SELECT FOUND_ROWS() as totalbaris;";
	$rsrow=$db->loadAssoc($qrow);
	$jumlahhalaman=1;
	if(isset($rsrow['totalbaris'])){
		$jumlahhalaman=ceil($rsrow['totalbaris']/$data['jumlah']);
	}
	
	return json_encode(array("data"=>$rs,"totalhalaman"=>$jumlahhalaman));
}
?>