<?php
	require_once dirname(dirname(__FILE__))."/bin/web/repeater.php";
	require_once dirname(dirname(__FILE__))."/dal/customDAL.php";
	require_once dirname(dirname(__FILE__))."/dal/pictureDAL.php";

	class _indexArticle extends webPartBase {
		public function load() : void {
			$this->loadData();
			if ($this->registry->indexAboutShow == "1") parent::loadTemplateFile(dirname(__FILE__)."/../templates/webParts/".$this->registry->webTemplates."/indexArticle.html");
			parent::load();
		}
		
		public function loadData() : void {
			// 關於我們
			$aboutImage = $this->registry->indexAboutImage;
			$aboutImageStyle = " style=\"display:none;\"";
			if (!empty($aboutImage) && parent::image_exists($aboutImage)) {
				$aboutImageStyle = "";
				$aboutImageValue = $aboutImage;
			} else {
				$aboutImageValue = $this->NoPicImage;
			}
			$indexAboutLink = $this->registry->indexAboutLink;
			$indexAboutLinkStyle = " style=\"display:none;\"";
			$indexAboutLinkT = "";
			if (!empty($indexAboutLink)) {
				$indexAboutLinkStyle = "";
				$indexAboutLinkT = $this->registry->indexAboutLinkT;
				$aboutLinkValue = $indexAboutLink;
			} else {
				$aboutLinkValue = "javascript:void(0);";
			}
			$aboutParams = [
				"indexAboutTitle1" => $this->registry->indexAboutTitle1,
				"indexAboutTitle2" => $this->registry->indexAboutTitle2,
				"aboutIntroduction" => $this->registry->indexAboutIntroduction,
				"aboutImageStyle" => $aboutImageStyle,
				"aboutImage" => $aboutImageValue,
				"indexAboutLinkStyle" => $indexAboutLinkStyle,
				"indexAboutLinkT" => $indexAboutLinkT,
				"aboutLink" => $aboutLinkValue,
			];
			foreach ($aboutParams as $paramName => $paramValue) {
				parent::addParameter($paramName, $paramValue);
			}

			// 小廣告
			$dalP = new pictureDAL();
			$aryP = [];
			$dataP = $dalP->executeRecordset([], "a.`PictureType`=20", "a.`TypeOrderNO`");
			$num1 = 0;
			foreach ($dataP 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"])
					];
					$aryP[] = $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"]);
				}
			}
			$listP = new repeater();
			$listP->id = "listP";
			$listP->dataSource = $aryP;
			parent::addControl($listP);
			
			// Youbtube
			$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";
				}
			} else {
				$youtubeStyle = " style='display:none;'";
			}
			parent::addParameter("youtubeHTML", $youtubeHTML);
			parent::addParameter("youtubeStyle", $youtubeStyle);
			parent::addParameter("youtubeimg", $youtubeimg);
			parent::addParameter("youtubeURL", $youtubeURL);

			// 結構化資料
			$phone = ($this->registry->webPhone != "") ? $this->registry->webPhone : $this->registry->webMobile;
			$GoogleSearch = "";
			if ($this->registry->indexIsShowJason == 1) $GoogleSearch .= parent::localBusinessHtml(parent::getLogo(), $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);
		}
	}
?>