<?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 _userAdd extends pageBase {
		private string $pUrl;
		private $dal;
		
		public function load(){
			$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/userAdd.html");
			parent::load();
		}
		
		public function loadData() {
			// 功能列表
			$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);

			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,}$/";
			
			do {
				$Number1 = time();
				$Number = mt_rand(10000000, 99999999) . $Number1 . mt_rand(100000, 999999) . mt_rand(10000000, 99999999);
				$PathName = $this->getRandSTR($Number);
				$countF = $this->dal->countRecordset([], "a.`PathName`='" . $PathName . "'");
			} while ($countF > 0);

			$this->dal->PathName = $PathName;
			$this->dal->LoginName = parent::getPost("LoginName", "", 3);
			$this->dal->Password = parent::getPost("Password", "", 3);
			if (!preg_match($rePass, $this->dal->Password)) {
				$this->alertMsg = "密碼格式錯誤，請輸入至少8碼以上的英文加數字加符號!".$this->dal->Password;
				$this->goBack = "Y";
				return false;
			}
			$this->dal->UserName = parent::getPost("UserName");
			$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 = 1;
			$this->dal->UserType = 64;
			$this->dal->LineUID = parent::getPost("LineUID");
			$this->dal->Gmail = parent::getPost("Gmail");
			$this->dal->FbAccount = parent::getPost("FbAccount");
		
			$this->dal->LimitArray = array_map(fn($v) => ["MenuID" => $v], parent::getPostArray("MenuID"));
			$saved = $this->dal->insertRecord();

			if ($saved === false) {
				$this->alertMsg = "儲存時發生錯誤!";
				$this->goBack = "Y";
			} else {
				$this->locationHref = $this->pUrl;
			}
		}

		public function getRandSTR($num) {
			$num = str_pad($num, 32, "0", STR_PAD_LEFT);
			preg_match_all("/\d{2}/", $num, $aryN);
			$aryS = array_merge(range("0", "9"), range("A", "Z"), range("a", "z"));
			$countS = count($aryS);
			$str = '';
			foreach ($aryN[0] as $v) {
				$str .= $aryS[(int) $v % $countS];
			}
			return $str;
		}
	}

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