<?php
	require_once "pageBase.php";
	require_once dirname(__FILE__)."/../bin/web/page.php";
	require_once dirname(__FILE__)."/../bin/web/repeater.php";
	require_once dirname(__FILE__)."/../dal/menuDAL.php";
	require_once dirname(__FILE__)."/../dal/limitDAL.php";

	class _sidePage extends page {
		public $userID, $limitID;
	
		public function __construct() {
			parent::__construct();
		}

		//覆蓋父類load()方法
		public function load() {
			parent::loadTemplateFile(dirname(__FILE__) . "/templates/sidePage.html");

			$dalM = new menuDAL();
			$dalL = new limitDAL();
			$aryJ = [];
			
			$filter = "a.`ParentID` = 0 AND a.`Visible` = 1";
			if ($this->userID != "1") $filter .= " AND a.`IsOA` = 0";
			$dataM = $dalM->executeRecordset([], $filter, "a.`OrderNO`");
			foreach ($dataM as $rows) {
				$menuID = $rows["MenuID"];
				$menuName = urlencode($rows["MenuName"]);
				$menuIcon = urlencode($rows["MenuIcon"]);
				$menuClass = "";

				$aryCL = [];
				$intoAry = [ parent::addArray(":parentID", $menuID, "int") ];
				$filter = "a.`ParentID` = :parentID AND a.`Visible` = 1";
				if ($this->userID != "1") $filter .= " AND a.`IsOA` = 0";
				$dataM2 = $dalM->executeRecordset($intoAry, $filter, "a.`OrderNO`");
				
				foreach($dataM2 as $rows2){
					$minClass = "";
					if ($this->limitID == $rows2["MenuID"]) {
						$menuClass = "action";
						$minClass = "chack";
					}
					$checkID = $rows2["MenuID"];
					$checkName = urlencode($rows2["MenuName"]);
					$checkLink = urlencode($rows2["MenuLink"]);
					$linkTarget = urlencode($rows2["LinkTarget"]);
                
					if ($dalL->checkLimit($this->userID, $checkID)) {
						$aryCL[] = [
							"MenuID" => $checkID,
							"MenuName" => $checkName,
							"MenuLink" => $checkLink,
							"LinkTarget" => $linkTarget,
							"minClass" => $minClass
						];
					}
				}
				$show = (count($aryCL) > 0) ? "fa fa-angle-down" : "";

				if ($dalL->checkLimit($this->userID, $menuID)) {
					$aryJ[] = [
						"MenuID" => $menuID,
						"MenuName" => $menuName,
						"MenuIcon" => $menuIcon,
						"menuClass" => $menuClass,
						"show" => $show,
						"childList" => $aryCL
					];
				}
			}
			$txtJ = urldecode(json_encode($aryJ));
			parent::addParameter("txtLJ", $txtJ);
			
			parent::load();
		}
	}
?>