<?php
	require_once "pageBase.php";
	require_once "bin/web/repeater.php";

	class _newsdetail extends pageBase {
		public $news;
	
		public function load() {
			$this->news = new news();
			$this->loadData();
			parent::loadTemplateFile("templates/{$this->registry->webTemplates}/news-detail.html");
			parent::load();
		}
		
		public function loadData() {
			// 橫幅廣告
			$this->picturetype = "60";

			// 宣告
			$dal = new newsDAL();
			$dalC = new news_categoryDAL();
			switch ($this->registry->webLinkType) {
				case "2":
					$dal->TwPathName = parent::getGet("path", "", 2);
					break;
				case "3":
					$dal->PathName = parent::getGet("path", "", 2);
					break;
				default:
					$dal->NewsID = parent::getGet("id");
			}
			$dal->loadRecord();
			if ($dal->Visible != "1" || $dal->CategoryVisible != "1" || (int)$dal->OnlineTime > time()) {
				$this->alertMsg = "找不到指定的網頁!";
				$this->locationHref = "/";
				return false;
			}

			if ($this->registry->isAnalyze != 0) $dal->click();

			$dalC->CategoryID = $dal->CategoryID;
			$dalC->loadRecord();

			// 內頁內容陣列
			$parameter = [
				"Title" => $dal->Title,
				"Description" => $dal->Description,
				"Image" => (!empty($dal->Image) && parent::image_exists($dal->Image)) ? $dal->Image : $this->NoPicImage,
				"imageStyle" => "",
				"QuoteStyle" => ($dal->QuoteText == "" && $dal->QuoteLink == "") ? " style='display:none;'" : "",
				"QuoteText" => ($dal->QuoteText == "") ? $dal->QuoteLink : $dal->QuoteText,
				"QuoteLink" => ($dal->QuoteLink == "") ? "javascript:void(0);" : $dal->QuoteLink,
				"Introduction" => str_replace("\r\n", "<br />", $dal->Introduction),
				"OnlineTime" => date("Y-m-d H:i:s", $dal->OnlineTime),
				"OnlineDate" => date("Y-m-d", $dal->OnlineTime),
				"OnlineY" => date("Y", $dal->OnlineTime),
				"OnlineM" => date("m", $dal->OnlineTime),
				"OnlineD" => date("d", $dal->OnlineTime),
				"OnlineM2" => date("M", $dal->OnlineTime),
				"link" => $this->news->linkNewsDetail($dal->NewsID),
				"NewsID" => $dal->NewsID,
				"CategoryID" => $dal->CategoryID,
				"CategoryName" => $dal->CategoryName,
				"linkC" => $this->news->linkNewsList($dal->CategoryID),
			];

			// 資料內容檢查
			if ($parameter["Image"] == $this->NoPicImage) $parameter["imageStyle"] = " style='display:none;'";
			
			if (!empty($dal->BannerImage) && parent::image_exists($dal->BannerImage)) {
				parent::setPageBanner($dal->BannerImage, $dal->BannerTitle, $dal->BannerLink, $dal->BannerColor, $dal->BannerDesc);
			} else if (!empty($dalC->BannerImage) && parent::image_exists($dalC->BannerImage)) {
				parent::setPageBanner($dalC->BannerImage, $dalC->BannerTitle, $dalC->BannerLink, $dalC->BannerColor, $dalC->BannerDesc);
			}

			if (!empty($dal->OGTitle)) $parameter["ogTitle"] = $dal->OGTitle;
			if (!empty($dal->OGDescription)) $parameter["ogDescription"] = $dal->OGDescription;
			if (!empty($dal->OGImage)) $parameter["ogImage"] = parent::setPageOGImage($dal->OGImage);

			if (!empty($dal->WebTitle)) $parameter["webTitle"] = $dal->WebTitle;
			if (!empty($dal->WebKeyword)) $parameter["webKeyword"] = $dal->WebKeyword;
			if (!empty($dal->WebDescription)) $parameter["webDescription"] = $dal->WebDescription;

			foreach ($parameter as $name => $value) {
				parent::addParameter($name, $value);
			}
			
			// 最新消息分類列表
			$dataC = $dalC->executeRecordset([], "a.`Visible`=1 AND a.`Disable`=0");
			$dataC = $this->formatCategory($dataC, $dal->CategoryID);
			$listC = new repeater();
			$listC->id = "listC";
			$listC->dataSource = $dataC;
			parent::addControl($listC);

			// 同分類最新消息列表
			$intoAry = [
				parent::addArray(":categoryID", $dal->CategoryID, "int"),
				parent::addArray(":onlineTime", time(), "int"),
			];
			$dataN = $dal->executeRecordset($intoAry, "a.`CategoryID`=:categoryID AND a.`Visible`=1 AND a.`Disable`=0 AND a.`OnlineTime`<:onlineTime", "a.`OrderNO`,a.`OnlineTime` DESC,a.`NewsID` DESC");

			// 上下頁
			$key = array_search($dal->NewsID, array_column($dataN, "NewsID")); // 取得上下筆的key值
			$up = ($key - 1);
			$down = ($key + 1);
			$pageParameter = [
				"pageUpTitle" => "-",
				"pageUpLink" => "javascript:void(0);",
				"pageUpImage" => $this->NoPicImage,
				"pageUpStyle" => " style='display:none;'",
				"pageDownTitle" => "-",
				"pageDownLink" => "javascript:void(0);",
				"pageDownImage" => $this->NoPicImage,
				"pageDownStyle" => " style='display:none;'",
			];
			if ($up >= 0) {
				$pageParameter["pageUpTitle"] = $dataN[$up]["Title"];
				$pageParameter["pageUpLink"] = $this->news->linkNewsDetail($dataN[$up]["NewsID"]);
				if (!empty($dataN[$up]["Image"]) && parent::image_exists($dataN[$up]["Image"])) $pageParameter["pageUpImage"] = $dataN[$up]["Image"];
				$pageParameter["pageUpStyle"] = "";
			}
			if ($down < count($dataN)) {
				$pageParameter["pageDownTitle"] = $dataN[$down]["Title"];
				$pageParameter["pageDownLink"] = $this->news->linkNewsDetail($dataN[$down]["NewsID"]);
				if (!empty($dataN[$down]["Image"]) && parent::image_exists($dataN[$down]["Image"])) $pageParameter["pageDownImage"] = $dataN[$down]["Image"];
				$pageParameter["pageDownStyle"] = "";
			}
			foreach ($pageParameter as $name => $value) {
				parent::addParameter($name, $value);
			}
			
			// 結構化資料
			$GoogleSearch = $this->news->seoGoogleSearch($dal->NewsID, 1);
			parent::addParameter("GoogleSearch", $GoogleSearch);
			
			parent::loadData();
		}
		
		private function formatCategory($data = [], $id = 0) {
			foreach ($data as &$category) {
				$category["className"] = ($id == $category["CategoryID"]) ? " class='action'" : "";
				$category["link"] = $this->news->linkNewsList($category["CategoryID"]);
			}
			return $data;
		}
	}

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