<?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",
		"getdatapegawai"=>"getdatapegawai"
	];
	
	$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","nip");
	$alias=array("Nama","NIP");
	$kolom=array("t1.nama","t1.nip");
	$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 getdatapegawai($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'];
		}
	}
	
	$idmodule=$db->getidmodulenama($_SESSION['moduleload']);
	$wh.="and t1.status in (select idstatus from tb_master_status_view where idmodule=? )";
	array_push($datapush,$idmodule);
	
	
	$posisihalaman=($data['halaman']-1)*$data['jumlah'];
	array_push($datapush,$posisihalaman,$data['jumlah']);
	
	$query="SELECT  
				SQL_CALC_FOUND_ROWS t1.id,
				t1.`id`,
				t1.nip,
				t1.`tanggal_input`,
				t1.`nama`,
				t1.`idpropinsi`,
				t1.`idkota`,
				t1.`alamat`,
				t1.tempatlahir,
				t1.tanggallahir,
				t1.`idjeniskelamin`,
				t1.`idgolongan`,
				t1.`idjabatanstruk`,
				t1.`idjabatanfung`,
				t1.`idpendidikan`,
				t1.`namasekolah`,
				t1.`nohp`,
				t1.`email`,
				t1.`notelp`,
				t1.`tanggal_sk_gol`,
				t1.`tanggal_sk_struk`,
				t1.`tanggal_sk_fung`,
				t1.`status`,
				t2.nama as jeniskelamin,
				if(t3.kode is null,'',t3.kode) as golongan,
				if(t4.nama is null,'',t4.nama) as jabatanstruk,
				if(t5.nama is null,'',t5.nama) as jabatanfung,
				a1.nama as namastatus,
				a1.bg,a1.warna
			FROM tb_pegawai as t1
		inner join tb_jenis_kelamin as t2 on t1.idjeniskelamin=t2.id
		left join tb_master_golongan as t3 on t1.idgolongan=t3.id
		left join tb_master_jabatan as t4 on t1.idjabatanstruk=t4.id
		left join tb_master_jabatan_fungsional as t5 on t1.idjabatanfung=t5.id
		inner join tb_master_status as a1 on t1.status=a1.id
		WHERE t1.id>0 ".$wh."
		order BY t1.nama 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));
}
?>