<?php
$modelClass =& get_instance();
$modelClass->load->model('default/defaults');

function _print_child($data,$parent_id,$title,$link,$userid,$level=0){
	$modelClass =& get_instance();
	$menu_pesan = '';
	if($parent_id==262){
		$menu_pesan = ' id="menu_pesan" ';
	}
	if(count($data)>0){
		$top = true;
		foreach ($data as $child) {
			if($child['parent_id']==$parent_id){
				$top = false;
				break;
			}
		}
		if(!$top){
			if($level==1){
				echo '<li class="dropdown-submenu has-sub">';
					echo '<a '.$menu_pesan.' href="'.$link.'">'.$title.' <span class="caret"></span></a>';
					echo "<ul class='dropdown-menu'>";
			}else{
				echo '<li class="dropdown has-sub">';
					echo '<a class="dropdown-toggle" data-toggle="dropdown" '.$menu_pesan.' href="#">'.$title.' <span class="caret"></span></a>';
					echo "<ul class='dropdown-menu'>";
			}
		}else{
			echo '<li >';
				echo '<a '.$menu_pesan.' href="'.$link.'">'.$title.'</a>';
		}
			foreach ($data as $child) {
				$link = '';
				if($child['parent_id']==$parent_id){
					if(empty($child['link'])){
						$link = '#';
					}else{
						$link = base_url().$child['link'];
						if(!empty($child['params'])){
							$tmp = explode('=', $child['params']);
							if(count($tmp)>0){
								$link .= '/type/'.base64_encode($modelClass->encrypt->encode($tmp[1])).'/'.base64_encode($modelClass->encrypt->encode($child['params']));
							}
						}
					}
					_print_child($data,$child['id'],$child['title'],$link,$userid,$child['level']);
				}
			}
			if(!$top) echo "</ul>";
		echo "</li>";
	}else{
		echo '<li>';
			echo '<a href="'.$link.'"><span>'.$title.'</span></a>';
		echo "</li>";
	}
}
$userid = '';
if($this->session->userdata('userid')){
	$userid = $this->session->userdata('userid');
}

if(!empty($userid)){
	$query = $this->nativesession->get('menus');
}else{
	$query ='';
}

if(empty($query)){
	$query = $modelClass->defaults->getParentMenu($userid);
	$this->nativesession->set('menus', $query->result_array());
	$query = $this->nativesession->get('menus');
}
if($query!=false){
	$about=$modelClass->defaults->getMenuAbout();
	echo '<div class="navbar navbar-default" role="navigation">';
	echo '	<div class="navbar-collapse collapse">';
		echo "<ul class='nav navbar-nav'>";
	foreach ($query as $row) {
		$parent_id = $row['id'];
		if($row['parent_id']==''){
			if($row['link']==''){
				$link = '#';
			}else{
				$link = base_url().$row['link'];
				if(!empty($row['params'])){
					$tmp = explode('=', $row['params']);
					if(count($tmp)>0){
						$link .= '/type/'.base64_encode($modelClass->encrypt->encode($tmp[1]));
					}
				}
			}
			if($row['title']=='Delegasi'){
				_print_child($query,$parent_id,"Delegasi<span class='alert'>5</span>",$link,$userid,$row['level']);	
			}else{
				_print_child($query,$parent_id,$row['title'],$link,$userid,$row['level']);	
			}
			
		}
	}
		echo "</ul>";
	echo '<ul class="nav navbar-nav navbar-right">';
	echo '<li><a href="#">help <span class="caret"></span></a>';
	echo '		<ul class="dropdown-menu">';
				if($about!=false){
					foreach ($about->result() as $row) {
						echo '<li onclick="jalankan(\''.$row->link.'\')"><a>'.$row->title.'</a></li>';
					}
				}	
		echo "</ul>";	
	echo '</div>';
	echo '</div>';
	echo '</nav>';
}
?>