<?php
	require_once "pageBase.php";
	require_once "bin/system/string.php";
	require_once "bin/web/repeater.php";
	require_once "dal/customDAL.php";
	require_once "dal/pictureDAL.php";
	require_once "dal/productDAL.php";
	class _index extends pageBase{
		public function load(){
			$this->loadData();
			parent::loadTemplateFile("templates/{$this->registry->webTemplates}/index.html");
			parent::load();
		}

		public function loadData(){
			// 宣告
			$dal = new pictureDAL();
			$dalB = new bookDAL();
			$dalC = new customDAL();
			$dalN = new newsDAL();
			$dalP = new productDAL();

			// 設定header參數
			$this->is_index = true;
			$this->picturetype = 30;
			
			// LOGO
			$dal->PictureType = "10";
			$dal->TypeOrderNO = "0";
			$dal->loadRecord();
			if (empty($dal->Image) || parent::image_exists($dal->Image)) {
				$logoImage = $this->NoPicImage;
				parent::addParameter("fbImage", $this->NoPicImage);
				parent::addParameter("logoStyle", " style='display:none; width:auto;'");
			} else {
				$logoImage = $dal->Image;
				parent::addParameter("fbImage", $dal->Image);
				parent::addParameter("logoStyle", "");
			}
			parent::addParameter("logoImage", $logoImage);
			$logoTitle = (!empty($dal->Title) ? $dal->Title : $this->registry->webName);
			parent::addParameter("logoTitle", $logoTitle);
			$logoLink = (!empty($dal->Link) ? $dal->Link : "/");
			parent::addParameter("logoLink", $logoLink);
			
			// 結構化資料
			$phone = ($this->registry->webPhone != "") ? $this->registry->webPhone : $this->registry->webMobile;
			$GoogleSearch = "";
			if ($this->registry->indexIsShowJason == 1) $GoogleSearch .= parent::localBusinessHtml($logoImage, $this->registry->webTitle, $phone, $this->registry->indexRatingValue, $this->registry->indexBestRating);
			if ($this->registry->indexIsShowStars == 1) $GoogleSearch .= parent::isShowStars($this->registry->indexRatingValue, $this->registry->indexReviewCount);
			parent::addParameter("GoogleSearch", $GoogleSearch);
			
			// 自訂欄位
			$dataC = $dalC->executeRecordset(
				intoAry: [],
				filter: "a.`Visible`=1 AND a.`Disable`=0"
			);
			foreach ($dataC as $key => &$item) {
				$item["NO"] = ++$key;
				$item["CustomContent"] = str_replace("\r\n", "<br />", $item["CustomContent"]);
				$item["imageStyle"] = "";
				if ($item["CustomImage"] == "" || !parent::image_exists($item["CustomImage"])) {
					$item["CustomImage"] = $this->NoPicImage;
					$item["imageStyle"] = " style='display: none;'";
				}
				if ($item["CustomLink"] == "") $item["CustomLink"] = "javascript:void(0);";
			}
			$listC = new repeater();
			$listC->id = "listC";
			$listC->dataSource = $dataC;
			parent::addControl($listC);
			$listC2 = new repeater();
			$listC2->id = "listC2";
			$listC2->dataSource = $dataC;
			parent::addControl($listC2);
			$customClass = count($dataC) ? "" : " noneCI";
			parent::addParameter("customClass", $customClass);
			
			// 關於我們
			parent::addParameter("indexAboutTitle1", $this->registry->indexAboutTitle1);
			parent::addParameter("indexAboutTitle2", $this->registry->indexAboutTitle2);
			parent::addParameter("aboutIntroduction", $this->registry->indexAboutIntroduction);
			$aboutImage = ($this->registry->indexAboutImage ?? '');
			if (empty($aboutImage) || !parent::image_exists($aboutImage)) {
				parent::addParameter("aboutImageStyle", " style='display:none;'");
				parent::addParameter("aboutImage", $this->NoPicImage);
			} else {
				parent::addParameter("aboutImageStyle", "");
				parent::addParameter("aboutImage", $aboutImage);
			}
			$aboutLink = ($this->registry->indexAboutLink ?? '');
			if (empty($aboutLink)) {
				parent::addParameter("indexAboutLinkStyle", " style='display:none;'");
				parent::addParameter("indexAboutLinkT", "");
				parent::addParameter("aboutLink", "javascript:void(0);");
			} else {
				parent::addParameter("indexAboutLinkStyle", "");
				parent::addParameter("indexAboutLinkT", ($this->registry->indexAboutLinkT ?? ''));
				parent::addParameter("aboutLink", $aboutLink);
			}

			// 最新消息
			$dataN = [];
			if ($this->registry->indexNShow == 1) {
				$news = new news();
				$arrNS = [];
				parent::addParameter("indexNTitle", $this->registry->indexNTitle);
				parent::addParameter("indexNMLink", $this->registry->indexNMLink);
				parent::addParameter("indexNMLinkTitle", $this->registry->indexNMLinkTitle);
				
				if ($this->registry->indexNBImg != "" && parent::image_exists($this->registry->indexNBImg)) $arrNS[] = "background-image:url({$this->registry->indexNBImg});";
				if ($this->registry->indexNBColor != "") $arrNS[] = "background-color: {$this->registry->indexNBColor};";
				$newsStyle = (count($arrNS) > 0) ? "style=\"" . implode(" ", $arrNS) . "\"" : "";
				parent::addParameter("newsStyle", $newsStyle);
				
				$dataN = $dalN->executeRecordset(
					intoAry: [],
					filter: "a.`Visible`=1 AND a.`Disable`=0 AND a.`OnlineTime`<=" . time() . " AND b.`IsIndex`=1 AND b.`Visible`=1 AND b.`Disable`=0",
					start: 0,
					length: ($this->registry->indexNNO ?? 0)
				);
				foreach ($dataN as $key => &$item) {
					if (empty($item["Image"]) || !parent::image_exists($item["Image"])) $item["Image"] = $this->NoPicImage;
					$item["imageStyle"] = (empty($item["Image"]) || !parent::image_exists($item["Image"])) ? " style='display:none;'" : "";
					$item["link"] = $news->linkNewsDetail($item["NewsID"]);
					$item["linkC"] = $news->linkNewsList($item["CategoryID"]);

					$onlineTime = ($item["OnlineTime"] != "0") ? $item["OnlineTime"] : time();
					$item["OnlineDate"] = date("Y-m-d", $onlineTime);
					$item["OnlineDY"] = date("Y", $onlineTime);
					$item["OnlineDM"] = date("m", $onlineTime);
					$item["OnlineDD"] = date("d", $onlineTime);
					$item["OnlineDM2"] = date("M", $onlineTime);
					$item["newsStyle"] = ($this->registry->indexNBColor != "") ? " style='background-color:" . $this->registry->indexNBColor . "'" : "";
			
					$item["NO"] = ++$key;
					$item["NO2"] = str_pad($item["NO"], 2, '0', STR_PAD_LEFT);
				}
			}
			$listN = new repeater();
			$listN->id = "listN";
			$listN->dataSource = $dataN;
			parent::addControl($listN);
			parent::addParameter("countN", count($dataN));
			parent::addParameter("countN2", str_pad(count($dataN), 2, '0', STR_PAD_LEFT));
			
			$filter = "a.`IsIndex`=1 AND a.`Visible`=1 AND a.`Disable`=0 AND b.`IsIndex`=1 AND b.`Visible`=1 AND b.`Disable`=0 AND (b.`ParentID`=0 OR (c.`IsIndex`=1 AND c.`Visible`=1 AND c.`Disable`=0)) AND (b.`ParentID`=0 OR c.`ParentID`=0 OR (d.`IsIndex`=1 AND d.`Visible`=1 AND d.`Disable`=0))";

			// 產品
			$dataP = [];
			if ($this->registry->indexPShow == 1) {
				$product = new product();
				$arrPS = [];
				parent::addParameter("indexPTitle", $this->registry->indexPTitle);
				parent::addParameter("indexPMLink", $this->registry->indexPMLink);
				parent::addParameter("indexPMLinkTitle", $this->registry->indexPMLinkTitle);
				
				if ($this->registry->indexPBImg != "" && parent::image_exists($this->registry->indexPBImg)) $arrPS[] = "background-image:url({$this->registry->indexPBImg});";
				if ($this->registry->indexPBColor != "") $arrPS[] = "background-color: {$this->registry->indexPBColor};";
				$productStyle = (count($arrPS) > 0) ? "style=\"" . implode(" ", $arrPS) . "\"" : "";
				parent::addParameter("productStyle", $productStyle);

				$dataP = $dalP->executeRecordset(
					intoAry: [],
					filter: $filter,
					order: "RAND()",
					start: 0,
					length: ($this->registry->indexPNO ?? 0)
				);
				foreach ($dataP as &$item) {
					if (empty($item["IconImage"]) || !parent::image_exists($item["IconImage"])) $item["IconImage"] = $this->NoPicImage;
					
					if ($this->registry->ShowPrice == "1") {
						if ($item["Price"] == "0" || $item["Price"] == "") $item["priceStyle"] = " style='display:none;'";
						if ($item["OldPrice"] == "0" || $item["OldPrice"] == "") $item["oldPriceStyle"] = " style='display:none;'";
					} else {
						$item["priceStyle"] = " style='display:none;'";
						$item["oldPriceStyle"] = " style='display:none;'";
						$item["sellStyle"] = " style='display:none;'";
					}

					$item["link"] = $product->linkProductDetail($item["ProductID"]);
					$item["linkC"] = $product->linkProductList($item["CategoryID"]);
				}
			}
			$listP = new repeater();
			$listP->id = "listP";
			$listP->dataSource = $dataP;
			parent::addControl($listP);
			
			// 相本
			$dataB = [];
			if ($this->registry->indexBShow == 1) {
				$book = new book();
				$arrBS = [];
				parent::addParameter("indexBTitle", $this->registry->indexBTitle);
				parent::addParameter("indexBMLink", $this->registry->indexBMLink);
				parent::addParameter("indexBMLinkTitle", $this->registry->indexBMLinkTitle);

				if ($this->registry->indexBBImg != "" && parent::image_exists($this->registry->indexBBImg)) $arrBS[] = "background-image:url({$this->registry->indexBBImg});";
				if ($this->registry->indexBBColor != "") $arrBS[] = "background-color: {$this->registry->indexBBColor};";
				$bookStyle = (count($arrBS) > 0) ? "style=\"" . implode(" ", $arrBS) . "\"" : "";
				parent::addParameter("bookStyle", $bookStyle);
			
				$dataB = $dalB->executeRecordset(
					intoAry: [],
					filter: $filter,
					order: "RAND()",
					start: 0,
					length: ($this->registry->indexBNO ?? 0)
				);
				foreach ($dataB as &$item) {
					if (empty($item["BookImage"]) || !parent::image_exists($item["BookImage"])) $item["BookImage"] = $this->NoPicImage;
					
					$item["link"] = $book->linkBookDetail($item["BookID"]);
					$item["linkC"] = $book->linkBookList($item["CategoryID"]);

					$onlineTime = ($item["CreatedTime"] != "0") ? $item["CreatedTime"] : time();
					$item["OnlineDate"] = date("Y-m-d", $onlineTime);
					$item["OnlineDY"] = date("Y", $onlineTime);
					$item["OnlineDM"] = date("m", $onlineTime);
					$item["OnlineDD"] = date("d", $onlineTime);
					$item["OnlineDM2"] = date("M", $onlineTime);
				}
			}
			$listB = new repeater();
			$listB->id = "listB";
			$listB->dataSource = $dataB;
			parent::addControl($listB);

			//Youbtube
			$youtubeBox = "";
			$youtubeHTML = "";
			$youtubeStyle = "";
			$youtubeimg = $this->NoPicImage;
			$youtubeURL = "javascript:void(0);";
			if ($this->registry->youtubeIndex == "1") {
				if ($this->registry->youtubeURL != "") {
					$youtubeURL = str_replace("//youtu.be/", "//www.youtube.com/embed/", $this->registry->youtubeURL);
					if ($this->registry->youtubeType == "1") {
						$youtubeHTML .= "<a id=\"playYoutu\" href=\"{$youtubeURL}\" data-fancybox=\"gallery\" title=\"{$this->registry->indexAboutTitle1}\"><i class=\"fa fa-play\"></i></a>";
					} else if ($this->registry->youtubeType == "2") {
						$youtubeHTML .= "<a id=\"playYoutu\" href=\"{$this->registry->youtubeURL}\" target=\"_blank\" title=\"{$this->registry->indexAboutTitle1}\"><i class=\"fa fa-play\"></i></a>";
					} else if ($this->registry->youtubeType == "3") {
						$youtubeHTML .= "<iframe class=\"embed-player slide-media\" src=\"{$youtubeURL}\" frameborder=\"0\" allowfullscreen=\"\" allow=\"autoplay\"></iframe>";
					}
					$youtubeimg = "https://img.youtube.com/vi/" . substr($youtubeURL, -11) . "/maxresdefault.jpg";
					$youtubeBox = "<li><div class=\"bg\">{$youtubeHTML}</div><div class=\"info\"></div></li>";
				}
			} else {
				$youtubeStyle = " style='display:none;'";
			}
			parent::addParameter("youtubeBox", $youtubeBox);
			parent::addParameter("youtubeHTML", $youtubeHTML);
			parent::addParameter("youtubeStyle", $youtubeStyle);
			parent::addParameter("youtubeimg", $youtubeimg);
			parent::addParameter("youtubeURL", $youtubeURL);
			
			//臉書粉絲團
			$fbFans = "";
			if (!empty($this->registry->fbFansURL)) {
				$facepile = ($this->registry->fbFansFriendFace == '0') ? 'false' : 'true';
				$hidecover = ($this->registry->fbFansCoverPhoto == '1') ? 'false' : 'true';
				$showpost = ($this->registry->fbFansPagePost == '0') ? 'false' : 'true';
			
				$fbFans = "<div class=\"fb-page\" data-href=\"{$this->registry->fbFansURL}\" data-width=\"1200\" data-adapt-container-width=\"true\" data-hide-cover=\"{$hidecover}\" data-show-facepile=\"{$facepile}\" data-show-posts=\"{$showpost}\"><div class=\"fb-xfbml-parse-ignore\"><blockquote cite=\"{$this->registry->fbFansURL}\"><a href=\"{$this->registry->fbFansURL}\">Facebook</a></blockquote></div></div>";
			}
			parent::addParameter("fbFans", $fbFans);

			//LinkTarget
			$LinkTarget = (parent::generateIosDevice()) ? " target=\"_blank\"" : "";
			parent::addParameter("LinkTarget", $LinkTarget);
			parent::addParameter("lineTitle", $this->registry->lineTitle);
			
			// LINE QRCode
			$lineQRCode = $this->registry->lineQRCode;
			if (empty($lineQRCode) || !parent::image_exists($lineQRCode)) {
				$QRstyle = "display:none;";
				$lineQRCode = $this->NoPicImage;
			} else {
				$QRstyle = "";
			}
			parent::addParameter("QRstyle", $QRstyle);
			parent::addParameter("lineQRCode", $lineQRCode);

			//小廣告圖
			$aryPT = [];
			$dataPT = $dal->executeRecordset(
				intoAry: [],
				filter: "a.`PictureType`='20'",
				order: "a.`TypeOrderNO`"
			);
			foreach ($dataPT as $key => $item) {
				if (!empty($item["Image"]) && parent::image_exists($item["Image"])) {
					$ary = [
						"Image" => $item["Image"],
						"Title" => $item["Title"],
						"Link" => (!empty($item["Link"]) ? $item["Link"] : "javascript:void(0);"),
						"DanDesc" => str_replace("\r\n", "<br />", $item["DanDesc"])
					];
					$aryPT[] = $ary;
					parent::addParameter("small_picture_" . $key . "_image", $ary["Image"]);
					parent::addParameter("small_picture_" . $key . "_title", $ary["Title"]);
					parent::addParameter("small_picture_" . $key . "_link", $ary["Link"]);
					parent::addParameter("small_picture_" . $key . "_danDesc", $ary["DanDesc"]);
				}
			}
			$listPT = new repeater();
			$listPT->id = "listPT";
			$listPT->dataSource = $aryPT;
			parent::addControl($listPT);

			//父類方法
			parent::loadData();
		}
	}

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