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

			$this->limitID = "48";
			if (!parent::chkUserLimit()) {
				$this->alertMsg = "您沒有使用此頁面的權限!如有疑問請洽主管理者!";
				$this->locationHref = "index.php";
			} else {
				parent::isPostBack() ? $this->saveData() : $this->loadData();
			}
			parent::loadTemplateFile(dirname(__FILE__) . "/templates/extrainfoSet.html");
			parent::load();
		}
		
		public function loadData() {
			$data = $this->dal->executeRecordset();
			foreach ($data as &$item) {
				$item["TypeStyle"] = ($item["Type"] == "1" || $item["Type"] == "3" || $item["Type"] == "4") ? "" : " style='display:none;'";
				$item["ImgStyle"] = ($item["Type"] == "1" || $item["Type"] == "3" || $item["Type"] == "4") ? " display:none;" : "";
				$item["FontStyle"] = ($item["Type"] == "1" || $item["Type"] == "3" || $item["Type"] == "4") ? " style='display:none;'" : "";
		
				if (empty($item["Icon"]) || !parent::image_exists($item["Icon"])) {
					$item["Icon2"] = "/images/no-pic.jpg";
					$item["Icon3"] = "javascript:void(0);";
				} else {
					$item["Icon2"] = $item["Icon"];
					$item["Icon3"] = $item["Icon"];
				}
			}
			$list = new repeater();
			$list->id = "list";
			$list->dataSource = $data;
			parent::addControl($list);

			parent::loadData();
		}

		public function saveData() {
			$name = parent::getPostArray("Name");
			$value = parent::getPostArray("Value");
			$link = parent::getPostArray("Link");
			$icon = parent::getPostArray("Icon");
			$type = parent::getPostArray("Type");
			$visible = parent::getPostArray("Visible");
			$isShow = parent::getPostArray("IsShow");
			
			$count = count($name);

			if ($count !== count($value) || $count !== count($link) || $count !== count($icon) || $count !== count($type) || $count !== count($visible) || $count !== count($isShow)) {
				$this->alertMsg = "資料傳輸異常!請重新整理再試一次!";
				$this->locationHref = "extrainfoSet.php";
				return;
			}

			$this->dal->deleteRecord();
			for ($i = 0; $i < $count; $i++) {
				$this->dal->ID = ($i + 1);
				$this->dal->Name = $name[$i];
				$this->dal->Value = $value[$i];
				$this->dal->Link = $link[$i];
				$this->dal->Icon = $icon[$i];
				$this->dal->Type = $type[$i];
				$this->dal->Visible = $visible[$i];
				$this->dal->IsShow = $isShow[$i];

				if (preg_match("/^[1-8]$/", $this->dal->Type) && preg_match("/^[0-1]$/", $this->dal->Visible)) $this->dal->insertRecord();
			}

			$this->alertMsg = "儲存完畢!";
			$this->locationHref = "extrainfoSet.php";
		}
	}

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