<?php
	require_once "pageBase.php";
	require_once "../dal/registryDAL.php";
	class _apiSet extends pageBase {
		private string $isPayPage;
		private string $istwsms;
		public function load() {
			$this->limitID = "52";
			if (!parent::chkUserLimit()) {
				$this->alertMsg = "您沒有使用此頁面的權限!如有疑問請洽主管理者!";
				$this->locationHref = "index.php";
			} else {
				$this->isPayPage = registryDAL::getRegistryValue("isPayPage");
				$this->istwsms = registryDAL::getRegistryValue("istwsms");

				if ($this->isPayPage == 0 && $this->istwsms == 0) {
					$this->alertMsg = "尚未開啟第三方串接的服務!如需使用請前往設定!";
					$this->locationHref = "advancedSet.php";
				} else {
					$this->limitID = "52";
					parent::isPostBack() ? $this->saveData() : $this->loadData();
				}
			}
			parent::loadTemplateFile(dirname(__FILE__) . "/templates/apiSet.html");
			parent::load();
		}
		
		public function loadData() {
			$configKeys = [ "merchantID", "hashKey", "hashIV", "ecpay_merchantID", "ecpay_hashKey", "ecpay_hashIV", "pchomepay_appId", "pchomepay_secretCode", "twsmsuaer", "twsmspass" ];

			$settings = [];
			foreach ($configKeys as $key) {
				$value = registryDAL::getRegistryValue($key);
				$settings[$key] = $value;
			}
			
			$aryT = [];
			$aryH = [];
			//金流設置
			$payTypes = [ 1 => "藍新金流設定", 2 => "綠界金流設定", 3 => "PCHome金流設定" ];

			if (isset($payTypes[$this->isPayPage])) {
				$payname = $payTypes[$this->isPayPage];
				$payhtml = "";
		
				if ($this->isPayPage == 1) { // 藍新
					$payhtml .= <<<HTML
					<div class="d_flex form_list"><label>商店代號</label><input type="text" id="merchantID" name="merchantID" value="{$settings['merchantID']}" /></div>
					<div class="d_flex form_list"><label>HashKey</label><input type="text" id="hashKey" name="hashKey" value="{$settings['hashKey']}" /></div>
					<div class="d_flex form_list"><label>HashIV</label><input type="text" id="hashIV" name="hashIV" value="{$settings['hashIV']}" /></div>
HTML;
				} else if ($this->isPayPage == 2) { // 綠界
					$payhtml .= <<<HTML
					<div class="d_flex form_list"><label>商店代號</label><input type="text" id="ecpay_merchantID" name="ecpay_merchantID" value="{$settings['ecpay_merchantID']}" /></div>
					<div class="d_flex form_list"><label>HashKey</label><input type="text" id="ecpay_hashKey" name="ecpay_hashKey" value="{$settings['ecpay_hashKey']}" /></div>
					<div class="d_flex form_list"><label>HashIV</label><input type="text" id="ecpay_hashIV" name="ecpay_hashIV" value="{$settings['ecpay_hashIV']}" /></div>
HTML;
				} else if ($this->isPayPage == 3) { // PCHome
					$payhtml .= <<<HTML
					<div class="d_flex form_list"><label>App Id</label><input type="text" id="pchomepay_appId" name="pchomepay_appId" value="{$settings['pchomepay_appId']}" /></div>
					<div class="d_flex form_list"><label>Secret Code</label><input type="text" id="pchomepay_secretCode" name="pchomepay_secretCode" value="{$settings['pchomepay_secretCode']}" /></div>
HTML;
				}
		
				$nextPage = ($this->istwsms > 0) ? "<div class=\"d_flex form_btn\"><a class=\"ml_2 btn btn_tertiary\" data-action=\"nextPage\" href=\"javascript:void(0);\">下一步</a></div>" : "";
				$aryT[] = ["id" => "payset", "data" => " data-action=\"true\"", "name" => $payname];
				$aryH[] = ["html_box" => "<div id=\"payset\" class=\"tab_content\" data-action=\"true\">{$payhtml}{$nextPage}</div>"];
			}
			
			//簡訊設定
			if ($this->istwsms == 1) { // 台灣簡訊
				$upPage = ($this->isPayPage > 0) ? "<div class=\"d_flex form_btn\"><a class=\"ml_2 btn btn_orange\" data-action=\"upPage\" href=\"javascript:void(0);\">上一步</a></div>" : "";
				$data = ($this->isPayPage == 0) ? " data-action=\"true\"" : "";
				$smshtml = <<<HTML
				<div class="d_flex form_list"><label>帳號</label><input type="text" id="twsmsuaer" name="twsmsuaer" value="{$settings['twsmsuaer']}" /></div>
				<div class="d_flex form_list"><label>密碼</label><input type="text" id="twsmspass" name="twsmspass" value="{$settings['twsmspass']}" /></div>
HTML;
		
				$aryT[] = ["id" => "twsms", "data" => $data, "name" => "台灣簡訊"];
				$aryH[] = ["html_box" => "<div id=\"twsms\" class=\"tab_content\"{$data}>{$smshtml}{$upPage}</div>"];
			}
			
			$listT = new repeater();
			$listT->id = "listT";
			$listT->dataSource = $aryT;
			parent::addControl($listT);
			$listH = new repeater();
			$listH->id = "listH";
			$listH->dataSource = $aryH;
			parent::addControl($listH);
			
			parent::loadData();
		}

		public function saveData() {
			$aryR = [];

			if ($this->isPayPage == 1) { // 藍新
				$aryR["merchantID"] = parent::getPost("merchantID", "");
				$aryR["hashKey"] = parent::getPost("hashKey", "");
				$aryR["hashIV"] = parent::getPost("hashIV", "");
			} else if ($this->isPayPage == 2) { // 綠界
				$aryR["ecpay_merchantID"] = parent::getPost("ecpay_merchantID", "");
				$aryR["ecpay_hashKey"] = parent::getPost("ecpay_hashKey", "");
				$aryR["ecpay_hashIV"] = parent::getPost("ecpay_hashIV", "");
			} else if ($this->isPayPage == 3) { // PCHome
				$aryR["pchomepay_appId"] = parent::getPost("pchomepay_appId", "");
				$aryR["pchomepay_secretCode"] = parent::getPost("pchomepay_secretCode", "");
			}
			
			if ($this->istwsms == 1) { // 台灣簡訊
				$aryR["twsmsuaer"] = parent::getPost("twsmsuaer", "");
				$aryR["twsmspass"] = parent::getPost("twsmspass", "");
			}
			
			if (count($aryR) > 0) {
				foreach ($aryR as $key => $value) {
					registryDAL::saveRegistry($key, $value);
				}
			}
			$this->alertMsg = "儲存完畢!";
			$this->locationHref = "apiSet.php";
		}
	}
	$page = new _apiSet();
	$page->load();
	$page->render();
?>