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

	class _articledetail extends pageBase {
		public $article;
	
		public function load() {
			$this->article = new article();
			$this->loadData();
			parent::loadTemplateFile("templates/{$this->registry->webTemplates}/article-detail.html");
			parent::load();
		}
		
		public function loadData() {
			// 橫幅廣告
			$this->picturetype = "40";
			
			// 宣告
			$dal = new articleDAL();
			$dalC = new article_categoryDAL();
			$path = parent::getGet("path", "", 2);

			if ($this->registry->webLinkType === "2") {
				$dal->TwPathName = $path;
			} else {
				$dal->PathName = $path;
			}
			$dal->loadRecord();
			if ($dal->Visible != "1" || $dal->CategoryVisible != "1") {
				$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,
				"imgStyle" => "",
				"link" => $this->article->linkArticleDetail($dal->ArticleID),
				"ArticleID" => $dal->ArticleID,
				"CategoryID" => $dal->CategoryID,
				"CategoryName" => $dal->CategoryName,
				"linkC" => $this->article->linkArticleList($dal->CategoryID),
				// 結構化資料預設值
				"key" => "",
				"code" => "",
				"scoreStyle" => " style='display:none;'",
			];

			// 資料內容檢查
			if ($parameter["Image"] == $this->NoPicImage) $parameter["imgStyle"] = " 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;
			
			// 結構化資料
			$parameter["GoogleSearch"] = ($this->registry->isScore != "0") ? $this->article->seoGoogleSearch($dal->ArticleID, 1) : "";
			if ($this->registry->isScore == "2" && isset($_SESSION["member_id"]) && $_SESSION["member_id"] != "") {
				$scoreCode = "";
				for ($i = 0; $i < 10; $i++) $scoreCode .= chr(rand(65, 90));
				$_SESSION["scoreAry"][] = ["type" => "2", "id" => $dal->ArticleID, "code" => $scoreCode];
				end($_SESSION["scoreAry"]);
				$key = key($_SESSION["scoreAry"]);
				$parameter["key"] = $key;
				$parameter["code"] = $scoreCode;
				$parameter["scoreStyle"] = "";
			}

			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")];
			$dataA = $dal->executeRecordset($intoAry, "a.`CategoryID`=:categoryID AND a.`Visible`=1 AND a.`Disable`=0");
			$dataA = $this->formatArticle($dataA);
			$listA = new repeater();
			$listA->id = "listA";
			$listA->dataSource = $dataA;
			parent::addControl($listA);
			
			parent::loadData();
		}
		
		private function formatCategory($data = [], $id = 0) {
			$dal = new articleDAL();
			$iLabel = "<b data-action='sideOpen'><i class='fa fa-plus' aria-hidden='true'></i></b>";
			$iLabel2 = "<b data-action='sideOpen'><i class='fa fa-minus' aria-hidden='true'></i></b>";
			foreach ($data as &$category) {
				$category["className"] = ($id == $category["CategoryID"]) ? " class='action'" : "";
				$category["link"] = $this->article->linkArticleList($category["CategoryID"]);
				$category["iLabel"] = "";
				$category["SubList"] = "";
				
				$intoAry = [parent::addArray(":categoryID", $category["CategoryID"], "int")];
				$dataA = $dal->executeRecordset($intoAry, "a.`CategoryID`=:categoryID AND a.`Visible`=1 AND a.`Disable`=0");
				if (count($dataA) > 0) {
					$category["iLabel"] = ($id == $category["CategoryID"]) ? $iLabel2 : $iLabel;
					$category["SubList"] .= "<ul class='subUL'>";
					foreach ($dataA as $rowsA) {
						$category["SubList"] .= "<li><div class='subULHead'><p><a href='".$this->article->linkArticleDetail($rowsA["ArticleID"])."'>".$rowsA["Title"]."</a></p></div></li>";
					}
					$category["SubList"] .= "</ul>";
				}
			}
			return $data;
		}

		private function formatArticle($data = []) {
			foreach ($data as &$item) {
				$item["link"] = $this->article->linkArticleDetail($item["ArticleID"]);
				$item["linkC"] = $this->article->linkArticleList($item["CategoryID"]);
				
				if (empty($item["Image"]) || !parent::image_exists($item["Image"])) $item["Image"] = $this->NoPicImage;
				$item["imageStyle"] = ($item["Image"] == $this->NoPicImage) ? " style='display:none;'" : "";
			}
			return $data;
		}
	}

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