<?php
	require_once "pageBase.php";
	require_once "../bin/web/repeater.php";
	require_once "../dal/memberDAL.php";
	require_once "../function/function.php";

	class _memberEdit extends pageBase {
		private string $id;
		private $dal;

		//覆蓋父類load()方法
		public function load() {
			$this->id = parent::getInput("id", "0");
			$this->dal = new memberDAL();

			$this->limitID = "22";
			if (!parent::chkUserLimit()) {
				$this->alertMsg = "您沒有使用此頁面的權限!如有疑問請洽主管理者!";
				$this->locationHref = "index.php";
			} else {
				parent::isPostBack() ? $this->saveData() : $this->loadData();
			}
			parent::loadTemplateFile(dirname(__FILE__) . "/templates/memberEdit.html");
			parent::load();
		}
		public function loadData() {
			if ($this->id !== "" && $this->id !== "0") {
				$this->dal->MemberID = $this->id;
				$this->dal->loadRecord();
				
				$this->addMemberStatusParameters();
				$checked = $this->formatDateTimeWithIP($this->dal->CheckedTime, $this->dal->CheckedIP);

				$parameters = [
					"LevelID" => $this->dal->LevelID,
					"LoginName" => $this->dal->LoginName,
					"LoginPass" => $this->dal->LoginPass,
					"MemberNick" => $this->dal->MemberNick,
					"MemberName" => $this->dal->MemberName,
					"MemberGender" => $this->dal->MemberGender,
					"Sex" => ($this->dal->MemberGender == "0") ? "小姐" : "先生",
					"MemberEmail" => $this->dal->MemberEmail,
					"MemberMobile" => $this->dal->MemberMobile,
					"MemberCityID" => $this->dal->MemberCityID,
					"MemberAreaID" => $this->dal->MemberAreaID,
					"MemberAddress" => $this->dal->MemberAddress,
					"MemberPhone" => $this->dal->MemberPhone,
					"MemberLine" => $this->dal->MemberLine,
					"CheckCode" => $this->dal->CheckCode,
					"Status" => $this->dal->Status,
					"textStatus" => $statusText,
					"created" => $this->formatDateTimeWithIP($this->dal->CreatedTime, $this->dal->CreatedIP),
					"checked" => ($checked == "") ? "-" : $checked,
					"lastLogin" => $this->formatDateTimeWithIP($this->dal->LastLoginTime, $this->dal->LastLoginIP),
					"modified" => $this->formatDateTimeWithIP($this->dal->ModifiedTime, $this->dal->ModifiedIP),
					"levelUp" => $this->formatDateTimeWithIP($this->dal->LevelUpTime, $this->dal->LevelUpIP),
					"LevelName" => $this->dal->LevelName,
					"CityName" => $this->dal->CityName,
					"AreaName" => $this->dal->AreaName,
					"ZipCode" => $this->dal->ZipCode,
					"ModifiedLoginName" => $this->dal->ModifiedLoginName,
					"ModifiedUserName" => $this->dal->ModifiedUserName,
					"LevelUpLoginName" => $this->dal->LevelUpLoginName,
					"LevelUpUserName" => $this->dal->LevelUpUserName,
				];
				foreach ($parameters as $name => $value) {
					parent::addParameter($name, $value);
				}
			} else {
				$this->alertMsg = "遺失重要參數!請重新選擇!";
				$this->locationHref = "memberList.php";
			}

			parent::addParameter("id", $this->id);
			parent::loadData();
		}

		public function saveData() {
			if (empty($this->id) || $this->id === "0") {
				$this->alertMsg = "遺失重要參數!請重新選擇!";
				$this->goBack = "Y";
				return false;
			}
    
			$this->dal->MemberID = $this->id;
			$this->dal->loadRecord();
    
			$validStatus = ["10", "0", "-10"];
			$status = parent::getPost("Status", "");
			if(!in_array($status, $validStatus)){
				$this->alertMsg = "請選擇會員狀態!";
				$this->goBack = "Y";
				return false;
			}
    
			$this->dal->ModifiedTime = time();
			$this->dal->ModifiedIP = parent::getIP();
			$this->dal->ModifiedUserID = parent::getCurrentUserID();
			
			if ($this->dal->Status == "10") {
				if ($status == "-10") {
					$this->dal->Status = "-10";
				} else {
					$this->alertMsg = "會員狀態不能退回待驗證!";
					$this->goBack = "Y";
					return false;
				}
			} else if ($this->dal->Status == "-10") {
				if ($status != "-10") {
					$this->locationHref = "memberList.php";
					return false;
				}
			} else if ($status == "0") {
				$this->locationHref = "memberList.php";
				return false;
			} else {
				$this->dal->Status = $status;
			}
    
			$saved = $this->dal->updateRecord();
			
			if($saved === false){
				$this->alertMsg = "儲存時發生錯誤!";
				$this->goBack = "Y";
			}else{
				$this->locationHref = "memberList.php";
			}
		}

		// 添加會員狀態
		private function addMemberStatusParameters(){
			$statusText = "待驗證";
			if ($this->dal->Status == "-1") {
				$statusText .= "(失敗一次)";
			} else if ($this->dal->Status == "-2") {
				$statusText .= "(失敗兩次)";
			}
		}

		// 格式化日期和IP的函數
		private function formatDateTimeWithIP($timestamp, $ip) {
			$formattedTime = $timestamp ? date("Y-m-d H:i:s", $timestamp) : "";
			$formattedIP = $ip ? "($ip)" : "";
			return $formattedTime . $formattedIP;
		}
	}

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