<?php
	require_once "pageBase.php";
	require_once "../bin/web/repeater.php";
	require_once "../dal/messageDAL.php";
	require_once "../dal/message_typeDAL.php";
	require_once "../dal/re_messageDAL.php";
	require_once "../function/function.php";

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

		public function loadData() {
			$filterT = "a.`Visible`=1";
			$disabled = "";
			$style1 = "";
			$style2 = " style='display: none;'";

			if (!empty($this->id) && $this->id != 0) {
				$this->dal->ID = $this->id;
				$this->dal->loadRecord();
				
				$txtR = $this->dal->IsRead ? "已讀" : "未讀";
				$cTime = date("Y-m-d H:i:s", (int)$this->dal->CreatedTime);
				$mTime = date("Y-m-d H:i:s", (int)$this->dal->ModifiedTime);
				
				if ($this->dal->FromType == "1") {
					$filterT .= " AND a.`IsM`=1";
					$disabled = " disabled='disabled'";
					$txtF = "<a href='memberEdit.php?id={$this->dal->MemberID}' target='_blank'>{$this->dal->MemberName}</a>";
					$txtT = "管理者";
					/* 變更為已讀 */
					if ($this->dal->IsRead == "0") $this->dal->readRecord($this->id);

					parent::addParameter("editTitle", "檢視訊息");
				} else if ($this->dal->FromType == "2") {
					$filterT .= " AND a.`IsU`=1";
					$txtF = "管理者";
					if ($this->dal->MemberID == "0") {
						$txtT = $this->dal->LevelID ? "群發({$this->dal->LevelName})" : "群發";
					} else {
						$txtT = "<a href='memberEdit.php?id={$this->dal->MemberID}' target='_blank'>{$this->dal->MemberName}</a>";
					}

					parent::addParameter("editTitle", "修改訊息");
				} else {
					$this->alertMsg = "資料異常!請重新整理頁面再試一次!";
					$this->locationHref = "messageList.php";
					return;
				}

				parent::addParameter("TypeID", $this->dal->TypeID);
				parent::addParameter("MemberID", $this->dal->MemberID);
				parent::addParameter("txtR", $txtR);
				parent::addParameter("txtF", $txtF);
				parent::addParameter("txtT", $txtT);
				parent::addParameter("Title", $this->dal->Title);
				parent::addParameter("Content", $this->dal->Content);
				parent::addParameter("Visible", $this->dal->Visible);
				parent::addParameter("IsHot", $this->dal->IsHot);
				parent::addParameter("CTime", $cTime);
				parent::addParameter("MTime", $mTime);
			} else {
				$filterT .= " AND a.`IsU`=1";
				$txtF = "管理者";
				$style1 = " style='display: none;'";
				$style2 = "";
				parent::addParameter("TypeID", "0");
				parent::addParameter("MemberID", "");
				parent::addParameter("txtR", "未讀");
				parent::addParameter("txtF", $txtF);
				parent::addParameter("Visible", "1");
				parent::addParameter("IsHot", "0");
				parent::addParameter("editTitle", "新增訊息");
			}

			parent::addParameter("disabled", $disabled);
			parent::addParameter("style1", $style1);
			parent::addParameter("style2", $style2);

			/* 類型下拉 */
			$dataT = $this->dalT->executeRecordset([], $filterT, "a.`OrderNO`");
			$listT = new repeater();
			$listT->id = "listT";
			$listT->dataSource = $dataT;
			parent::addControl($listT);

			/* 訊息回覆 */
			$intoAry = [parent::addArray(":messageID", $this->id, "int")];
			$filterR = "a.`MessageID`=:messageID";
			$dataR = $this->dalR->executeRecordset($intoAry, $filterR);
			foreach ($dataR as &$item) {
				$item["CTime"] = date("Y-m-d H:i:s", (int)$item["CreatedTime"]);
				$item["txtV"] = $item["Visible"] ? "<span style='color:#090;'>&#10003;</span>" : "<span style='color:#f00;'>&#10005;</span>";
				$item["CName"] = ($item["FromType"] == "1") ? $item["CMName"] : $item["CUName"];
				$item["style"] = ($i % 2) ? " style='background-color: LightGray;'" : "";
		
				if ($item["Visible"] != "1") $item["Title"] = "<b style='color: red;'>[停用]</b>" . $item["Title"];
				if ($item["FromType"] == "1" && $item["IsRead"] == 0) $this->dalR->readRecord($item["ID"]);
			}
			$listR = new repeater();
			$listR->id = "listR";
			$listR->dataSource = $dataR;
			parent::addControl($listR);

			parent::addParameter("id", $this->id);
			parent::loadData();
		}

		public function saveData() {
			$type = parent::getPost("type");
			$saved = false;
			if ($type == "1") {
				if (!empty($this->id) && $this->id != 0) { //新增訊息
					/* 參數 */
					$this->dal->TypeID = parent::getPost("TypeID");
					$this->dal->FromType = 2;
					$this->dal->MemberID = parent::getPost("MemberID", "");
					$this->dal->LevelID = 0;
					$this->dal->Title = parent::getPost("Title");
					$this->dal->Content = parent::getPost("Content");
					$this->dal->Visible = parent::getPost("Visible", "0");
					$this->dal->IsHot = parent::getPost("IsHot", "0");
					$this->dal->CreatedIP = parent::getIP();
					$this->dal->CreatedUserID = parent::getCurrentUserID();
					$this->dal->ModifiedIP = parent::getIP();
					$this->dal->ModifiedUserID = parent::getCurrentUserID();
					
					/* 檢查 */
					if (!preg_match("/^\d+$/", $this->dal->MemberID)) {
						$this->alertMsg = "請選擇受文者!";
						$this->goBack = "Y";
						return;
					}
					if (empty($this->dal->Title)) {
						$this->alertMsg = "請輸入標題!";
						$this->goBack = "Y";
						return;
					}
					if (!preg_match("/^[1-9]\d*$/", $this->dal->TypeID)) {
						$this->alertMsg = "請選擇類型!";
						$this->goBack = "Y";
						return;
					}
					if (empty($this->dal->Content)) {
						$this->alertMsg = "請輸入內容!";
						$this->goBack = "Y";
						return;
					}
					$this->dal->Visible = ($this->dal->Visible === "0" || $this->dal->Visible === "1") ? $this->dal->Visible : "0";
					$this->dal->IsHot = ($this->dal->IsHot === "0" || $this->dal->IsHot === "1") ? $this->dal->IsHot : "0";

					/* 新增訊息 */
					$saved = $this->dal->insertRecord();
				} else { // 修改訊息
					$this->dal->ID = $this->id;
					$this->dal->loadRecord();
					/* 參數 & 檢查 */
					if ($this->dal->FromType == "2") { // 管理者發布的訊息
						$this->dal->TypeID = parent::getPost("TypeID");
						$this->dal->Title = parent::getPost("Title");
						$this->dal->Content = parent::getPost("Content");
						if (empty($this->dal->Title)) {
							$this->alertMsg = "請輸入標題!";
							$this->goBack = "Y";
							return;
						}
						if (!preg_match("/^[1-9]\d*$/", $this->dal->TypeID)) {
							$this->alertMsg = "請選擇類型!";
							$this->goBack = "Y";
							return;
						}
						if (empty($this->dal->Content)) {
							$this->alertMsg = "請輸入內容!";
							$this->goBack = "Y";
							return;
						}
					}

					$this->dal->Visible = ($this->dal->Visible === "0" || $this->dal->Visible === "1") ? $this->dal->Visible : "0";
					$this->dal->IsHot = ($this->dal->IsHot === "0" || $this->dal->IsHot === "1") ? $this->dal->IsHot : "0";

					/* 修改訊息 */
					$saved = $this->dal->updateRecord();
				}
			} else if ($type == "2") { // 新增回覆
				/* 參數 */
				$this->dalR->MessageID = $this->id;
				$this->dalR->FromType = 2;
				$this->dalR->Title = parent::getPost("ReTitle");
				$this->dalR->Content = parent::getPost("ReContent");
				$this->dalR->Visible = 1;
				$this->dalR->CreatedIP = parent::getIP();
				$this->dalR->CreatedUserID = parent::getCurrentUserID();
				$this->dalR->CreatedMemberID = 0;
				$this->dalR->ModifiedIP = parent::getIP();
				$this->dalR->ModifiedUserID = parent::getCurrentUserID();
				/* 檢查 */
				if (empty($this->id) || $this->id === "0") {
					$this->alertMsg = "新訊息不能回覆!";
					$this->goBack = "Y";
					return;
				}
				if (empty($this->dalR->Title)) {
					$this->alertMsg = "請輸入回覆標題!";
					$this->goBack = "Y";
					return;
				}
				if (empty($this->dalR->Content)) {
					$this->alertMsg = "請輸入回覆內容!";
					$this->goBack = "Y";
					return;
				}

				/* 新增回覆 */
				$saved = $this->dalR->insertRecord();
			}

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

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