<?
$returnary     = array();
//$memu_sql = "select * from program where pg_show=1 and pg_PgID in (select Pgid from rights where Esno='".$_SESSION["SYSTEM"]["userid"]."' and Length(Pgid)=3 ) order by pg_PgID asc";
$memu_sql = "select * from program where pg_show=1 and pg_PgID in (select Pgid from rights where Esno='5' and Length(Pgid)=3 ) order by pg_PgID asc";
$memu_rs = select_db($memu_sql);
$memu_rs_num = select_db_num($memu_sql);

if($memu_rs_num>0){
	$mi = 0;
	$mj = 0;
	$mk = 0;
	foreach ($memu_rs as $row){
		$returnary[$mi] = $row;
		//$memu_sql2 = "select * from program where pg_show=1 and pg_PgID in (select Pgid from rights where Esno='".$_SESSION["SYSTEM"]["userid"]."' and Pgid like '".$row["pg_PgID"]."%' and Length(Pgid)=5 ) order by pg_PgID asc";
		$memu_sql2 = "select * from program where pg_show=1 and pg_PgID in (select Pgid from rights where Esno='5' and Pgid like '".$row["pg_PgID"]."%' and Length(Pgid)=5 ) order by pg_PgID asc";
		$memu_rs2 = select_db($memu_sql2);
		$memu_rs2_num = select_db_num($memu_sql2);
		if($memu_rs2_num>0){
			$returnary[$mi]["hassub"] = true;
			$mj = 0;
			foreach ($memu_rs2 as $row2){
				$returnary[$mi]["2"][$mj]  = $row2;
				//$memu_sql3 = "select * from program where pg_show=1 and pg_PgID in (select Pgid from rights where Esno='".$_SESSION["SYSTEM"]["userid"]."' and Pgid like '".$row2["pg_PgID"]."%' and Length(Pgid)=7 ) order by pg_PgID asc";
				$memu_sql3 = "select * from program where pg_show=1 and pg_PgID in (select Pgid from rights where Esno='5' and Pgid like '".$row2["pg_PgID"]."%' and Length(Pgid)=7 ) order by pg_PgID asc";
				$memu_rs3 = select_db($memu_sql3);
				$memu_rs3_num = select_db_num($memu_sql3);
				if($memu_rs3_num>0){
					$returnary[$mi]["2"][$mj]["hassub"] = true;
					$mk = 0;
					foreach ($memu_rs3 as $row3){
						$returnary[$mi]["2"][$mj]["3"][$mk]  = $row3;
						$mk++;
					}
				}else{
					$returnary[$mi]["2"][$mj]["hassub"] = false;
					$returnary[$mi]["2"][$mj]["3"]  = null;
				}
				$mj++;
			}
		}else{
			$returnary[$mi]["hassub"]  = false;
			$returnary[$mi]["2"]       = null;
		}
		$mi++;
	}
}

$menu = $returnary;
?>
<div class="col-xs-6 col-sm-2 sidebar-offcanvas" id="sidebar" role="navigation">
	<aside>
		<div id="sub">
			<ul>
				<!-- li>
					<a href="Main.asp"><img src="<?=base_url('/img/IIE/tw_img/other/menu_01.png')?>" />即時訊息</a>
				</li-->
				<? foreach ($menu as $row){ ?>
					<li class="<?=($row["hassub"]?'has-sub':'')?> <?=(strpos(' '.$pgid,$row["pg_PgID"])>0?'open':'')?>" ><a href=" <?=($row["hassub"]?'#':base_url('/root/'.$row["pg_PrgFolder"]))?> "><?=$row["pg_Pname"]?></a>
					<? if(!empty($row["2"])){ ?>
						<ul <?=(strpos(' '.$pgid,$row["pg_PgID"])>0?'style="display: block;"':'')?>" >
						<? foreach ($row["2"] as $row2){ ?>
							<li class="<?=($row2["hassub"]?'has-sub':'')?>" >
								<a class="<?=($row2["hassub"]?'drop_link':'')?>"  href=" <?=($row2["hassub"]?'#':base_url('/root/'.$row2["pg_PrgFolder"]))?> "><?=$row2["pg_Pname"]?></a>
								<? if(!empty($row2["3"])){ ?>
            						<ul <?=(strpos(' '.$pgid,$row2["pg_PgID"])>0?'style="display: block;"':'')?>" >
            							<? foreach ($row2["3"] as $row3){ ?>
            							<li ><a href="/root/<?=$row3["pg_PrgFolder"]?>"><?=$row3["pg_Pname"]?></a></li>
            							<? } ?>
            						</ul>
            					<? } ?>
							</li>
						<? } ?>
						</ul>
					<? } ?>
					</li>
				<? }?>
				<li><a href="/root/logout.php">登出</a></li>
			</ul>
		</div>
	</aside> 
</div>