<?php $ci = get_instance();?>
<aside class="left-panel collapsed" style="">
	<div class="logo" style="text-align: center;">
		<a href="<?php echo base_url();?>" class="logo-expanded">
			<!-- <span class="nav-label" style="margin-left: 5px; margin-right: 5px; font-size: 18px;">
				<?php echo $ci->fm->decrypt($this->company_name);?>
			</span> -->
			<img src="<?php echo base_url("assets/img/logo.png");?>" class="img_logo" style="width: 100px; display: block; margin: 0 auto 0 auto;">
		</a>
	</div>

	<!-- Navbar Start -->
	<nav class="navigation" style="margin-top: 20px;">
		<ul class="list-unstyled">
			<?php 
				$menu_array = $this->menu_model->get_data(array(
					'condition' => "<T>.SHOW_ON_MENU = 1 AND <T>.AVAILABLE = 1",
					'sort' => "WEB_MENU.SEQN",
					'additional_join' => "LEFT JOIN WEB_ACCESS ON WEB_ACCESS.MENU = <T>.PKKEY AND WEB_ACCESS.USER_GROUP = '".$this->session->userdata('group_id')."'",
					'additional_select' => "WEB_ACCESS.PKKEY AS ACCESS_PKKEY",
					'' => "",
				));
				$parent_menu_array = array();
				$all_child_menu = array();
				foreach($menu_array as $v){
					if($v->PARENT_ID == "" || $v->PARENT_ID == "0"){
						array_push($parent_menu_array, $v);
					}
					else{
						array_push($all_child_menu, $v);
					}
				}
				
				$parent_child_array = array();
				foreach($parent_menu_array as $pm){
					$parent_child_array[$pm->PKKEY] = array();
				}
				
				foreach($all_child_menu as $cm){
					if(! isset($parent_child_array[$cm->PARENT_ID])){
						$parent_child_array[$cm->PARENT_ID] = array();
					}
					
					array_push($parent_child_array[$cm->PARENT_ID], $cm);
				}
			?>
			<?php foreach($parent_menu_array as $pm):?>
				<?php if(count($parent_child_array[$pm->PKKEY]) == 0):?>
					<?php if($pm->EVERYONE_ACCESS || $pm->ACCESS_PKKEY != ""):?>
						<?php 
							$active_string = "";
							if( ($this->uri->segment(1) == str_replace("/", "", $pm->URL) || ($this->uri->segment(1) == "" && str_replace("/", "", $pm->URL) == $this->router->routes['default_controller'])) && $pm->EXE != "1" ){
								$active_string = "class='active'";
							}
							
							$href = "";
							if($pm->EXE != "1"){
								$href .= base_url($pm->URL);
							}
							else{
								$href .= "javascript: call_exe('".$pm->PKKEY."'";
								if(isset($pm->TYPE) && $pm->TYPE == "EXE") $href .= ", true";
								$href .= ");";
							}
						?>
					<li <?php echo $active_string;?>>
						<a href="<?php echo $href;?>">
							<i class="<?php echo $pm->ICON_SET;?> <?php echo $pm->ICON?> fa-fw" style=""></i>
							<span class="nav-label"><?php echo $pm->NAME;?></span>
						</a>
					</li>
					<?php endif;?>
				<?php else:?>
					<?php $child_is_current_page = FALSE;?>
					<?php $child_has_access = FALSE;?>
					<?php 
						foreach($parent_child_array[$pm->PKKEY] as $cm){
							if($cm->EVERYONE_ACCESS || $cm->ACCESS_PKKEY != ""){
								$child_has_access = TRUE;
								
								if($this->uri->segment(1) == str_replace("/", "", $cm->URL) && $cm->EXE != "1"){
									$child_is_current_page = TRUE;
									break;
								}
							}
						}
					?>
					
					<?php if($child_has_access):?>
						<li class="has-submenu <?php echo $child_is_current_page? "child_has_access":""?>">
							<a href="javascript:;" title="<?php echo $pm->NAME;?>" data-toggle="dropdown" class="menu_btn_parent">
								<i class="<?php echo $pm->ICON_SET;?> <?php echo $pm->ICON;?> fa-fw" style=""></i>
								<span class="nav-label"><?php echo $pm->NAME;?></span>
								<b class="fa fa-caret-down pull-right menu_caret" style="margin-top: 3px;"></b>
							</a>
							<ul class="list-unstyled">
								<?php $has_menu_before_divider = FALSE;?>
								<?php $has_divider = FALSE;?>
								<?php foreach($parent_child_array[$pm->PKKEY] as $cm):?>
									<?php if($cm->EVERYONE_ACCESS || $cm->ACCESS_PKKEY != ""):?>
										<?php $report = substr($cm->URL, 0, 4) == "RPT:"? substr($cm->URL, 4):"";?>
										<?php if($cm->SEQN <= 50):?>
											<?php $has_menu_before_divider = TRUE;?>
										<?php else:?>
											<?php if(! $has_divider && $has_menu_before_divider):?>
<!--												<li class="divider"></li>-->
												<li>
													<hr style="margin-top: 2px; margin-bottom: 2px; border-color: #9E9E9E;">
												</li>
												<?php $has_divider = TRUE;?>
											<?php endif;?>
										<?php endif;?>
										<?php 
											$href = "";
											if($report != ""){
												$href = "javascript: crystal_report('".$report."');";
											}
											elseif($cm->EXE == "1"){
												$href .= "javascript: call_exe('".$cm->PKKEY."'";
												if(isset($cm->TYPE) && $cm->TYPE == "EXE") $href .= ", true";
												$href .= ");";
											}
											else{
												$href = base_url($cm->URL);
											}
										?>
										<li>
											<a href="<?php echo $href;?>" style="padding-left: 40px; padding-right: 10px; overflow: hidden; text-overflow: ellipsis;" title="<?php echo $cm->NAME;?>">
												<i class="<?php echo $cm->ICON_SET;?> <?php echo $cm->ICON;?> fa-fw"></i>
												<span><?php echo $cm->NAME;?></span>
											</a>
										</li>
									<?php endif;?>
								<?php endforeach;?>
							</ul>
						</li>
					<?php endif;?>
				<?php endif;?>
			<?php endforeach;?>
		</ul>
	</nav>
</aside>