<?php
	require_once "pageBase.php";
	require_once "bin/web/tables/sends.php";
	require_once "bin/web/repeater.php";
	require_once "dal/cityDAL.php";
	require_once "dal/memberDAL.php";
	require_once "dal/pictureDAL.php";
	class _new_member2 extends pageBase {
		private $message;
		public function load() {
			if ($this->registry->isCustomer == 0) {
				header("Location: /404.php");
				exit();
			}

			$this->message = "會員註冊成功!請到信箱收取通知信，填入驗證碼完成註冊!";
			$this->loadData();
			parent::addParameter("message", $this->message);
			parent::loadTemplateFile("templates/{$this->registry->webTemplates}/new_member2.html");
			parent::load();
		}
		
		public function loadData() {
			//橫幅廣告
			$this->picturetype = "130";

			//logo
			$logo = parent::getLogo();

			/* 參數 */
			$dal = new memberDAL();
			$checknum = parent::getPost("Checknum");
			$params = [
				"Email" => parent::getPost("Email"),
				"Pass" => parent::getPost("Pass"),
				"Nick" => parent::getPost("Nick"),
				"Name" => parent::getPost("Name"),
				"Sex" => parent::getPost("Sex"),
				"Mobile" => parent::getPost("Mobile"),
				"City" => parent::getPost("City"),
				"Area" => parent::getPost("Area"),
				"Address" => parent::getPost("Address"),
				"Phone" => parent::getPost("Phone"),
				"Line" => parent::getPost("Line")
			];

			foreach ($params as $key => $value) {
				$_SESSION["newmember_" . strtolower($key)] = $value;
				$$key = $value; // 將參數轉換為變數
			}

			if ($checknum == $_SESSION["dd_ckstr"] && isset($_SESSION["dd_ckstr"]) && $_SESSION["dd_ckstr"] != '') {
				$requiredFields = [
					"email" => "信箱",
					"pass" => "密碼",
					"nick" => "暱稱",
					"name" => "姓名",
					"mobile" => "手機",
					"city" => "縣市",
					"area" => "鄉鎮市區",
					"address" => "住址"
				];

				foreach ($requiredFields as $field => $fieldName) {
					if ($$field == '') {
						$txt = ($field == "sex" || $field == "city" || $field == "area") ? "請選擇" : "請輸入";
						$this->handleError($field, $txt . $fieldName . "!");
						return;
					}
				}

				if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
					$this->handleError("email", "信箱格式錯誤!");
					return;
				}
				$intoAry = [parent::addArray(":loginName", $email, "str", 200)];
				$filter = "a.`LoginName`=:loginName";
				if ($dal->countRecordset($intoAry, $filter) > 0) {
					$this->handleError("email", "信箱已註冊!請直接登入!", "member_login.html");
					return;
				}
				if (!preg_match("/^\S{5,20}$/", $pass)) {
					$this->handleError("pass", "密碼格式錯誤!");
					return;
				}
				$pass2 = hash("sha256", $pass);
				if ($sex != "0" && $sex != "1") {
					$this->handleError("sex", "請選擇性別!");
					return;
				}
				if (!preg_match("/^09\d{8}$/", $mobile)) {
					$this->handleError("mobile", "手機格式錯誤!");
					return;
				}
				if ($city == "0" || !preg_match("/^\d+$/", $city)) {
					$this->handleError("city", "請選擇縣市!");
					return;
				}
				if ($area == "0" || !preg_match("/^\d+$/", $area)) {
					$this->handleError("area", "請選擇鄉鎮市區!");
					return;
				}
				$checkCode = chr(rand(65, 90)) . chr(rand(65, 90)) . chr(rand(65, 90)) . chr(rand(65, 90)) . chr(rand(65, 90));
				
				$dal->LoginName = $email;
				$dal->LoginPass = hash("sha256", $pass);
				$dal->MemberNick = $nick;
				$dal->MemberName = $name;
				$dal->MemberGender = $sex;
				$dal->MemberEmail = $email;
				$dal->MemberMobile = $mobile;
				$dal->MemberCityID = $city;
				$dal->MemberAreaID = $area;
				$dal->MemberAddress = $address;
				$dal->MemberPhone = $phone;
				$dal->MemberLine = $line;
				$dal->CheckCode = $checkCode;
				$dal->CreatedIP = parent::getIP();
				$dal->ModifiedIP = parent::getIP();
				$dalC = new cityDAL();
				$dalC->CityID = $city;
				$dalC->loadRecord();

				// 通知信
				$sends = new sends();
				$mbody = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
				$mbody .= '<html xmlns="https://www.w3.org/1999/xhtml">';
				$mbody .= '<head>';
				$mbody .= '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
				$mbody .= '</head>';
				$mbody .= '<body>';
				$mbody .= '<p>會員註冊通知信</p>';
				$mbody .= '<p>驗證碼：「' . $checkCode . '」</p>';
				$mbody .= '<p>以下為註冊時填寫的會員資料</p>';
				$mbody .= '<p>暱稱：' . $nick . '</p>';
				$mbody .= '<p>姓名：' . mb_substr($name, 0, 1, "utf-8");
				for ($i = 1; $i < mb_strlen($name); $i++) $mbody .= '*';
				$mbody .= ($sex == '1') ? '先生' : '小姐';
				$mbody .= '</p>';
				$mbody .= '<p>手機：'.mb_substr($mobile, 0, 2, "utf-8");
				for($i=4;$i<mb_strlen($mobile);$i++) $mbody .= '*';
				$mbody .= mb_substr($mobile, -2, 2, "utf-8").'</p>';
				$mbody .= '<p>住址：'.$dalC->CityName.'******</p>';
				if ($phone != "") {
					$mbody .= '<p>電話：' . mb_substr($phone, 0, 2, "utf-8");
					for ($i = 4; $i < mb_strlen($phone); $i++) $mbody .= '*';
					$mbody .= mb_substr($phone, -2, 2, "utf-8").'</p>';
				}
				if ($line != "") $mbody .= '<p>Line ID：******</p>';
				$mbody .= '</body>';
				$mbody .= '</html>';

				$subject = "[" . $this->registry->webName . "][" . $_SERVER['HTTP_HOST'] . "]會員註冊通知信";

				if (!$dal->insertRecord()) {
					$this->message = "註冊失敗!";
					$this->alertMsg = "會員註冊失敗!";
					$this->locationHref = "new_member1.html";
					return;
				} else {
					unset($_SESSION["newmember_email"]);
					unset($_SESSION["newmember_pass"]);
					unset($_SESSION["newmember_nick"]);
					unset($_SESSION["newmember_name"]);
					unset($_SESSION["newmember_sex"]);
					unset($_SESSION["newmember_mobile"]);
					unset($_SESSION["newmember_city"]);
					unset($_SESSION["newmember_area"]);
					unset($_SESSION["newmember_address"]);
					unset($_SESSION["newmember_phone"]);
					unset($_SESSION["newmember_line"]);
					if (empty($this->registry->sendMailType)) {
						$sends->sendemail($this->registry->webName, $email, $subject, $mbody, $email);
					} else {
						$toAry = [$email];
						$sends->sendsmtpmail($subject, $mbody, $toAry);
					}
					parent::addParameter("email", $email);
				}
			} else {
				$this->message = "註冊失敗!";
				$this->alertMsg = "驗證碼輸入錯誤喔";
				$this->locationHref = "new_member1.html";
			}
		}

		public function handleError($field, $msg, $link = "") {
			unset($_SESSION["newmember_" . strtolower($field)]);
			$this->message = "註冊失敗!";
			$this->alertMsg = $msg;
			$this->locationHref = (empty($link)) ? "new_member1.html" : $link;
		}
	}

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