<?php
	require_once "pageBase.php";
	require_once "../bin/web/repeater.php";
	require_once "../dal/pictureDAL.php";
	require_once "../dal/registryDAL.php";
	class _index_pictureSet extends pageBase {
		private $dal;
		private int $bMaximum;
		public function load() {
			$this->dal = new pictureDAL();
			$this->bMaximum = registryDAL::getRegistryValue("bannerMaximum");

			$this->limitID = "64";
			if (!parent::chkUserLimit()) {
				$this->alertMsg = "您沒有使用此頁面的權限!如有疑問請洽主管理者!";
				$this->locationHref = "index.php";
			} else {
				parent::isPostBack() ? $this->saveData() : $this->loadData();
			}
			parent::loadTemplateFile(dirname(__FILE__) . "/templates/index_pictureSet.html");
			parent::load();
		}
		
		public function loadData() {
			$filter = "a.`PictureType`=30";
			$dataP = $this->dal->executeRecordset([], $filter, "a.`TypeOrderNO`");
			foreach ($dataP as $i => &$item) {
				$item["NO"] = $i;
				$item["NO2"] = $i + 1;
				$item["ShowType"] = $item["ShowType"] ?: 1;
				$item["style"] = ($item["ShowType"] == 1) ? " style=\"display:none;\"" : "";
			}
			$listP = new repeater();
			$listP->id = "listP";
			$listP->dataSource = $dataP;
			parent::addControl($listP);
			
			$add_btn_class = (count($dataP) == 0 || count($dataP) >= $this->bMaximum) ? "d_none" : "d_flex";
			$add_btn_class2 = (count($dataP) >= $this->bMaximum) ? "d_none" : "d_flex";
			$add_btn = "<div class=\"{$add_btn_class} form_list add_btn\"><a class=\"btn btn_success\" data-action=\"add\" href=\"javascript:void(0);\">新增</a></div>";
			parent::addParameter("add_btn", $add_btn);
			parent::addParameter("add_btn_class2", $add_btn_class2);
			parent::addParameter("bannerMaximum", $this->bMaximum);

			parent::loadData();
		}

		public function saveData() {
			$aryI = parent::getPostArray("Image");
			$aryT = parent::getPostArray("Title");
			$aryL = parent::getPostArray("Link");
			$aryC = parent::getPostArray("BanColor");
			$aryD = parent::getPostArray("DanDesc");
			$aryS = parent::getPostArray("ShowType");
			$aryV = parent::getPostArray("PictureVideo");

			if (count($aryS) <= $this->bMaximum) {
				$this->dal->deleteTypeRecord(30);
				$this->dal->PictureType = 30;
				for ($i = 0; $i < count($aryS); $i++) {
					$this->dal->TypeOrderNO = $i + 1;
					$this->dal->Image = $aryI[$i];
					$this->dal->Title = $aryT[$i];
					$this->dal->Link = $aryL[$i];
					$this->dal->BanColor = $aryC[$i];
					$this->dal->DanDesc = $aryD[$i];
					$this->dal->ShowType = $aryS[$i];
					$this->dal->PictureVideo = $aryV[$i];
					$this->dal->insertRecord();
				}
				$this->alertMsg = "儲存完畢!";
				$this->locationHref = "index_pictureSet.php";
			} else {
				$this->alertMsg = "首頁Banner上限是" . $this->bMaximum . "張，如需新增請洽客服人員!";
				$this->goBack = "Y";
				return false;
			}
		}
	}

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