<?php
if(count(get_included_files()) ==1) exit("Direct access not permitted.");
if(isset($_POST['ikdata'])){
if (!isset($_SESSION)) session_start();
$db=pDatabase::getInstance();
$datafungsi=[
	"getTabel"=>"getTabel",
	"datacari01"=>"datacari01",
	"inputkota"=>"inputkota",
	"editkota"=>"editkota",
	"hapusData"=>"hapusData",
	"updateLokasi"=>"updateLokasi"
];

if(!$db->ceckmodule($cl)) exit;
	$data=$_POST['ikdata'];
	$ex=false;
	if(isset($datafungsi[$data[0]])) $ex=$datafungsi[$data[0]]($data);
	echo $ex;
}else{
	echo false;
}

function gethalaman($qr,$dt,$jumlah){
	$db=pDatabase::getInstance();
	
	$rs=$db->loadAssoc($qr,$dt);
	
	$total=1;
	if(isset($rs['total'])){
		$total=ceil($rs['total']/$jumlah);
	}
	if($total==0){
		return 1;
	}else{
		return $total;
	}
}

function datacari01($data=array())
{
	$mode=0;
	$kunci=array("kota","propinsi","kode");
	$alias=array("Nama kota","Propinsi","Kode Kota");
	$kolom=array("t1.kota","t2.propinsi","t1.kode");
	$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 getTabel($data){
	$db=pDatabase::getInstance();
	$cari=$data[3];
	$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.kota LIKE ? ";
			array_push($datapush,$cr);
		}else{
			$datacari=$db->datacari(datacari01(),$arrcr);
			$datapush=$datacari['datapush'];
			$wh=$datacari['query'];
		}
	}
	
	$qt="SELECT COUNT(*) as total FROM tb_kota as t1
		inner join tb_propinsi as t2 on t1.idpropinsi=t2.id
		WHERE t1.id>0 and t2.status=1 ".$wh;
		
	$total=gethalaman($qt,$datapush,$data[2]);
	array_push($datapush,$data[1],$data[2]);
	
	$query="SELECT t1.id,t1.idpropinsi,t1.kota,t2.propinsi,t1.kode,t1.lokasi,
			if(t1.lokasi is null,'-',t1.lokasi) as lokasipeta,
			$total as halaman FROM tb_kota	as t1
		inner join tb_propinsi as t2 on t1.idpropinsi=t2.id	
		WHERE t1.id>0 and t2.status=1 ".$wh."
		ORDER BY t2.propinsi ASC, t1.kota ASC LIMIT ?,?";
	$rs=$db->jsonData($query,$datapush);
	return $rs;
}
function inputkota($data){
	$db=pDatabase::getInstance();
	$datainput=[
		"idpropinsi"=>$data['idpropinsi'],
		"kode"=>$data['kode'],
		"kota"=>$data['nama'],
		"lokasi"=>$data['lokasi']
	];
	$rs=$db->insert("tb_kota",$datainput);
	return $rs;
}
function editkota($data){
	$db=pDatabase::getInstance();
	$datainput=[
		"idpropinsi"=>$data['idpropinsi'],
		"kode"=>$data['kode'],
		"kota"=>$data['nama'],
		"lokasi"=>$data['lokasi']
	];
	$rs=$db->update("tb_kota",$datainput,"id",$data['id']);
	return $rs;
}
function hapusData($data){
	$db=pDatabase::getInstance();
	$query="DELETE FROM tb_kota WHERE id=?";
	$rs=$db->query($query,array($data[1]));
	return $rs;
}

function updateLokasi($data){
	$db=pDatabase::getInstance();
	$datainput=[
		"lokasi"=>$data['lokasi']
	];
	$rs=$db->update("tb_kota",$datainput,"id",$data['id']);
	return $rs;
}
?>