<?php
if(count(get_included_files()) ==1) exit("Direct access not permitted.");
if (!isset($_SESSION)) session_start();
$mod = basename(__FILE__, '.php');
$page->tbCss($mod);
$page->tbJs($mod);
//$page->addLoad("modmenu.load()");

if(isset($_SESSION['menu_sub'])){
	$datamenu=json_encode($_SESSION['menu_sub'],true);
	$page->addLoad("modmenu.buka($datamenu)");
}
if(isset($_GET['scrmenu'])){
	$page->addLoad("modmenu.scrmenuto(".$_GET['scrmenu'].")");
}
?>

<div class="mod_menu_kiri">
	<div class="box_list_menu" >
		<a href="<?php echo NPAGE.'member';?>">
			<div class="mod_list_menu" style="background: url(<?php echo NPAGE;?>module/mod_menu/css/img/home.png) no-repeat;background-size:15px 15px;background-position:0px 6px;">Halaman Utama</div>
		</a>
		
<?php

$query="";
if($_SESSION['profile']['level']==100){
	$query="SELECT t1.`id`,
			t1.`menu`,
			t1.`tautan`,
			t1.`urutan`,
			t1.`level`,
			t1.`icon`,
			t1.`status`,
			t1.`jumlahsub`,
			t1.`jumlah_akses`,
			if(t3.nama_awal is null,'PILIH FILE ICON MENU',t3.nama_awal) as nama_awal,
			if(t3.nama_baru is null,'',t3.nama_baru) as nama_baru,
			if(t3.folder is null,'',t3.folder) as folder
			FROM tb_menu as t1
			inner join tb_menu_akses as t2 on t1.id=t2.idmenu
			left join tb_dokumen as t3 on t1.idicon=t3.id
			WHERE t2.idlevel=? AND t1.status=1  AND t1.idjenis=1
		UNION
		SELECT t1.`id`,
			t1.`menu`,
			t1.`tautan`,
			t1.`urutan`,
			t1.`level`,
			t1.`icon`,
			t1.`status`,
			t1.`jumlahsub`,
			t1.`jumlah_akses`,
			if(t3.nama_awal is null,'PILIH FILE ICON MENU',t3.nama_awal) as nama_awal,
			if(t3.nama_baru is null,'',t3.nama_baru) as nama_baru,
			if(t3.folder is null,'',t3.folder) as folder
			FROM tb_menu as t1
			left join tb_dokumen as t3 on t1.idicon=t3.id
			WHERE (t1.level=100 OR t1.level=0) AND t1.status=1 AND t1.idjenis=1
		ORDER BY urutan ASC";
		
}else{
	$query="SELECT t1.`id`,
			t1.`menu`,
			t1.`tautan`,
			t1.`urutan`,
			t1.`level`,
			t1.`icon`,
			t1.`status`,
			t1.`jumlahsub`,
			t1.`jumlah_akses`,
			if(t3.nama_awal is null,'PILIH FILE ICON MENU',t3.nama_awal) as nama_awal,
			if(t3.nama_baru is null,'',t3.nama_baru) as nama_baru,
			if(t3.folder is null,'',t3.folder) as folder
			FROM tb_menu as t1
			inner join tb_menu_akses as t2 on t1.id=t2.idmenu 
			left join tb_dokumen as t3 on t1.idicon=t3.id
			WHERE t2.idlevel=? AND t1.status=1 AND t1.idjenis=1
		UNION
		SELECT t1.`id`,
			t1.`menu`,
			t1.`tautan`,
			t1.`urutan`,
			t1.`level`,
			t1.`icon`,
			t1.`status`,
			t1.`jumlahsub`,
			t1.`jumlah_akses`,
			if(t3.nama_awal is null,'PILIH FILE ICON MENU',t3.nama_awal) as nama_awal,
			if(t3.nama_baru is null,'',t3.nama_baru) as nama_baru,
			if(t3.folder is null,'',t3.folder) as folder
			FROM tb_menu as t1 
			left join tb_dokumen as t3 on t1.idicon=t3.id
			where t1.level=0 AND t1.idjenis=1
		ORDER BY urutan ASC";
		
}
$datapush=array($_SESSION['profile']['level']);
$rs=$db->getObj($query,$datapush);

if(sizeof($rs)>0){
	foreach ($rs as $obj) {
		$rssub=array();
		$href="#";
		if($obj->tautan!="#") $href=NPAGE."member/mod/".$obj->tautan;
		
		$qsub="";
		if($_SESSION['profile']['level']==100){
			$qsub="SELECT t1.`id`,
				t1.`idmenu`,
				t1.`sub_menu`,
				t1.`tautan`,
				t1.`urutan`,
				t1.`level`,
				t1.`status`,
				t1.`jumlah_akses` FROM tb_menu_sub as t1
				left join tb_menu_sub_akses as t2 on t1.id=t2.idsubmenu 
				WHERE t1.idmenu=? AND (t2.idlevel=? OR t1.level=100) AND t1.status=1 
				ORDER by t1.urutan ASC";
		}else{
			$qsub="SELECT t1.`id`,
				t1.`idmenu`,
				t1.`sub_menu`,
				t1.`tautan`,
				t1.`urutan`,
				t1.`level`,
				t1.`status`,
				t1.`jumlah_akses` FROM tb_menu_sub as t1
				inner join tb_menu_sub_akses as t2 on t1.id=t2.idsubmenu 
				WHERE t1.idmenu=? AND t2.idlevel=? AND t1.status=1
				ORDER by t1.urutan ASC";
		}
		
		$rssub=$db->getObj($qsub,array($obj->id,$_SESSION['profile']['level']));
		$datamn=array("tautan"=>$obj->tautan,"id"=>$obj->id,"sub"=>sizeof($rssub));
		$onklik="onclick='modmenu.menuklik(".json_encode($datamn,true).")'";
		?>
		<div onclick='modmenu.menuklik(<?php echo json_encode($datamn,true);?>)' class="mod_list_menu" style="background: url(<?php echo NPAGE.$obj->folder.$obj->nama_baru;?>) no-repeat;background-size:15px 15px;background-position:0px 6px;"><?php echo $obj->menu;?></div>
		
		<div id="mod_sub_<?php echo $obj->id;?>" class="mod_box_sub_menu">
		<?php
		foreach ($rssub as $sub) {
			$datasub=array("tautan"=>$sub->tautan);
			?>
			<div class="mod_sub_menu" onclick='modmenu.menusubklik(<?php echo json_encode($datasub,true);?>)'><?php echo $sub->sub_menu;?></div>
			<?php
		}
		?>
		</div>
		<?php
	}
}
?>
	</div>	
</div>
