<?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();
	$db=pDatabase::getInstance();
	$datafungsi=[
		"datacari01"=>"datacari01",
		"getTabel"=>"getTabel",
		"getdatakota"=>"getdatakota",
	];
	
	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("nama","alamat","id");
	$alias=array("Nama peserta","Alamat","ID");
	$kolom=array("t1.nama","t1.alamat","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.nama LIKE ? ";
			array_push($datapush,$cr);
		}else{
			$datacari=$db->datacari(datacari01(),$arrcr);
			$datapush=$datacari['datapush'];
			$wh=$datacari['query'];
		}
	}
	
	if($_SESSION['profile']['marketing']==1){
		$wh.="and t1.idmarketing=? ";
		array_push($datapush,$_SESSION['profile']['idmarketing']);
	}
	
	$qt="SELECT COUNT(*) as total FROM tb_toko_peserta t1
		inner join tb_jenis_kelamin as t2 on t1.idjeniskelamin=t2.id
		inner join tb_propinsi as t3 on t1.idpropinsi=t3.id
		inner join tb_kota as t4 on t1.idkota=t4.id
		inner join tb_level_peserta as t5 on t1.level_peserta=t5.id
		inner join tb_master_status as a1 on t1.status=a1.id
		left join tb_marketing as c1 on t1.idmarketing=c1.id
		WHERE t1.id>0 ".$wh;
		
	$total=gethalaman($qt,$datapush,$data[2]);
	array_push($datapush,$data[1],$data[2]);
	
	array_unshift($datapush,$_SESSION['profile']['level'],$_SESSION['profile']['level']);
	
	$query="SELECT t1.`id`,
				t1.`tanggal`,
				t1.`kode`,
				t1.`urutan`,
				t1.`nama`,
				t1.idjeniskelamin,
				t1.idmarketing,
				t1.level_peserta,
				t1.`nohp`,
				t1.`email`,
				t1.`nik`,
				t1.`alamat`,
				t1.`idpropinsi`,
				t1.`idkota`,
				t1.omset,
				t1.point,
				t1.`status`,
				t2.nama as jeniskelamin,
				t3.propinsi,
				t4.kota,
				t5.nama as levelpeserta,
				a1.nama as namastatus,
				a1.bg,a1.warna,
				if(c1.nama is null,'Tidak ada',c1.nama) as namamarketing,
				if(b1.id is null,0,1) as edit,
				if(b2.id is null,0,1) as hapus,
			$total as halaman FROM tb_toko_peserta as t1
		inner join tb_jenis_kelamin as t2 on t1.idjeniskelamin=t2.id
		inner join tb_propinsi as t3 on t1.idpropinsi=t3.id
		inner join tb_kota as t4 on t1.idkota=t4.id
		inner join tb_level_peserta as t5 on t1.level_peserta=t5.id
		inner join tb_master_status as a1 on t1.status=a1.id
		left join tb_master_status_level as b1 on a1.id=b1.idstatus and b1.mode=0 and b1.idlevel=?
		left join tb_master_status_level as b2 on a1.id=b2.idstatus and b2.mode=1 and b2.idlevel=?
		left join tb_marketing as c1 on t1.idmarketing=c1.id
		WHERE t1.id>0 ".$wh."
		ORDER BY t1.urutan ASC LIMIT ?,?";
	$rs=$db->jsonData($query,$datapush);
	
	return $rs;
}

function getdatakota($data){
	$db=pDatabase::getInstance();
	$query="select * from tb_kota where idpropinsi=? ";
	$rs=$db->jsonData($query,array($data['idpropinsi']));
	return $rs;
}

?>