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

	class _book extends pageBase {
		public $book;
		private $dalC;
	
		public function load() {
			$this->book = new book();
			$this->dalC = new book_categoryDAL();
			$this->loadData();
			parent::loadTemplateFile("templates/{$this->registry->webTemplates}/book.html");
			parent::load();
		}
		
		public function loadData() {
			// 橫幅廣告
			$this->picturetype = "80";
			
			// 宣告
			$dal = new bookDAL();
			$dalC2 = new book_categoryDAL();
			$dalC3 = new book_categoryDAL();
			
			// 預設值
			$parameter = [
				"CategoryName" => "相本",
				"CategoryLink" => $this->book->linkBookList(),
				"CategoryDescription" => "",
				"styleCD" => " style='display:none;'",
			];

			$aryR = [];
			if ($this->registry->webLinkType == "2") {
				$this->dalC->TwPathName = parent::getGet("c", "", 2);
			} else {
				$this->dalC->CategoryID = parent::getGet("id");
			}
			if ($this->dalC->TwPathName != "" || $this->dalC->CategoryID != "") {
				$this->dalC->loadRecord();
				if (!($this->dalC->Visible == "1" && $this->dalC->Disable == "0") || 
					!($this->dalC->ParentID == "0" || ($this->dalC->ParentVisible == "1" && $this->dalC->ParentDisable == "0")) || 
					!($this->dalC->ParentID == "0" || $this->dalC->GrandID == "0" || ($this->dalC->GrandVisible == "1" && $this->dalC->GrandDisable == "0"))) {
					$this->alertMsg = "找不到指定的頁面!";
					$this->locationHref = "/";
					return false;
				}
				$id = $this->dalC->CategoryID;
				$parameter["CategoryName"] = $this->dalC->CategoryName;
				$parameter["CategoryLink"] = $this->book->linkBookList($this->dalC->CategoryID);
				$parameter["CategoryDescription"] = $this->dalC->Description;
				$parameter["styleCD"] = ($parameter["CategoryDescription"] == "") ? " style='display:none;'" : "";
				
				$aryR[] = ["Name"=>$parameter["CategoryName"], "Link"=>$parameter["CategoryLink"]];
				if ($this->dalC->ParentID != "0") {
					$aryR[] = ["Name"=>$this->dalC->ParentName, "Link"=>$this->book->linkBookList($this->dalC->ParentID)];
					if ($this->dalC->GrandID != "0") $aryR[] = ["Name"=>$this->dalC->GrandName, "Link"=>$this->book->linkBookList($this->dalC->GrandID)];
				}
				$aryR = array_reverse($aryR);

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

				if (!empty($this->dalC->WebTitle)) $parameter["webTitle"] = $this->dalC->WebTitle;
				if (!empty($this->dalC->WebKeyword)) $parameter["webKeyword"] = $this->dalC->WebKeyword;
				if (!empty($this->dalC->WebDescription)) $parameter["webDescription"] = $this->dalC->WebDescription;
				
				if (!empty($this->dalC->BannerImage) && parent::image_exists($this->dalC->BannerImage)) {
					parent::setPageBanner($this->dalC->BannerImage, $this->dalC->BannerTitle, $this->dalC->BannerLink, $this->dalC->BannerColor, $this->dalC->BannerDesc);
				} else {
					if ($this->dalC->ParentID != "0") {
						$dalC2->CategoryID = $this->dalC->ParentID;
						$dalC2->loadRecord();
						if (!empty($dalC2->BannerImage) && parent::image_exists($dalC2->BannerImage)) {
							parent::setPageBanner($dalC2->BannerImage, $dalC2->BannerTitle, $dalC2->BannerLink, $dalC2->BannerColor, $dalC2->BannerDesc);
						} else {
							if ($dalC2->ParentID != "0") {
								$dalC3->CategoryID = $dalC2->ParentID;
								$dalC3->loadRecord();
								if (!empty($dalC3->BannerImage) && parent::image_exists($dalC3->BannerImage)) parent::setPageBanner($dalC3->BannerImage, $dalC3->BannerTitle, $dalC3->BannerLink, $dalC3->BannerColor, $dalC3->BannerDesc);
							}
						}
					}
				}
			} else {
				$id = 0;
				if (!empty($this->registry->bookTitle)) $parameter["webTitle"] = $this->registry->bookTitle;
				if (!empty($this->registry->bookKeyword)) $parameter["webKeyword"] = $this->registry->bookKeyword;
				if (!empty($this->registry->bookDescription)) $parameter["webDescription"] =  $this->registry->bookDescription;
			}
			foreach ($parameter as $name => $value) {
				parent::addParameter($name, $value);
			}

			// 麵包削
			$listR = new repeater();
			$listR->id = "listR";
			$listR->dataSource = $aryR;
			parent::addControl($listR);
			
			// 頁碼、單頁範圍
			$p = (int) parent::getGet("p", 1);
			$pz = (int) ($this->registry->bookPageSize ?: 20);
			
			// 條件式
			$intoAry = [];
			$filter = "a.`Visible`=1 AND a.`Disable`=0 AND b.`Visible`=1 AND b.`Disable`=0 AND (b.`ParentID`=0 OR c.`Visible`=1 OR c.`Disable`=0) AND (b.`ParentID`=0 OR c.`ParentID`=0 OR d.`Visible`=1 OR d.`Disable`=0)";
			$orderExp = "";
			if (!empty($id) && $id != 0) {
				$intoAry[] = parent::addArray(":categoryID", $id, "int");
				$intoAry[] = parent::addArray(":parentID", $id, "int");
				$intoAry[] = parent::addArray(":grandID", $id, "int");
				$filter .= " AND (a.`CategoryID`=:categoryID OR b.`ParentID`=:parentID OR c.`ParentID`=:grandID)";
			}

			$rows = $dal->countRecordset($intoAry, $filter);
			$pMax = ceil($rows / $pz);
			$p = max(min($p, $pMax), 1);
			$start = ($p - 1) * $pz;

			$data = $dal->execute2Recordset($intoAry, $filter, $orderExp, $start, $pz);
			$data = $this->formatBook($data);
			$list = new repeater();
			$list->id = "list";
			$list->dataSource = $data;
			parent::addControl($list);

			// 相本分類列表
			$dataC = $this->dalC->executeRecordset([], "a.`ParentID`=0 AND 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->book->linkBookList($id, $page);
				});
			} else {
				$pagtxt = '沒有資料';
			}
			parent::addParameter("pagtxt", $pagtxt);
			
			// 結構化資料
			$GoogleSearch = (!empty($id) && $id != 0) ? $this->book->seoGoogleSearch($id) : "";
			parent::addParameter("GoogleSearch", $GoogleSearch);
			
			parent::loadData();
		}

		private function formatCategory($data = [], $id = 0) {
			$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>";
			$this->dalC->CategoryID = $id;
			$this->dalC->loadRecord();
			foreach ($data as &$category) {
				$category["link"] = $this->book->linkBookList($category["CategoryID"]);
				$category["iLabel"] = "";
				$category["SubList"] = "";
				$category["className"] = "";

				$intoAry = [parent::addArray(":parentID", $category["CategoryID"], "int")];
				$dataS = $this->dalC->executeRecordset($intoAry, "a.`ParentID`=:parentID AND a.`Visible`=1 AND a.`Disable`=0");
				if (count($dataS) > 0) {
					$categoryId = $category["CategoryID"];
					if ($categoryId == $id || $categoryId == $this->dalC->ParentID || $categoryId == $this->dalC->GrandID) {
						$category["className"] = " class='action'";
						$category["iLabel"] = $iLabel2;
					} else {
						$category["iLabel"] = $iLabel;
					}
					
					$category["SubList"] .= "<ul class='subUL'>";
					foreach ($dataS as $rows) {
						$into2Ary = [parent::addArray(":parentID", $rows["CategoryID"], "int")];
						$dataS2 = $this->dalC->executeRecordset($into2Ary, "a.`ParentID`=:parentID AND a.`Visible`=1 AND a.`Disable`=0");

						$category["SubList"] .= ($rows["CategoryID"] == $id || $rows["CategoryID"] == $this->dalC->ParentID) ? "<li class='action'>" : "<li>";
						$category["SubList"] .= "<div class='subULHead'><p><a href='".$this->book->linkBookList($rows["CategoryID"])."'>".$rows["CategoryName"]."</a></p>";
						
						if (count($dataS2) > 0) {
							if ($rows["CategoryID"] == $id || $rows["CategoryID"] == $this->dalC->ParentID) {
								$category["className"] = " class='action'";
								$category["SubList"] .= $iLabel2."</div>";
							} else {
								$category["SubList"] .= $iLabel."</div>";
							}

							$category["SubList"] .= "<ul class='sub2UL'>";
							foreach ($dataS2 as $rows2) {
								$category["SubList"] .= ($rows2["CategoryID"] == $id) ? "<li class='action'>" : "<li>";
								$category["SubList"] .= "<div class='sub2ULHead'><p><a href='".$this->book->linkBookList($rows2["CategoryID"])."'>".$rows2["CategoryName"]."</a></p></div></li>";
							}
							$category["SubList"] .= "</ul>";
						} else {
							$category["SubList"] .= "</div>";
						}
						$category["SubList"] .= "</li>";
					}
					$category["SubList"] .= "</ul>";
				} else {
					if ($category["CategoryID"] == $id) $category["className"] = " class='action'";
				}
			}
			return $data;
		}

		private function formatBook($data = []) {
			foreach ($data as &$item) {
				$item["link"] = $this->book->linkBookDetail($item["BookID"]);
				$item["linkC"] = $this->book->linkBookList($item["CategoryID"]);
				
				if (empty($item["BookImage"]) || !parent::image_exists($item["BookImage"])) $item["BookImage"] = $this->NoPicImage;
				$item["imageStyle"] = ($item["BookImage"] == $this->NoPicImage) ? " style='display:none;'" : "";
			}
			return $data;
		}
	}

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