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

	class _article_categoryEdit extends pageBase {
		private string $id;
		private $dal;

		public function load() {
			$this->dal = new article_categoryDAL();
			$this->id = parent::getInput("id","0");

			$this->limitID = "30";
			if (!parent::chkUserLimit()) {
				$this->alertMsg = "您沒有使用此頁面的權限!如有疑問請洽主管理者!";
				$this->locationHref = "index.php";
			} else {
				parent::isPostBack() ? $this->saveData() : $this->loadData();
			}
			parent::loadTemplateFile(dirname(__FILE__) . "/templates/article_categoryEdit.html");
			parent::load();
		}

		public function loadData() {
			$delTxt = "";
			if (!empty($this->id) && $this->id != 0) {
				$this->dal->CategoryID = $this->id;
				$this->dal->loadRecord();
				if (parent::getCurrentUserID() > "1" && $this->dal->Disable == "1") {
					$this->alertMsg = "分類已經被刪除了!如有疑問請洽主管理者!";
					$this->locationHref = "article_categoryList.php";
				} else {
					$this->addParametersFromCategory($this->dal);
					
					$bestRating = $this->dal->BestRating;
					$ratingValue = $this->dal->RatingValue;
					$ratingCount = $this->dal->RatingCount;
					$reviewCount = $this->dal->ReviewCount;
					$isShowJason = $this->dal->IsShowJason;
					$isShowStars = $this->dal->IsShowStars;
					$BannerImage = $this->dal->BannerImage;
					$BannerTitle = $this->dal->BannerTitle;
					$BannerLink = $this->dal->BannerLink;
					$BannerColor = $this->dal->BannerColor;
					$BannerDesc = $this->dal->BannerDesc;
					$OGTitle = $this->dal->OGTitle;
					$OGDescription = $this->dal->OGDescription;
					$OGImage = $this->dal->OGImage;
					$WebTitle = $this->dal->WebTitle;
					$WebKeyword = $this->dal->WebKeyword;
					$WebDescription = $this->dal->WebDescription;
					$editTitle = "修改";
					
					$this->getDeleteText($this->dal);
				}
			} else {
				$dataO = $this->dal->executeRecordset([], "", "a.`OrderNO` DESC", 0, 1);
				$orderNO = (count($dataO) == 0) ? 10 : ((int)$dataO[0]["OrderNO"] + 10);
				$defaultParameters = [
					"IsIndex" => "1",
					"Visible" => "1",
					"OrderNO" => $orderNO
				];
				foreach ($defaultParameters as $paramName => $paramValue) {
					parent::addParameter($paramName, $paramValue);
				}
				
				$ratingValue = rand(40, 49) / 10;
				$reviewCount = rand(10, 99);
				$ratingCount = round($ratingValue * $reviewCount);
				$editTitle = "新增";
			}
			
			$aryT = array();
			$aryH = array();
			//Banner設定
			$aryT[] = ["id" => "banset", "name" => "Banner設定"];
			$aryH[] = ["html_box" => parent::BannerSetHtml(($BannerTitle ?? ''), ($BannerLink ?? ''), ($BannerColor ?? ''), ($BannerImage ?? ''), ($BannerDesc ?? ''), "Article")];
			//社群分享設定
			$aryT[] = ["id" =>"ogset", "name" => "社群分享設定"];
			$aryH[] = ["html_box" => parent::MetaogSetHtml(($OGTitle ?? ''), ($OGImage ?? ''), ($OGDescription ?? ''), "Article")];
			//結構化資料
			if(parent::getCurrentUserID() == 1){
				$aryT[] = ["id" => "boxgseo", "name" => "結構化資料設定"];
				$aryH[] = ["html_box" => parent::GoogleSearchHtml(($bestRating ?? 5), $ratingValue, $reviewCount, $ratingCount, ($isShowJason ?? 0), ($isShowStars ?? 0))];
			}
			//SEO設定
			$aryT[] = ["id" => "seoset", "name" => "SEO設定"];
			$aryH[] = ["html_box" => parent::SeoSetHtml(($WebTitle ?? ''), ($WebKeyword ?? ''), ($WebDescription ?? ''))];
			
			$listT = new repeater();
			$listT->id = "listT";
			$listT->dataSource = $aryT;
			parent::addControl($listT);
			$listH = new repeater();
			$listH->id = "listH";
			$listH->dataSource = $aryH;
			parent::addControl($listH);
			
			parent::addParameter("id", $this->id);
			parent::addParameter("editTitle", $editTitle);
			parent::addParameter("delTxt", $delTxt);
			parent::loadData();
		}

		public function saveData() {
			if (!empty($this->id) && $this->id != 0) {
				$this->dal->CategoryID = $this->id;
				$this->dal->loadRecord();
			} else {
				$this->dal->CreatedUserID = parent::getCurrentUserID();
				$this->dal->CreatedIP = parent::getIP();
			}

			$this->dal->CategoryName = parent::getPost("CategoryName", "");
			if ($this->dal->CategoryName === "") {
				$this->alertMsg = "請輸入分類名稱!";
				$this->goBack = "Y";
				return false;
			}
			$twPathName = parent::getPost("TwPathName", $this->dal->CategoryName);
			$twPathName = parent::generateTwPathName($twPathName);
			if ($this->dal->checkTwPathNameExists($twPathName) && $this->dal->TwPathName !== $twPathName) {
				$this->alertMsg = "中文路徑名稱「" . $twPathName . "」已經存在!";
				$this->goBack = "Y";
				return false;
			}
			$this->dal->TwPathName = $twPathName;
			$this->dal->Description = parent::getPost("Description", "", 2);
			$this->dal->BannerImage = parent::getPost("BannerImage");
			$this->dal->BannerTitle = parent::getPost("BannerTitle");
			$this->dal->BannerLink = parent::getPost("BannerLink");
			$this->dal->BannerColor = parent::getPost("BannerColor");
			$this->dal->BannerDesc = parent::getPost("BannerDesc");
			$this->dal->OGTitle = parent::getPost("OGTitle");
			$this->dal->OGDescription = preg_replace("/\s+/", " ", parent::getPost("OGDescription"));
			$this->dal->OGImage = parent::getPost("OGImage");
			$this->dal->IsIndex = parent::getPost("IsIndex", "0");
			$this->dal->Visible = parent::getPost("Visible", "0");
			$this->dal->OrderNO = parent::getPost("OrderNO", "0");
			
			$this->dal->WebTitle = preg_replace("/\s+/", " ", parent::getPost("WebTitle"));
			if ($this->dal->WebTitle === "") $this->dal->WebTitle = $this->dal->CategoryName;
			$this->dal->WebKeyword = preg_replace("/\s+/", " ", parent::getPost("WebKeyword"));
			if ($this->dal->WebKeyword === "") $this->dal->WebKeyword = $this->dal->CategoryName;
			$this->dal->WebDescription = preg_replace("/\s+/", " ", parent::getPost("WebDescription"));
			if ($this->dal->WebDescription === "") $this->dal->WebDescription = $this->dal->CategoryName;
			
			$this->dal->ModifiedUserID = parent::getCurrentUserID();
			$this->dal->ModifiedIP = parent::getIP();
    
			$this->dal->BestRating = parent::getPost("BestRating", "5");
			$this->dal->RatingValue = parent::getPost("RatingValue", "0");
			$this->dal->RatingCount = parent::getPost("RatingCount", "0");
			$this->dal->ReviewCount = parent::getPost("ReviewCount", "0");
			$this->dal->IsShowJason = parent::getPost("IsShowJason", "0");
			$this->dal->IsShowStars = parent::getPost("IsShowStars", "0");
			
			$saved = ($this->id !== "" && $this->id !== "0") ? $this->dal->updateRecord() : $this->dal->insertRecord();
			if ($saved === false) {
				$this->alertMsg = "儲存時發生錯誤!";
				$this->goBack = "Y";
			} else {
				$this->locationHref = "article_categoryList.php";
			}
		}

		/* 取得分類資訊 */
		private function addParametersFromCategory($category) {
			$parameters = [
				"CategoryName" => $category->CategoryName,
				"TwPathName" =>  $category->TwPathName,
				"Description" =>  $category->Description,
				"IsIndex" => $category->IsIndex,
				"Visible" => $category->Visible,
				"OrderNO" => $category->OrderNO
			];
			foreach ($parameters as $name => $value) {
				parent::addParameter($name, $value);
			}
		}

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

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