<?php
	require_once "pageBase.php";
	require_once "dal/customerDAL.php";
	require_once "Facebook/autoload.php";
	require_once "Facebook/Facebook.php";
	class _memberlogin extends pageBase
	{
		public function load(){
			if(!parent::isPostBack())
	 			$this->loadData();
 			else
 				$this->saveData();
			
			parent::load();
		}

		public function loadData(){
			$dal = new customerDAL();
			$dalCOP = new car_order_productDAL();

			//應用程式配置載入
			$fb = new Facebook\Facebook([
				'app_id' => '460847270964486',
				'app_secret' => '760b9abdc5df67d9cf92e2285c5be4e3',
				'default_graph_version' => 'v2.10'
			]);
			
			
			$helper = $fb->getRedirectLoginHelper();
			$_SESSION['FBRLH_state']=$_GET['state'];
			try {
				$accessToken = $helper->getAccessToken();
			} catch(Facebook\Exceptions\FacebookResponseException $e) {
				// When Graph returns an error
				echo 'Graph returned an error: ' . $e->getMessage();
				exit;
			} catch(Facebook\Exceptions\FacebookSDKException $e) {
				// When validation fails or other local issues
				echo 'Facebook SDK returned an error: ' . $e->getMessage();
				exit;
			}
			
			if (isset($accessToken)) {
				// Logged in!
				$_SESSION['facebook_access_token'] = (string) $accessToken;				
				// Now you can redirect to another page and use the
				// access token from $_SESSION['facebook_access_token']
			}
			
			$helper = $fb->getJavaScriptHelper();
			try {
				$accessToken = $helper->getAccessToken();
			} catch(Facebook\Exceptions\FacebookResponseException $e) {
				// When Graph returns an error
				echo 'Graph returned an error: ' . $e->getMessage();
				exit;
			} catch(Facebook\Exceptions\FacebookSDKException $e) {
				// When validation fails or other local issues
				echo 'Facebook SDK2 returned an error: ' . $e->getMessage();
				exit;
			}
			
			if (isset($accessToken)) {
				// Logged in
			}
			
			$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
			
			try {
				$response = $fb->get('/me');
				$userNode = $response->getGraphUser();
			} catch(Facebook\Exceptions\FacebookResponseException $e) {
				// When Graph returns an error
				echo 'Graph returned an error: ' . $e->getMessage();
				exit;
			} catch(Facebook\Exceptions\FacebookSDKException $e) {
				// When validation fails or other local issues
				echo 'Facebook SDK3 returned an error: ' . $e->getMessage();
				exit;
			}
			
			$FacebookID = $userNode->getId();
			$FacebookEmail = $userNode->getEmail();
			$FacebookName = $userNode->getName();
			
			$data = $dal->executeRecordset("a.FacebookID='".$FacebookID."'","",0,-1);
			if(count($data) == 1){
				$_SESSION["customerID"] = $data[0]["CustomerID"];
				if($data[0]["CustomerEmail"] == ""){
					parent::alert2("登入成功!您尚未填寫完整資料!!","member.html");
				}else{
					if((int)$data[0]["CustomerLevel"] == -1){
						parent::alert2("這個帳號已經被停權!!\r\n如有疑問請和客服人員聯絡","memberlogin.html");
					}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{
				$_SESSION["FacebookID"] = $FacebookID;
				$_SESSION["LoginName"] = $FacebookEmail;
				$_SESSION["CustomerName"] = $FacebookName;				
				parent::alert2("登入成功，請填寫詳細會員資訊!","member.html");
			}
			
			//父類方法蘋果也界有限公司
			parent::loadData();
		}
	}

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