<?php
	require_once "pageBase.php";
	require_once "../bin/web/tables/sends.php";
	require_once "../dal/contactDAL.php";

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

		public function load() {
			$this->dal = new contactDAL();
			$this->id = parent::getInput("id","0");
			$this->pUrl = urldecode(parent::getInput("pUrl","contactList.php"));
			
			$this->limitID = "26";
			if (!parent::chkUserLimit()) {
				$this->alertMsg = "您沒有使用此頁面的權限!如有疑問請洽主管理者!";
				$this->locationHref = "index.php";
			} else {
				parent::isPostBack() ? $this->saveData() : $this->loadData();
			}
			parent::loadTemplateFile(dirname(__FILE__) . "/templates/contactView.html");
			parent::load();
		}
		
		public function loadData(){
			$delTxt = "";
			if ($this->id != "" && $this->id != 0) {
				$this->dal->ContactID = $this->id;
				$this->dal->loadRecord();

				if (parent::getCurrentUserID() > "1" && $this->dal->Disable == "1") {
					$this->alertMsg = "聯絡訊息已經被刪除了!如有疑問請洽主管理者!";
					$this->locationHref = "contactList.php";
				} else {
					$this->addParametersFromContact($this->dal);
					$delTxt = $this->getDeleteText($this->dal);
				}
			} else {
				parent::redirect($this->pUrl);
			}

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

		public function saveData() {
			$this->dal->ContactID = $this->id;
			$this->dal->loadRecord();
			
			$reMail = parent::getPost("ReMail", "");
			$reTitle = parent::getPost("ReTitle", "");
			$reContent = stripslashes(parent::getPost("ReContent", "", 3));
		
			$webName = parent::getRegistryValue("webName");
			$webEmail = parent::getRegistryValue("webEmail");
			$sendMailType = parent::getRegistryValue("sendMailType");

			$sends = new sends();
			$mailflag = 0;
			if (empty($sendMailType)) {
				if (!$sends->sendemail($webName, $webEmail, $reTitle, $reContent, $reMail)) $mailflag = 1;
			}else{
				$toAry = [$reMail];
				if (!$sends->sendsmtpmail($reTitle, $reContent, $toAry)) $mailflag = 1;
			}

			if ($mailflag === 0) {
				$this->updateContactRecord($reMail, $reTitle, $reContent);
				$this->alertMsg = "郵件已經發送成功!";
				$this->locationHref = $this->pUrl;
			} else {
				$this->alertMsg = "發送郵件時出現錯誤!";
				$this->goBack = "Y";
			}
		}

		/* 聯絡我們相關 */
		private function addParametersFromContact($contact) {
			$parameters = [
				"Name" => $contact->Name,
				"Phone" => $contact->Phone,
				"Email" => $contact->Email,
				"ProductID" => $contact->ProductID,
				"ProductName" => $contact->ProductName,
				"Mobile" => $contact->Mobile,
				"Company" => $contact->Company,
				"Address" => $contact->Address,
				"Url" => $contact->Url,
				"Title" => $contact->Title,
				"Content" => $contact->Content,
				"ReMail" => ($contact->ReMail != "") ? $contact->ReMail : $contact->Email,
				"ReTitle" => $contact->ReTitle,
				"ReContent" => $contact->ReContent,
				"IsReply" => ($contact->IsReply == "1") ? "<span class=\"badge badge_soft_success\">回覆</span>" : "<span class=\"badge badge_soft_danger\">未回</span>",
				"CreatedIP" => $contact->CreatedIP,
				"CreatedTime" => date("Y-m-d H:i:s", $contact->CreatedTime)
			];
			foreach ($parameters as $name => $value) {
				parent::addParameter($name, $value);
			}
		
			if ($contact->IsView == 0) $contact->viewRecord(parent::getCurrentUserID());
		}

		/* 獲取刪除文本 */
		private function getDeleteText($contact) {
			return $contact->Disable == "1" ? "<p class=\"mt_3 h4 txt_danger card_body\">注意:聯絡訊息已經被刪除。</p>" : "";
		}

		/* 變更回覆狀態 */
		private function updateContactRecord($reMail, $reTitle, $reContent) {
			$this->dal->ReMail = $reMail;
			$this->dal->ReTitle = $reTitle;
			$this->dal->ReContent = $reContent;
			$this->dal->IsReply = "1";
			$this->dal->ModifiedUserID = parent::getCurrentUserID();
			$this->dal->updateRecord();
		}
	}

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