<?php
	require_once "pageBase.php";
	require_once "bin/system/conver.php";
	require_once "bin/web/repeater.php";
	require_once "dal/pictureDAL.php";
	require_once "dal/customerDAL.php";
	require_once "Facebook/autoload.php";
	require_once "Facebook/Facebook.php";

	class _memberlogin extends pageBase{
		public function load(){
			if(isset($_SESSION["CustomerID"])){
				parent::alert2("您已經登入成功，不需要再登入了","/");
			}

			if(!parent::isPostBack())
	 			$this->loadData();
 			else
 				$this->saveData();
			
			parent::load();
		}
		public function loadData(){
			//加載模板
			parent::loadTemplateFile("templates/".$this->registry->webTemplates."/memberlogin.html");
			
			//應用程式配置載入
			$fb = new Facebook\Facebook([
				'app_id' => '460847270964486',
				'app_secret' => '760b9abdc5df67d9cf92e2285c5be4e3',
				'default_graph_version' => 'v2.10'
			]);
			
			$helper = $fb->getRedirectLoginHelper();
			$permissions = ['email', 'user_likes']; // optional
			$loginUrl = $helper->getLoginUrl('http://543th.com/fbloging.php', $permissions);
			//parent::addParameter("loginUrl",$loginUrl);
			
			//橫幅廣告
			$this->picturetype = "100";

			//父類方法
			parent::loadData();
		}
		
		public function saveData(){
			$checknum = conver::toDbString(trim($_POST["Checknum"]));
			if($checknum == $_SESSION["dd_ckstr"] && isset($_SESSION["dd_ckstr"]) && $_SESSION["dd_ckstr"] != ''){
				$dal = new customerDAL();
				$LoginName = $_POST["LoginName"];
				$LoginPassword = $_POST["LoginPassword"];
				$data = $dal->executeRecordset("a.LoginName='".$LoginName."'","",0,-1);
				if(count($data) == 1){
					if($data[0]["LoginPassword"] != $LoginPassword){
						parent::alert("輸入密碼錯誤!");
					}else if($data[0]["CustomerLevel"] < 0){
						parent::alert("很抱歉，這個帳號已經被停權!如有疑問請洽客服人員。");
					}else{
						$_SESSION["CustomerID"] = $data[0]["CustomerID"];
						$_SESSION["CustomerLevel"] = $data[0]["CustomerLevel"];
						$_SESSION["LoginName"] = $data[0]["LoginName"];
						$_SESSION["CustomerName"] = $data[0]["CustomerName"];
						$_SESSION["FacebookID"] = $data[0]["FacebookID"];
						parent::alert2("登入成功!","/");
					}
				}else{
					parent::alert("沒有這個會員帳號!!");
				}
			}else{
				parent::alert("驗證碼輸入錯誤喔");
			}
		}
	}

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