<?php
	require_once "pageBase.php";
	require_once "../bin/system/conver.php";
	require_once "../bin/system/guid.php";
	require_once "../bin/web/repeater.php";
	require_once "../dal/customDAL.php";
	require_once "../function/function.php";

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

		public function loadData() {
			$delTxt = "";
			if (!empty($this->id) && $this->id != 0) {
				$this->dal->CustomID = $this->id;
				$this->dal->loadRecord();
				if (parent::getCurrentUserID() > "1" && $this->dal->Disable == "1") {
					$this->alertMsg = "自訂欄位已經被刪除了!如有疑問請洽主管理者!";
					$this->locationHref = "customList.php";
				} else {
					$parameters = [
						"CustomTitle" => $this->dal->CustomTitle,
						"CustomImage" => $this->dal->CustomImage,
						"CustomLink" => $this->dal->CustomLink,
						"CustomContent" => $this->dal->CustomContent,
						"Visible" => $this->dal->Visible,
						"OrderNO" => $this->dal->OrderNO,
						"CreatedTime" => date("Y-m-d H:i:s", $this->dal->CreatedTime),
						"ModifiedTime" => date("Y-m-d H:i:s", $this->dal->ModifiedTime),
						"CLName" => $this->dal->CLName,
						"CUName" => $this->dal->CUName,
						"MLName" => $this->dal->MLName,
						"MUName" => $this->dal->MUName,
						"editTitle" =>"修改自訂欄位內容",
					];
					
					$delTxt = $this->getDeleteText($this->dal);
				}
			} else {
				$parameters = [
					"Visible" => "1",
					"editTitle" => "新增自訂欄位",
				];
			}
			foreach ($parameters as $name => $value) {
				parent::addParameter($name, $value);
			}
			
			parent::addParameter("id", $this->id);
			parent::addParameter("delTxt", $delTxt);
			parent::loadData();
		}
		
		public function saveData() {
			if (!empty($this->id) && $this->id != 0) {
				$this->dal->CustomID = $this->id;
				$this->dal->loadRecord();
			} else {
				$this->dal->CreatedUserID = parent::getCurrentUserID();
				$this->dal->CreatedIP = parent::getIP();
			}
			
			$this->dal->CustomTitle = parent::getPost("CustomTitle", "");
			$this->dal->CustomImage = parent::getPost("CustomImage", "");
			$this->dal->CustomLink = parent::getPost("CustomLink", "");
			$this->dal->CustomContent = parent::getPost("CustomContent", "");
			$this->dal->Visible = parent::getPost("Visible", "0");
			if (!in_array($this->dal->Visible, ["0", "1"], true)) {
				$this->alertMsg = "請選擇啟用狀態!";
				$this->goBack = "Y";
				return false;
			}
			$this->dal->OrderNO = parent::getPost("OrderNO", "0");

			$this->dal->ModifiedUserID = parent::getCurrentUserID();
			$this->dal->ModifiedIP = parent::getIP();
			
			$saved = ($this->id !== "" && $this->id !== "0") ? $this->dal->updateRecord() : $this->dal->insertRecord();
			if ($saved === false) {
				$this->alertMsg = "儲存時發生錯誤!";
				$this->goBack = "Y";
			} else {
				$this->locationHref = "customList.php";
			}
		}

		/* 獲取刪除文本 */
		private function getDeleteText($custom) {
			return $custom->Disable == "1" ? "<p class=\"mt_3 h4 txt_danger card_body\">注意:自訂欄位已經被刪除，不會顯示在前台列表頁。</p>" : "";
		}
	}

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