<?php
	require_once "pageBase.php";
	require_once "../dal/registryDAL.php";
	class _indexSet extends pageBase {
		public function load() {
			$this->limitID = "50";
			if (!parent::chkUserLimit()) {
				$this->alertMsg = "您沒有使用此頁面的權限!如有疑問請洽主管理者!";
				$this->locationHref = "index.php";
			} else {
				parent::isPostBack() ? $this->saveData() : $this->loadData();
			}
			parent::loadTemplateFile(dirname(__FILE__) . "/templates/indexSet.html");
			parent::load();
		}
		
		public function loadData() {
			$webConfig = registryDAL::getRegistryValues();
			foreach ($webConfig as $key => &$value) {
				$value = $value ?? "";
				if (in_array($key, ["indexNNO", "indexPNO", "indexBNO"]) && $value == -1) $value = "";
				parent::addParameter($key, $value);
			}
			
			parent::loadData();
		}
		
		public function saveData() {
			$aryR = [
				// 關於我們
				"indexAboutTitle1" => parent::getPost("indexAboutTitle1"),
				"indexAboutTitle2" => parent::getPost("indexAboutTitle2"),
				"indexAboutLink" => parent::getPost("indexAboutLink"),
				"indexAboutLinkT" => parent::getPost("indexAboutLinkT"),
				"indexAboutIntroduction" => parent::getPost("indexAboutIntroduction", "", 2),
				"indexAboutImage" => parent::getPost("indexAboutImage"),
				"indexAboutShow" => parent::getPost("indexAboutShow"),
				// 最新消息
				"indexNTitle" => parent::getPost("indexNTitle"),
				"indexNNO" => parent::getPost("indexNNO", "-1"),
				"indexNMLink" => parent::getPost("indexNMLink"),
				"indexNMLinkTitle" => parent::getPost("indexNMLinkTitle"),
				"indexNBImg" => parent::getPost("indexNBImg"),
				"indexNBColor" => parent::getPost("indexNBColor"),
				"indexNShow" => parent::getPost("indexNShow"),
				// 產品設置
				"indexPTitle" => parent::getPost("indexPTitle"),
				"indexPNO" => parent::getPost("indexPNO", "-1"),
				"indexPMLink" => parent::getPost("indexPMLink"),
				"indexPMLinkTitle" => parent::getPost("indexPMLinkTitle"),
				"indexPShow" => parent::getPost("indexPShow"),
				"indexPBImg" => parent::getPost("indexPBImg"),
				"indexPBColor" => parent::getPost("indexPBColor"),
				// 相本設置
				"indexBTitle" => parent::getPost("indexBTitle"),
				"indexBNO" => parent::getPost("indexBNO", "-1"),
				"indexBMLink" => parent::getPost("indexBMLink"),
				"indexBMLinkTitle" => parent::getPost("indexBMLinkTitle"),
				"indexBShow" => parent::getPost("indexBShow"),
				"indexBBImg" => parent::getPost("indexBBImg"),
				"indexBBColor" => parent::getPost("indexBBColor"),
				"ModifiedTime" => time(),
			];

			foreach ($aryR as $key => $value) {
				registryDAL::saveRegistry($key, $value);
			}
			$this->alertMsg = "儲存完畢!";
			$this->locationHref = "indexSet.php";
		}
	}

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