<?php
	require_once "pageBase.php";
	require_once "../bin/system/conver.php";
	require_once "../bin/web/repeater.php";
	require_once "../dal/menuDAL.php";
	require_once "../dal/userDAL.php";

	class _userView extends pageBase {
		private string $id;
		private string $pUrl;
		private $dal;

		public function load() {
			$this->id = parent::getInput("id", "0");
			$this->pUrl = urldecode(parent::getInput("pUrl", "userList.php"));
			$this->dal = new userDAL();

			$this->limitID = "21";
			if (!parent::chkUserLimit()) {
				$this->alertMsg = "您沒有使用此頁面的權限!如有疑問請洽主管理者!";
				$this->locationHref = "index.php";
			} else {
				parent::isPostBack() ? $this->saveData() : $this->loadData();
			}
			parent::loadTemplateFile(dirname(__FILE__) . "/templates/userView.html");
			parent::load();
		}

		public function loadData() {
			if ($this->id !== "" && $this->id !== "0" && $this->id !== "1") {
				$this->dal->UserID = $this->id;
				$this->dal->loadRecord();
				$this->addUserDataParameters();
				$this->addLineBoxParameter();
				$this->addLineButtonParameter();
				$this->addDisplayLIDParameter();
				$this->addFunctionListParameters();
				
				//
				$listL = new repeater();
				$listL->id = "listL";
				$listL->dataSource = $this->dal->LimitArray;
				parent::addControl($listL);
			}else{
				parent::redirect($this->pUrl);
			}
			
			parent::addParameter("id", $this->id);
			parent::addParameter("pUrl", $this->pUrl);
			
			parent::loadData();
		}
		public function saveData(){
			$reMail = "/^[A-Za-z0-9\-_\.]+@[A-Za-z0-9\-_]+(\.[A-Za-z0-9\-_]+)+$/";
			$rePass = "/^(?=.*[A-Za-z])(?=.*\d)(?=.*[$@$!%*#?&])[A-Za-z\d$@$!%*#?&]{8,}$/";

			if ($this->id !== "" && $this->id !== "0") {
				$this->dal->UserID = $this->id;
				$this->dal->loadRecord();
				$this->dal->Password = parent::getPost("Password", "", 3);
				if (!preg_match($rePass, $this->dal->Password)) {
					$this->alertMsg = "密碼格式錯誤，請輸入至少8碼以上的英文加數字加符號!注意:符號僅限【\$、@、!、%、*、#、?、&】";
					$this->goBack = "Y";
					return false;
				}
				$this->dal->UserName = parent::getPost("UserName", "", 3);
				$this->dal->Email = parent::getPost("Email");
				if ($this->dal->Email !== "" && !preg_match($reMail, $this->dal->Email)) {
					$this->alertMsg = "電子信箱格式錯誤!";
					$this->goBack = "Y";
					return false;
				}
				$this->dal->Visible = parent::getPost("Visible", "0");
				$this->dal->Gmail = parent::getPost("Gmail");
				$this->dal->FbAccount = parent::getPost("FbAccount");
				if (parent::getCurrentUserID()=== 1) {
					$lineUID = parent::getPost("LineUID");
					if ($this->dal->LineUID !== $lineUID) {
						$this->dal->LineUID = $lineUID;
						$this->dal->LineName = "";
						$this->dal->LinePicture = "";
					}
				}
				$menuID = parent::getPostArray("MenuID");
				$this->dal->LimitArray = [];
				foreach ($menuID as $v) {
					$this->dal->LimitArray[] = ["MenuID" => $v];
				}

				if (!$this->dal->updateRecord()) {
					$this->alertMsg = "儲存時發生錯誤!";
					$this->goBack = "Y";
				} else {
					$this->locationHref = $this->pUrl;
				}
			} else {
				$this->alertMsg = "缺少重要參數!";
				$this->locationHref = $this->pUrl;
			}
		}

		// 添加使用者資料的參數
		private function addUserDataParameters() {
			parent::addParameter("LoginName", $this->dal->LoginName);
			parent::addParameter("Password", $this->dal->Password);
			parent::addParameter("UserName", $this->dal->UserName);
			parent::addParameter("Email", $this->dal->Email);
			parent::addParameter("Visible", $this->dal->Visible);
			parent::addParameter("LineUID", $this->dal->LineUID);
			parent::addParameter("LineName", $this->dal->LineName);
			parent::addParameter("LinePicture", $this->dal->LinePicture);
			parent::addParameter("Gmail", $this->dal->Gmail);
			parent::addParameter("FbAccount", $this->dal->FbAccount);
		}

		// 添加 Line 相關的參數
		private function addLineBoxParameter() {
			$line_box = "";

			if ($this->dal->LineName !== "" && $this->dal->LinePicture !== "") {
				$line_box .= <<<HTML
				<div class="d_flex form_list">
					<label>Line帳號</label>
					<p class="d_flex upBox">
						<a class="img_box" href="javascript:void(0);" style="background-image: url({$this->dal->LinePicture});"><img src="images/upImgBg.png"></a>
						<font class="col_12 d_flex f_aitems_center">{$this->dal->LineName}</font>
					</p>
				</div>
			HTML;
			}
			parent::addParameter("line_box", $line_box);
		}
		
		// 添加 Line 按鈕相關的參數
		private function addLineButtonParameter() {
			$line_btn = ($this->dal->LineUID !== "") ? "<a class=\"btn btn_danger\" href=\"javascript:void(0);\" data-action=\"delItem\">移除Line帳號</a>" : "<a class=\"btn btn_green\" href=\"myLineJoinSend.php\">立即前往綁定</a>";
			parent::addParameter("line_btn", $line_btn);
		}

		// 添加顯示 LID 參數
		private function addDisplayLIDParameter() {
			$displayLID = (parent::getCurrentUserID() === 1) ? "" : " style=\"display: none;\"";
			parent::addParameter("displayLID", $displayLID);
		}
		
		// 添加功能列表相關的參數
		private function addFunctionListParameters() {
			$dalM = new menuDAL();
			$aryJ = [];
			$dataM1 = $dalM->executeRecordset([], "a.`ParentID`='0' AND a.`Visible`='1' AND a.`IsOA`=0", "a.`OrderNO`");
			foreach ($dataM1 as $rows) {
				$menuID = $rows["MenuID"];
				$menuName = urlencode($rows["MenuName"]);

				$aryCL = [];
				$intoAry = [ parent::addArray(":parentID", $menuID, "int") ];
				$dataM2 = $dalM->executeRecordset($intoAry, "a.`ParentID`=:parentID AND a.`Visible`=1 AND a.`IsOA`=0", "a.`OrderNO`");
				foreach ($dataM2 as $rows2) {
					$aryCL[] = ["MenuID" => $rows2["MenuID"], "MenuName" => urlencode($rows2["MenuName"])];
				}

				$aryJ[] = ["MenuID" => $menuID, "MenuName" => $menuName, "aryCL" => $aryCL];
			}
			$txtJ = urldecode(json_encode($aryJ));
			parent::addParameter("txtJ", $txtJ);
		}
	}

	$page = new _userView();
	$page->load();
	$page->render();
?>