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

			$this->limitID = "66";
			if (!parent::chkUserLimit()) {
				$this->alertMsg = "您沒有使用此頁面的權限!如有疑問請洽主管理者!";
				$this->locationHref = "index.php";
			} else {
				parent::isPostBack() ? $this->saveData() : $this->loadData();
			}
			parent::loadTemplateFile(dirname(__FILE__) . "/templates/small_pictureSet.html");
			parent::load();
		}

		public function loadData() {
			parent::addParameter("smallPictureShow", registryDAL::getRegistryValue("smallPictureShow"));
			
			$dataP = $this->dal->executeRecordset([], "a.`PictureType`=20");
			foreach ($dataP as $key => &$item) {
				$item["NO"] = ++$key;
			}
			$listP = new repeater();
			$listP->id = "listP";
			$listP->dataSource = $dataP;
			parent::addControl($listP);

			$add_btn_class = count($dataP) > 0 ? "d_flex" : "d_none";
			$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::loadData();
		}

		public function saveData() {
			$arySP = parent::getPostArray("smallPictureShow");
			$smallPictureShow = $arySP !== null ? implode(",", $arySP) : "";
			registryDAL::saveRegistry("smallPictureShow", $smallPictureShow);
			
			$aryI = parent::getPostArray("Image");
			$aryT = parent::getPostArray("Title");
			$aryL = parent::getPostArray("Link");
			$aryD = parent::getPostArray("DanDesc");
			if (count($aryI) !== count($aryT) || count($aryI) !== count($aryL) || count($aryI) !== count($aryD)) {
				$this->alertMsg = "參數異常!";
				$this->goBack = "Y";
				return;
			}
		
			$this->dal->deleteTypeRecord(20);
			$this->dal->PictureType = 20;
			$this->dal->TypeOrderNO = 0;
			foreach ($aryI as $i => $image) {
				$this->dal->TypeOrderNO++;
				$this->dal->Image = $image;
				$this->dal->Title = $aryT[$i];
				$this->dal->Link = $aryL[$i];
				$this->dal->BanColor = "";
				$this->dal->DanDesc = $aryD[$i];
				$this->dal->ShowType = 1;
				$this->dal->PictureVideo = "";
				$this->dal->insertRecord();
			}
			
			$this->alertMsg = "儲存完畢!";
			$this->locationHref = "small_pictureSet.php";
		}
	}

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