<?php
	require_once "pageBase.php";
	require_once "dal/memberDAL.php";
	class _check_member extends pageBase {
		private $dal;
		public function load() {
			if ($this->registry->isCustomer == 0) {
				header("Location: /404.php");
				exit();
			}
		
			$this->dal = new memberDAL();
    
			if (isset($_SESSION["member_id"]) && $_SESSION["member_id"] != "") {
				$this->dal->MemberID = $_SESSION["member_id"];
				$this->dal->loadRecord();

				if ((int)$this->dal->Status > 0) {
					parent::isPostBack() ? $this->saveData() : $this->loadData();
				} elseif ((int)$this->dal->Status < -2) {
					unset($_SESSION["member_id"]);
					$this->alertMsg = "您已遭停權，如有疑問請和客服人員聯絡!";
					$this->locationHref = "/";
				} else {
					unset($_SESSION["member_id"]);
					$this->alertMsg = "您尚未通過驗證，請先完成驗證再登入一次!";
					$this->locationHref = "check_member.html";
				}
			} else {
				$this->alertMsg = "您尚未登入! 請先登入!";
				$this->locationHref = "member_login.html";
			}
			
			parent::loadTemplateFile("templates/{$this->registry->webTemplates}/check_member.html");
			parent::load();
		}
		
		public function loadData() {
			// 橫幅廣告
			$this->picturetype = "130";
			
			parent::loadData();
		}

		private function saveData() {
			/* 參數 */
			$login = parent::getPost("login");
			$code = parent::getPost("code");
			$ip = parent::getIP();

			// 檢查驗證
			$data = $this->findMemberData($login);

			if (count($data) === 0) {
				$this->alertMsg = "找不到驗證的帳號資料!";
				$this->goBack = "Y";
				return;
			}

			$status = (int)$data[0]["Status"];
		
			if ($status > 0) {
				$this->alertMsg = "此帳號已經完成驗證!請勿重複驗證!";
				$this->goBack = "Y";
				return;
			} elseif ($status < -2) {
				$this->alertMsg = "此帳號已遭停權!如有疑問請和客服人員聯絡!";
				$this->goBack = "Y";
				return;
			}

			if ($data[0]["CheckCode"] === $code) {
				/* 驗證成功 */
				if ($this->performValidationSuccess($data[0]["MemberID"], $ip)) {
					$this->locationHref = "member_login.html";
				} else {
					$this->alertMsg = "驗證失敗!請重新整理頁面再試一次!或和客服人員聯絡!";
					$this->goBack = "Y";
				}
			} else {
				/* 驗證失敗 */
				$status = $status - 1;
				if ($status < -2) $status = -10;
		
				if ($this->performValidationFailure($data[0]["MemberID"], $ip, $status)) {
					$this->alertMsg = "驗證碼錯誤!請注意!驗證碼皆為大寫英文字母!";
					$this->goBack = "Y";
				} else {
					$this->alertMsg = "驗證失敗!請重新整理頁面再試一次!或和客服人員聯絡!";
					$this->goBack = "Y";
				}
			}
		}

		private function findMemberData($login) {
			$intoAry = [parent::addArray(":loginName", $login, "str", 200)];
			$filter = "a.`LoginName`=:loginName";
			$orderExp = "a.`MemberID`";
			return $this->dal->executeRecordset($intoAry, $filter, $orderExp, 0, 1);
		}
		
		private function performValidationSuccess($memberID, $ip) {
			$this->dal->MemberID = $memberID;
			$this->dal->Status = 10;
			$this->dal->CheckedIP = $ip;
			return $this->dal->checkedRecord();
		}
		
		private function performValidationFailure($memberID, $ip, $status) {
			$this->dal->MemberID = $memberID;
			$this->dal->Status = $status;
			$this->dal->CheckedIP = $ip;
			return $this->dal->checkedRecord();
		}
	}

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