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

	class _header extends webPartBase {
		public bool $is_index = false;
		public int $picturetype = 30;
		public string $searchkw = "";
		public $dal;
		
		public function load() : void {
			$this->dal = new webmenuDAL();
			$this->loadData();
			parent::loadTemplateFile(dirname(__FILE__) . "/../templates/webParts/" . $this->registry->webTemplates . "/header_in.html");
			parent::load();
		}

		public function loadData() : void {
			$dalE = new extrainfoDAL();
			$dalP = new pictureDAL();
			// 全站搜尋
			parent::addParameter("searchkw", $this->searchkw);
			
			/*==================== 網站資訊 ====================*/
			$webInfo = [
				"webName" => $this->registry->webName,
				"webEmail" => $this->registry->webEmail,
				"webAddress" => $this->registry->webAddress,
				"webPhone" => $this->registry->webPhone,
				"webMobile" => $this->registry->webMobile,
				"webFax" => $this->registry->webFax,
				"fbFansURL" => $this->registry->fbFansURL,
			];
			foreach ($webInfo as $paramName => $paramValue) {
				$style = (!empty($paramValue)) ? "" : "style='display:none;'";
				$paramStyle = ($paramName === "fbFansURL") ? "FBStyle" : $paramName . "Style";

				parent::addParameter($paramName, $paramValue);
				parent::addParameter($paramStyle, $style);
			}
			// 網站SEO
			$seoParams = [ "webH1", "webMarquee" ];
			foreach ($seoParams as $paramName) {
				parent::addParameter($paramName, $this->registry->$paramName);
			}
			// 網路資源
			$networkResources = [ "fbFansFriendFace", "fbFansCoverPhoto", "fbFansPagePost", "fbFansLOGO", "webTemplates" ];
			foreach ($networkResources as $paramName) {
				parent::addParameter($paramName, $this->registry->$paramName);
			}
			
			/*==================== Web Jump =====================*/
			$headerStyle = ($this->registry->webJumpType != "0") ? " style=\"position: relative !important;\"" : "";
			parent::addParameter("headerStyle","");
			
			/*==================== Line 相關 ====================*/
			// Line 連結開啟方式
			$userAgent = $_SERVER['HTTP_USER_AGENT'] ?? '';
			$deviceDetected = parent::determineDevice();
			$iOSDevice = (strpos($userAgent, 'iPhone') || strpos($userAgent, 'iPad'));
			$LinkTarget = ($deviceDetected && $iOSDevice && !strpos($userAgent, "CriOS") && strpos($userAgent, "Version")) ? " target=\"_blank\"" : " target=\"_blank\"";
			// Line QrCode
			$lineQRCode = $this->registry->lineQRCode;
			if (empty($lineQRCode) || !parent::image_exists($lineQRCode)) {
				parent::addParameter("QRstyle", " style='display:none;'");
				$lineQRCode = "/images/no-pic.jpg";
			} else {
				parent::addParameter("QRstyle", "");
			}
			parent::addParameter("LinkTarget", $LinkTarget);
			parent::addParameter("lineTitle", $this->registry->lineTitle);
			parent::addParameter("lineQRCode", $lineQRCode);

			/*===================== 購物相關 ======================*/
			$cartStyle = ($this->registry->webType != '20') ?" style='display:none;'":"";
			parent::addParameter("cartStyle", $cartStyle);
			
			/*===================== 額外資訊 ======================*/
			$dataE = $dalE->executeRecordset([], "a.`Visible`=1");
			$listE = new repeater();
			$listE->id = "listE";
			$listE->dataSource = $dataE;
			parent::addControl($listE);

			/*======================= 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);
			
			/*====================== 主選單 =======================*/
			$dataW = $this->dal->executeRecordset([], "a.`ParentID`=0 AND a.`Visible`=1", "a.`OrderNO`");
			$dataW = parent::formatWebMenu($dataW);

			$listW1 = new repeater();
			$listW1->id = "listW1";
			$listW1->dataSource = $dataW;
			parent::addControl($listW1);

			$listW2 = new repeater();
			$listW2->id = "listW2";
			$listW2->dataSource = $dataW;
			parent::addControl($listW2);
			
			/*====================== 小廣告 =======================*/
			$aryP = [];
			$dataP = $dalP->executeRecordset([], "a.`PictureType`=20", "a.`TypeOrderNO`");
			$num1 = 0;
			foreach ($dataP as $rows) {
				if (!empty($rows["Image"]) && parent::image_exists($rows["Image"])) {
					if (empty($rows["Link"])) $rows["Link"] = "javascript:void(0);";
					$rows["DanDesc"] = str_replace(PHP_EOL, "<br />", $rows["DanDesc"]);
					$aryP[] = $rows;
				}
			}
			$listP = new repeater();
			$listP->id = "listP";
			$listP->dataSource = $aryP;
			parent::addControl($listP);
		}
	}
?>