<?php
	require_once "../pageBase.php";
	require_once "../dal/commentDAL.php";
	require_once "../dal/customerDAL.php";
	class _comment extends page{
		private $aryJ;
		public function load(){
			$action = htmlentities(trim($_POST["do"]));
			$action = strtolower($action);
			$this->aryJ = array();
			
			switch($action){
				case "com":
					$this->com();
					break;
				default:
					$this->aryJ["Status"] = "1";
					$this->aryJ["Message"] = urlencode("系統異常!請重新整理再試一次!!");
					break;
			}
			$txtJ = urldecode(json_encode($this->aryJ));
			echo $txtJ;

			parent::load();
		}
		public function com(){
			$dal = new commentDAL();
			$dalC = new customerDAL();
			$id = htmlentities(trim($_POST["id"]));
			$stars = htmlentities(trim($_POST["stars"]));
			$cont = htmlentities(trim($_POST["cont"]));
			$check = htmlentities(trim($_POST["check"]));
			if($id == "" || $id == "0"){
				$this->aryJ["Status"] = "1";
				$this->aryJ["Message"] = urlencode("系統異常!!請重新整理再試一次!");
			}else{
				if(isset($_SESSION["CustomerID"])){
					$dalC->CustomerID = $_SESSION["CustomerID"];
					$dalC->loadRecord();
					if($dalC->CustomerLevel == "0"){
						if($stars == ""){
							$this->aryJ["Status"] = "1";
							$this->aryJ["Message"] = urlencode("請選擇星級!!");
							exit;
						}
						if($cont == ""){
							$this->aryJ["Status"] = "1";
							$this->aryJ["Message"] = urlencode("請輸入評論內容!!");
							exit;
						}
						if($check == $_SESSION["dd_ckstr"] && isset($_SESSION["dd_ckstr"]) && $_SESSION["dd_ckstr"] != ""){
							$dal->CustomerID = $_SESSION["CustomerID"];
							$dal->ProductID = $id;
							$dal->Stars = $stars;
							$dal->Content = $cont;
							$dal->Visible = "1";
							$dal->CreatedTime = date("Y-m-d H:i:s");
							if(!empty($_SERVER["HTTP_CLIENT_IP"])){
								$ip = $_SERVER["HTTP_CLIENT_IP"];
							}else if(!empty($_SERVER["HTTP_X_FORWARDED_FOR"])){
								$ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
							}else{
								$ip = $_SERVER["REMOTE_ADDR"];
							}						
							$dal->CreatedIP = $ip;
							if(!$dal->insertRecord()){
								$this->aryJ["Status"] = "1";
								$this->aryJ["Message"] = urlencode("註冊時發生無法預期的錯誤!請再試一次!");
							}else{
								$this->aryJ["Status"] = "0";
								$this->aryJ["Message"] = urlencode("評論與評分成功!!");
							}
						}else{
							$this->aryJ["Status"] = "1";
							$this->aryJ["Message"] = urlencode("驗證碼輸入錯誤!!");
						}
					}else{
						unset($_SESSION["CustomerID"]);
						unset($_SESSION["CustomerLevel"]);
						unset($_SESSION["LoginName"]);
						unset($_SESSION["CustomerName"]);
						unset($_SESSION["FacebookID"]);
						$this->aryJ["Status"] = "2";
						$this->aryJ["Message"] = urlencode("很抱歉，這個帳號已經被停權!如有疑問請洽客服人員。");
					}
				}else{
					$this->aryJ["Status"] = "2";
					$this->aryJ["Message"] = urlencode("您尚未登入!");
				}
			}
		}
	}

	$page = new _comment();
	$page->load();
?>