<?php
	require_once "pageBase.php";
	require_once "../dal/message_typeDAL.php";
	require_once "../function/function.php";

	class _message_typeEdit extends pageBase {
		private string $id;
		private $dal;
		
		public function load() {
			$this->dal = new message_typeDAL();
			$this->id = parent::getInput("id","0");
			
			$this->limitID = "83";
			if (!parent::chkUserLimit()) {
				$this->alertMsg = "您沒有使用此頁面的權限!如有疑問請洽主管理者!";
				$this->locationHref = "index.php";
			} else {
				parent::isPostBack() ? $this->saveData() : $this->loadData();
			}
			parent::loadTemplateFile(dirname(__FILE__) . "/templates/message_typeEdit.html");
			parent::load();
		}

		public function loadData(){
			if (!empty($this->id) && $this->id != 0) {
				$this->dal->TypeID = $this->id;
				$this->dal->loadRecord();

				$parameters = [
					"TypeName" => $this->dal->TypeName,
					"Visible" => $this->dal->Visible,
					"OrderNO" => $this->dal->OrderNO,
					"IsM" => $this->dal->IsM,
					"IsU" => $this->dal->IsU,
					"editTitle" => "修改訊息類型",
				];
			} else {
				$data = $this->dal->executeRecordset([], "a.`Visible`=1", "a.`OrderNO` DESC", 0, 1);
				$orderNO = (count($data) == 0) ? 0 : ((int)$data[0]["OrderNO"] + 1);

				$parameters = [
					"Visible" => "1",
					"OrderNO" => $orderNO,
					"IsM" => "1",
					"IsU" => "1",
					"editTitle" => "新增訊息類型",
				];
			}
			foreach ($parameters as $name => $value) {
				parent::addParameter($name, $value);
			}

			parent::addParameter("id", $this->id);
			parent::loadData();
		}
		public function saveData() {
			if (!empty($this->id) && $this->id != 0) {
				$this->dal->TypeID = $this->id;
				$this->dal->loadRecord();
			}
			$this->dal->TypeName = parent::getPost("TypeName", "");
			$this->dal->Visible = parent::getPost("Visible", "0");
			$this->dal->OrderNO = parent::getPost("OrderNO", "");
			$this->dal->IsM = parent::getPost("IsM", "0");
			$this->dal->IsU = parent::getPost("IsU", "0");
			if ($this->dal->TypeName === "") {
				$this->alertMsg = "請輸入類型名稱!";
				$this->goBack = "Y";
				return;
			}

			$validVisible = in_array($this->dal->Visible, ["0", "1"]);
			$validOrderNO = ctype_digit($this->dal->OrderNO);
			$validIsM = in_array($this->dal->IsM, ["0", "1"]);
			$validIsU = in_array($this->dal->IsU, ["0", "1"]);
			if (!$validVisible) {
				$this->alertMsg = "請選擇啟用狀態!";
				$this->goBack = "Y";
				return;
			}
			if (!$validOrderNO) {
				$this->alertMsg = "排序欄位只能輸入數字!";
				$this->goBack = "Y";
				return;
			}
			if (!$validIsM) {
				$this->alertMsg = "請選擇會員是否可用!";
				$this->goBack = "Y";
				return;
			}
			if (!$validIsU) {
				$this->alertMsg = "請選擇管理員是否可用!";
				$this->goBack = "Y";
				return;
			}
			
			$saved = ($this->id !== "" && $this->id !== "0") ? $this->dal->updateRecord() : $this->dal->insertRecord();
			if ($saved === false) {
				$this->alertMsg = "儲存時發生錯誤!";
				$this->goBack = "Y";
			} else {
				$this->locationHref = "message_typeList.php";
			}
		}
	}

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