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

	class _faq extends pageBase {
		public $faq;
	
		public function load() {
			$this->faq = new faq();
			$this->loadData();
			parent::loadTemplateFile("templates/{$this->registry->webTemplates}/faq.html");
			parent::load();
		}
		
		public function loadData() {
			// 橫幅廣告
			$this->picturetype = "70";
			
			// 宣告
			$dal = new faqDAL();
			$dalC = new faq_categoryDAL();
			
			// 預設值
			$parameter = [
				"CategoryName" => "問與答",
				"CategoryLink" => $this->faq->linkFaqList(),
				"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->faq->linkFaqList($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->faqTitle)) $parameter["webTitle"] = $this->registry->faqTitle;
				if (!empty($this->registry->faqKeyword)) $parameter["webKeyword"] = $this->registry->faqKeyword;
				if (!empty($this->registry->faqDescription)) $parameter["webDescription"] =  $this->registry->faqDescription;
			}
			foreach ($parameter as $name => $value) {
				parent::addParameter($name, $value);
			}

			// 條件式
			$intoAry = [];
			$filter = "a.`Visible`=1 AND a.`Disable`=0 AND b.`Visible`=1 AND b.`Disable`=0";
			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->faqPageSize ?: 10);
			$pMax = ceil($rows / $pz);
			$p = max(min($p, $pMax), 1);
			$start = ($p - 1) * $pz;

			$orderExp = "b.`OrderNO`,b.`CategoryID`,a.`OrderNO`,a.`FaqID`";
			$data = $dal->executeRecordset($intoAry, $filter, $orderExp, $start, $pz);
			$data = $this->formatFaq($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->faq->linkFaqList($id, $page);
				});
			} else {
				$pagtxt = '沒有資料';
			}
			parent::addParameter("pagtxt", $pagtxt);
			
			// 結構化資料
			$GoogleSearch = (!empty($id) && $id != 0) ? $this->faq->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->faq->linkFaqList($category["CategoryID"]);
			}
			return $data;
		}
		private function formatFaq($data = [], $start = 0) {
			foreach ($data as $key => &$item) {
				$item["link"] = $this->faq->linkFaqDetail($item["FaqID"]);
				$item["linkC"] = $this->faq->linkFaqList($item["CategoryID"]);

				$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);
			}
			return $data;
		}
	}

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