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

	class _banner extends webPartBase {
		public bool $is_index = false;
		public int $picturetype = 30;
		public string $searchkw = "";
		public string $bannerImage, $bannerTitle, $bannerLink, $bannerColor, $bannerDesc;
		
		public function load() : void {
			$this->loadData();
			parent::load();
		}
		
		public function loadData() : void {
			$dalP = new pictureDAL();
			// 網站版型
			parent::addParameter("webTemplates",$this->registry->webTemplates);

			// 全站搜尋
			parent::addParameter("searchkw", $this->searchkw);
			
			// logo
			$dalP->PictureType = "10";
			$dalP->TypeOrderNO = "0";
			$dalP->loadRecord();
			$logoImage = $this->NoPicImage;
			$logoStyle = " style=\"display:none; width: auto;\"";
			if ($dalP->Image !== "" && parent::image_exists($dalP->Image)) {
				$logoImage = $dalP->Image;
				$logoStyle = "";
			}
			$logoTitle = ($dalP->Title === "") ? $this->registry->webName : $dalP->Title;
			$logoLink = ($dalP->Link === "") ? "/" : $dalP->Link;
			parent::addParameter("logoImage", $logoImage);
			parent::addParameter("logoStyle", $logoStyle);
			parent::addParameter("logoTitle", $logoTitle);
			parent::addParameter("logoLink", $logoLink);

			// Youbtube
			$youtubeStyle = " style=\"display:none;\"";
			$youtubeURL = "";
			$youtubeImg = $this->NoPicImage;
			if ($this->registry->youtubeURL !== "") {
				$youtubeURL = str_replace("//youtu.be/","//www.youtube.com/embed/",$this->registry->youtubeURL);
				$youtubeImg = "https://img.youtube.com/vi/".substr($youtubeURL, -11)."/maxresdefault.jpg";
				$playList = stristr($youtubeURL, "embed/");
				$playList = preg_replace("/^embed\//", "", $playList);
	
				$queryParams = [];
				if ($this->registry->youtubeRel == "0") $queryParams[] = "rel=0";
				if ($this->registry->youtubeAutoPlay == "1") $queryParams[] = "autoplay=1";
				if ($this->registry->youtubeMute == "1") $queryParams[] = "mute=1";
				if ($this->registry->youtubeLoop == "1") $queryParams[] = "loop=1&playlist=".$playList;
	
				if (!empty($queryParams)) {
					$query = implode("&", $queryParams);
					$youtubeURL .= (strpos($youtubeURL, "?") ? "&" : "?") . $query;
				}
				$youtubeStyle = "<a href=\"".$youtubeURL."\" data-fancybox=\"gallery\"><i class=\"fa fa-play\"></i></a>";
			}
			parent::addParameter("youtubeStyle", $youtubeStyle);
			parent::addParameter("youtubeURL", $youtubeURL);
			parent::addParameter("youtubeImg", $youtubeImg);

			// Banner Start
			if ($this->is_index || $this->registry->pageBannerType == "10") {
				$aryP = [];
				$aryP2 = [];
				$dataP = $dalP->executeRecordset([], "a.`PictureType`='30'", "a.`TypeOrderNO`");

				foreach ($dataP as $item) {
					$ary = [
						"Title" => $item["Title"],
						"Link" => $item["Link"] !== "" ? $item["Link"] : "javascript:void(0);",
						"BanColorStyle" => $item["BanColor"] !== "" ? " style=\"background-color:".$item["BanColor"].";\"" : "",
						"DanDescStyle" => $item["DanDesc"] === "" ? " style='display:none;'" : "",
						"DanDesc" => str_replace("\r\n", "<br />", $item["DanDesc"]),
					];

					if ($item["Image"] !== "" && parent::image_exists($item["Image"])) {
						$ary["BannerImg"] = $item["Image"];
						$ary["Image"] = " style=\"background-image:url(".$item["Image"].");\"";
						$ary["Image2"] = " style=\"background-image:url(".$item["Image"].");\"";
					}

					if ($item["ShowType"] === 1 && $item["Image"] !== "" && parent::image_exists($item["Image"])) {
						$ary["boxClass"] = " image";
						$aryP[] = $ary;
						$aryP2[] = "\"".$item["Image"]."\"";
					} else if ($item["ShowType"] === 2) {
						if ($item["PictureVideo"] !== "" && parent::video_exists($item["PictureVideo"])) {
							$ary["boxClass"] = " video";
							$ary["boxStart"] = "";
							$ary["video"] = "<video class=\"slide-video slide-media\" autoplay=\"\" loop=\"loop\" runat=\"server\" preload=\"none\" playsinline=\"\" muted=\"muted\" poster=\"".$item["Image"]."\"><source src=\"".$item["PictureVideo"]."\" type=\"video/mp4\" />您的瀏覽器不支援HTML 5影片播放標籤&#60;video&#61;格式。Your browser doesn't support the&#60;video&#61;tag.</video>";

							$referer = parse_url($item["PictureVideo"]);
							if ($referer["host"] === "www.youtube.com" || $referer["host"] === "youtu.be") {
								$ary["youtubeURL"] = str_replace("//youtu.be/", "//www.youtube.com/embed/", $item["PictureVideo"]);
								$ary["youtubeimg"] = "https://img.youtube.com/vi/" . substr($ary["youtubeURL"], -11) . "/hqdefault.jpg";
					
								if (parent::determineDevice()) {
									if ($item["Image"] === "" || !parent::image_exists($item["Image"])) {
										$ary["BannerImg"] = $ary["youtubeimg"];
										$ary["Image2"] = " style=\"background-image:url(".$ary["youtubeimg"].");\"";
									}
								}
					
								$ary["boxClass"] = " youtube";
								$ary["video"] = "<iframe class=\"embed-player slide-media\" src=\"".$ary["youtubeURL"]."\" frameborder=\"0\" allowfullscreen allow=\"autoplay\"></iframe>";
							} else if ($referer["host"] == "player.vimeo.com") {
								$rtype = explode("/",$referer["path"]);
								if($rtype[1] == "external"){
									$ary["boxStart"] = " data-video-start=\"4\"";
								}else{
									$ary["boxClass"] = " vimeo";
									$ary["video"] = "<iframe class=\"embed-player slide-media\" src=\"".$item["PictureVideo"]."\" frameborder=\"0\" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>";
								}
							}
							$aryP[] = $ary;
						}
					}
				}
				$listP = new repeater();
				$listP->id = "listP";
				$listP->dataSource = $aryP;
				
				$listP2 = new repeater();
				$listP2->id = "listP2";
				$listP2->dataSource = $aryP;
				
				$txtP2 = implode(",", $aryP2);
				
				parent::addControl($listP);
				parent::addControl($listP2);
				parent::addParameter("txtP2", $txtP2);

				$templateFilePath = "banner_main.html";
			} else {
				if (!empty($this->bannerImage)) {
					$Image = $this->bannerImage;
					$Title = $this->bannerTitle;
					$Link = ($this->bannerLink === "") ? "javascript:void(0);" : $this->bannerLink;
					$BanColor = $this->bannerColor;
					$BanColorStyle = (!empty($this->bannerColor)) ? " style=\"background-color:".$this->bannerColor.";\"" : "";
					$DanDesc = $this->bannerDesc;
					$DanDescStyle = (!empty($this->bannerDesc)) ? " style=\"display:none;\"" : "";
				} else {
					$dalP->PictureType = $this->picturetype;
					$dalP->TypeOrderNO = "0";
					$dalP->loadRecord();
					$Image = ($dalP->Image != "" && parent::image_exists($dalP->Image)) ? $dalP->Image : $this->NoPicImage;
					$Title = $dalP->Title;
					$Link = ($dalP->Link != "") ? $dalP->Link : "javascript:void(0);";
					$BanColor = $dalP->BanColor;
					$BanColorStyle = ($dalP->BanColor != "") ? " style=\"background-color:".$dalP->BanColor.";\"" : "";
					$DanDesc = $dalP->DanDesc;
					$DanDescStyle = ($dalP->DanDesc != "") ? " style=\"display:none;\"" : "";
				}

				parent::addParameter("Image", $Image);
				parent::addParameter("Title", $Title);
				parent::addParameter("Link", $Link);
				parent::addParameter("BanColor", $BanColor);
				parent::addParameter("BanColorStyle", $BanColorStyle);
				parent::addParameter("DanDesc", $DanDesc);
				parent::addParameter("DanDescStyle", $DanDescStyle);

				$templateFilePath = "banner_sub.html";
			}

			parent::loadTemplateFile($templateFilePath);
			parent::loadTemplateFile(dirname(__FILE__) . "/../templates/webParts/{$this->registry->webTemplates}/{$templateFilePath}");
		}
	}
?>