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

	class _news extends pageBase {
		public $news;
	
		public function load() {
			$this->news = new news();
			$this->loadData();
			parent::loadTemplateFile("templates/{$this->registry->webTemplates}/news.html");
			parent::load();
		}
		
		public function loadData() {
			// 橫幅廣告
			$this->picturetype = "60";
			
			// 宣告
			$dal = new newsDAL();
			$dalC = new news_categoryDAL();
			
			// 預設值
			$parameter = [
				"CategoryName" => "最新消息",
				"CategoryLink" => $this->news->linkNewsList(),
				"CategoryDescription" => "",
				"styleCD" => " style='display:none;'",
				"categoryClass" => " class='action'",
			];
			if ($this->registry->webLinkType == "2") {
				$dalC->TwPathName = parent::getGet("c", "", 2);
			} else {
				$dalC->CategoryID = parent::getGet("id");
			}
			if ($dalC->TwPathName != "" || $dalC->CategoryID != "") {
				$dalC->loadRecord();
				if ($dalC->Visible != "1" && $dalC->Disable != "0") {
					$this->alertMsg = "找不到指定的頁面!";
					$this->locationHref = "/";
					return false;
				}
				$id = $dalC->CategoryID;
				$parameter["CategoryName"] = $dalC->CategoryName;
				$parameter["CategoryLink"] = $this->news->linkNewsList($dalC->CategoryID);
				$parameter["CategoryDescription"] = $dalC->Description;
				$parameter["styleCD"] = ($parameter["CategoryDescription"] == "") ? " style='display:none;'" : "";
				$parameter["categoryClass"] = "";

				if (!empty($dalC->BannerImage) && parent::image_exists($dalC->BannerImage)) parent::setPageBanner($dalC->BannerImage, $dalC->BannerTitle, $dalC->BannerLink, $dalC->BannerColor, $dalC->BannerDesc);

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

				if (!empty($dalC->WebTitle)) $parameter["webTitle"] = $dalC->WebTitle;
				if (!empty($dalC->WebKeyword)) $parameter["webKeyword"] = $dalC->WebKeyword;
				if (!empty($dalC->WebDescription)) $parameter["webDescription"] = $dalC->WebDescription;
			} else {
				$id = 0;
				if (!empty($this->registry->newsTitle)) $parameter["webTitle"] = $this->registry->newsTitle;
				if (!empty($this->registry->newsKeyword)) $parameter["webKeyword"] = $this->registry->newsKeyword;
				if (!empty($this->registry->newsDescription)) $parameter["webDescription"] =  $this->registry->newsDescription;
			}
			foreach ($parameter as $name => $value) {
				parent::addParameter($name, $value);
			}

			// 條件式
			$intoAry = [];
			$intoAry[] = parent::addArray(":onlineTime", time(), "int");
			$filter = "a.`Visible`=1 AND a.`Disable`=0 AND b.`Visible`=1 AND b.`Disable`=0 AND a.`OnlineTime`<:onlineTime";
			if (!empty($id) && $id != 0) {
				$intoAry[] = parent::addArray(":categoryID", $id, "int");
				$filter .= " AND a.`CategoryID`=:categoryID";
			}
			
			$rows = $dal->countRecordset($intoAry, $filter);

			$p = (int) parent::getGet("p", 1);
			$pz = (int) ($this->registry->newsPageSize ?: 10);
			$pMax = ceil($rows / $pz);
			$p = max(min($p, $pMax), 1);
			$start = ($p - 1) * $pz;

			$orderExp = "a.`OrderNO`,a.`OnlineTime` DESC,a.`NewsID` DESC";
			$data = $dal->executeRecordset($intoAry, $filter, $orderExp, $start, $pz);
			$data = $this->formatNews($data, $start);
			$list = new repeater();
			$list->id = "list";
			$list->dataSource = $data;
			parent::addControl($list);

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

			//頁導航
			if ($rows > 0) {
				$pagtxt = parent::generatePagination($p, $pMax, $id, function ($id, $page) {
					return $this->news->linkNewsList($id, $page);
				});
			} else {
				$pagtxt = '沒有資料';
			}
			parent::addParameter("pagtxt", $pagtxt);
			
			// 結構化資料
			$GoogleSearch = "";
			if (!empty($id) && $id != 0) $GoogleSearch = $this->news->seoGoogleSearch($id);
			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;
		}

		private function formatNews($data = [], $start = 0) {
			foreach ($data as $key => &$item) {
				$item["link"] = $this->news->linkNewsDetail($item["NewsID"]);
				$item["linkC"] = $this->news->linkNewsList($item["CategoryID"]);
				
				if (empty($item["Image"]) || !parent::image_exists($item["Image"])) $item["Image"] = $this->NoPicImage;
				$item["imageStyle"] = ($item["Image"] == $this->NoPicImage) ? " style='display:none;'" : "";

				$item["NO1"] = $key + 1;
				$item["NO2"] = str_pad($item["NO1"], 2, "0", STR_PAD_LEFT);
				$item["NO3"] = $item["NO1"] + $start;
				$item["NO4"] = str_pad($item["NO3"], 3, "0", STR_PAD_LEFT);

				$item["Introduction"] = str_replace("\r\n", "<br />", $item["Introduction"]);
				$item["sIntroduction"] = mb_substr($item["Introduction"], 0, 50, "utf-8");
				$item["OnlineDate"] = date("Y-m-d", $item["OnlineTime"]);
				$item["OnlineY"] = date("Y", $item["OnlineTime"]);
				$item["OnlineM"] = date("m", $item["OnlineTime"]);
				$item["OnlineD"] = date("d", $item["OnlineTime"]);
				$item["OnlineM2"] = date("M", $item["OnlineTime"]);
			}
			return $data;
		}
	}

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