<?php
	require_once "pageBase.php";
	require_once "../dal/registryDAL.php";
	class _outsideSet extends pageBase {
		public function load() {
			$this->limitID = "60";
			if (!parent::chkUserLimit()) {
				$this->alertMsg = "您沒有使用此頁面的權限!如有疑問請洽主管理者!";
				$this->locationHref = "index.php";
			} else {
				parent::isPostBack() ? $this->saveData() : $this->loadData();
			}
			parent::loadTemplateFile(dirname(__FILE__) . "/templates/outsideSet.html");
			parent::load();
		}
		
		public function loadData() {
			$webConfig = registryDAL::getRegistryValues();
			foreach ($webConfig as $key => &$value) {
				$value = $value ?? "";
				parent::addParameter($key, $value);
			}
			parent::loadData();
		}
		
		public function saveData() {
			$sendMailType = parent::getPost("sendMailType");
			$aryR = [
				"sendMailType" => $sendMailType,
				"sendMailAddress" => "",
				"sendMailPassword" => "",
				"sendMailHost" => "",
				"sendMailPort" => "",
				"sendMailSMTPSecure" => "",
				"sendMailSMTPAuth" => "",
				"headHTML" => parent::getPost("headHTML", "", 3),
				"bodyTopHTML" => parent::getPost("bodyTopHTML", "", 3),
				"ModifiedTime" => time(),
			];

			if ($sendMailType != "1" && $sendMailType != "5") {
				$aryR["sendMailAddress"] = parent::getPost("sendMailAddress");
				$aryR["sendMailPassword"] = parent::getPost("sendMailPassword", "", 3);
				$aryR["sendMailHost"] = parent::getPost("sendMailHost");
				$aryR["sendMailPort"] = parent::getPost("sendMailPort");
				$aryR["sendMailSMTPSecure"] = parent::getPost("sendMailSMTPSecure");
				$aryR["sendMailSMTPAuth"] = parent::getPost("sendMailSMTPAuth");
			}
			
			foreach ($aryR as $key => $value) {
				registryDAL::saveRegistry($key, $value);
			}
			
			$this->alertMsg = "儲存完畢!";
			$this->locationHref = "outsideSet.php";
		}
	}

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