<?php
	require_once "pageBase.php";
	require_once "bin/system/conver.php";
	require_once "bin/system/email.php";
	require_once "bin/system/smtp_mail.php";
	require_once "bin/web/repeater.php";
	require_once "dal/contactDAL.php";
	require_once "dal/productDAL.php";
	require_once "dal/pictureDAL.php";
	class _contact extends pageBase{
		public function load(){
			if(parent::isPostBack())
				$this->saveData();
			else
				$this->loadData();
			parent::load();
		}
		public function loadData(){
			//加載模板
			parent::loadTemplateFile("templates/".$this->registry->webTemplates."/contact.html");
			
			//橫幅廣告
			$this->picturetype = "90";
			
			//聯絡我們標題
			parent::addParameter("ContactName",$this->registry->ContactName);

			//給客戶的話
			$webContact = $this->registry->webContact;
			parent::addParameter("webContact",$webContact);
			
			//網站資訊
			$isShow = 0;

			//客服信箱是否顯示
			if($this->registry->webEmail == ""){
				parent::addParameter("webEmail","");
				parent::addParameter("webEmailStyle"," style='display:none;'");
			}else{
				if($this->registry->webEmailisShow == "1"){
					$isShow = 1;
					parent::addParameter("webEmail",$this->registry->webEmail);
					parent::addParameter("webEmailStyle","");
				}else{
					parent::addParameter("webEmail","");
					parent::addParameter("webEmailStyle"," style='display:none;'");
				}
			}
			
			//聯絡地址是否顯示
			if($this->registry->webAddress == ""){
				parent::addParameter("webAddress","");
				parent::addParameter("webAddressStyle"," style='display:none;'");
				parent::addParameter("webAddressLink","javascript:void(0);");
			}else{
				if($this->registry->webAddressisShow == "1"){
					$isShow = 1;
					parent::addParameter("webAddress",$this->registry->webAddress);
					parent::addParameter("webAddressStyle","");
					if(trim($this->registry->webAddress) != ''){
						$webAddressLink = "http://maps.google.com/maps?ll=23.016519,120.21529&z=15&t=m&hl=zh-TW&gl=US&mapclient=embed&q=".urlencode(trim($this->registry->webAddress));
					}else{
						$webAddressLink = "javascript:void(0);";
					}
					parent::addParameter("webAddressLink",$webAddressLink);
				}else{
					parent::addParameter("webAddress","");
					parent::addParameter("webAddressStyle"," style='display:none;'");
					parent::addParameter("webAddressLink","javascript:void(0);");
				}
			}
			
			//聯絡電話是否顯示
			if($this->registry->webPhone == ""){
				parent::addParameter("webPhone","");
				parent::addParameter("webPhoneStyle"," style='display:none;'");
			}else{
				if($this->registry->webPhoneisShow == "1"){
					$isShow = 1;
					parent::addParameter("webPhone",$this->registry->webPhone);
					parent::addParameter("webPhoneStyle","");
				}else{
					parent::addParameter("webPhone","");
					parent::addParameter("webPhoneStyle"," style='display:none;'");
				}
			}
			
			//聯絡傳真是否顯示
			if($this->registry->webFax == ""){
				parent::addParameter("webFax","");
				parent::addParameter("webFaxStyle"," style='display:none;'");
			}else{
				if($this->registry->webFaxisShow == "1"){
					$isShow = 1;
					parent::addParameter("webFax",$this->registry->webFax);
					parent::addParameter("webFaxStyle","");
				}else{
					parent::addParameter("webFax","");
					parent::addParameter("webFaxStyle"," style='display:none;'");
				}
			}
			if($isShow == 1){
				parent::addParameter("webInfoStyle","");
			}else{
				parent::addParameter("webInfoStyle"," style='display:none;'");
			}
			
			//Google MAP
			if($this->registry->googlemapMain == "1" && trim($this->registry->googlemapAddress) != ""){
				parent::addParameter("googlemapStyle","");
				parent::addParameter("googlemapAddress",urlencode($this->registry->googlemapAddress));
			}else{
				parent::addParameter("googlemapStyle"," style='display:none;'");
				parent::addParameter("googlemapAddress","");
			}
			
			//Youbtube
			if(trim($this->registry->youtubeURL) == ""){
				parent::addParameter("youtubeStyle"," style='display:none;'");
				parent::addParameter("youtubeURL","");
			}else{
				parent::addParameter("youtubeStyle","");
				$youtubeURL = str_replace("//youtu.be/","//www.youtube.com/embed/",$this->registry->youtubeURL);
				parent::addParameter("youtubeURL",$youtubeURL);
			}

			//臉書粉絲團
			if(trim($this->registry->fbFansURL) != ''){
				if($this->registry->fbFansFriendFace == '0'){
					$facepile = 'false';
				}else{
					$facepile = 'true';
				}
				if($this->registry->fbFansCoverPhoto == '1'){
					$hidecover = 'false';
				}else{
					$hidecover = 'true';
				}
				if($this->registry->fbFansPagePost == '0'){
					$showpost = 'false';
				}else{
					$showpost = 'true';
				}
				$fbFans = '<div class="fb-page" data-href="'.$this->registry->fbFansURL.'"  data-width="1200" data-adapt-container-width="true" data-hide-cover="'.$hidecover.'" data-show-facepile="'.$facepile.'" data-show-posts="'.$showpost.'"><div class="fb-xfbml-parse-ignore"><blockquote cite="'.$this->registry->fbFansURL.'"><a href="'.$this->registry->fbFansURL.'">Facebook</a></blockquote></div></div>';
			}else{
				$fbFans = '';
			}
			parent::addParameter("fbFans",$fbFans);
			
			//網站資訊是否顯示
			$arySN = explode(",",$this->registry->sideNavShow);
			if(in_array($this->picturetype,$arySN)){
				parent::addParameter("sideNavStyle","");
			}else{
				parent::addParameter("sideNavStyle"," style='display:none;'");
			}
			
			//產品訊息
			$dalP = new productDAL();
			$id = parent::getPageParameter("id","0");
			parent::addParameter("id",$id);
			if($id != "" && $id != "0"){
				$dalP->ProductID = $id;
				$dalP->loadRecord();
				parent::addParameter("ProductName",$dalP->ProductName);
			}else{
				parent::addParameter("ProductName","");
			}

			//父類方法
			parent::loadData();
		}

		private function saveData(){
			////logo
			$dalP = new pictureDAL();
			$dalP->PictureType = "10";
			$dalP->TypeOrderNO = "0";
			$dalP->loadRecord();
			
			$checknum = conver::toDbString(trim($_POST["Checknum"]));
			if($checknum == $_SESSION["dd_ckstr"] && isset($_SESSION["dd_ckstr"]) && $_SESSION["dd_ckstr"] != ''){
				$dal = new contactDAL();
				$dal->Name = conver::toDbString(trim($_POST["Name"]));
				$dal->Phone = conver::toDbString(trim($_POST["Phone"]));
				$dal->Email = conver::toDbString(trim($_POST["Email"]));
				$dal->ProductID = conver::toDbString(trim($_POST["id"]));
				$dal->ProductName = conver::toDbString(trim($_POST["ProductName"]));
				$dal->Mobile = conver::toDbString(trim($_POST["Mobile"]));
				$dal->Company = conver::toDbString(trim($_POST["Company"]));
				$dal->Address = conver::toDbString(trim($_POST["Address"]));
				$dal->Url = conver::toDbString(trim($_POST["Url"]));
				$dal->Title = conver::toDbString(trim($_POST["Title"]));
				$dal->Content = conver::toDbString(trim($_POST["Content"]));
				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;

				//系統信件內容
				$mailtxt = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
				$mailtxt .= '<html xmlns="http://www.w3.org/1999/xhtml">';
				$mailtxt .= '<head>';
				$mailtxt .= '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
				$mailtxt .= '</head>';
				$mailtxt .= '<body>';
				$mailtxt .= '<table border="1" align="center" cellpadding="0" cellspacing="0" style="width:660px; border:1px #000 solid;">';
				$mailtxt .= '<tr>';
				$mailtxt .= '<td colspan="2" style="border:0; border-bottom:1px #ccc solid; text-align:center;"><img src="';
				if($dalP->Image == ''){
					$mailtxt .= '';
				}else{
					$mailtxt .= 'http://'.$_SERVER["HTTP_HOST"].$dalP->Image;
				}
				$mailtxt .= '" style="margin:30px;" /></td>';
				$mailtxt .= '</tr>';
				$mailtxt .= '<tr>';
				$mailtxt .= '<td style="width:200px; border:0; border-bottom:1px #ccc solid; text-align:center;">';
				$mailtxt .= '<br />標題：<br />';
				$mailtxt .= '</td>';
				$mailtxt .= '<td style="width:400px; border:0; border-bottom:1px #ccc solid;">';
				$mailtxt .= '<br />'.$dal->Title.'<br />';
				$mailtxt .= '</td>';
				$mailtxt .= '</tr>';
				$mailtxt .= '<tr style="background:#E7E7E7;">';
				$mailtxt .= '<td style="width:200px; border:0; border-bottom:1px #ccc solid; text-align:center;">';
				$mailtxt .= '<br />姓名：<br />';
				$mailtxt .= '</td>';
				$mailtxt .= '<td style="width:400px; border:0; border-bottom:1px #ccc solid;">';
				$mailtxt .= '<br />'.$dal->Name.'<br />';
				$mailtxt .= '</td>';
				$mailtxt .= '</tr>';
				$mailtxt .= '<tr>';
				$mailtxt .= '<td style="width:200px; border:0; border-bottom:1px #ccc solid; text-align:center;">';
				$mailtxt .= '<br />電話：<br />';
				$mailtxt .= '</td>';
				$mailtxt .= '<td style="width:400px; border:0; border-bottom:1px #ccc solid;">';
				$mailtxt .= '<br />'.$dal->Phone.'<br />';
				$mailtxt .= '</td>';
				$mailtxt .= '</tr>';
				$mailtxt .= '<tr style="background:#E7E7E7;">';
				$mailtxt .= '<td style="width:200px; border:0; border-bottom:1px #ccc solid; text-align:center;">';
				$mailtxt .= '<br />信箱：<br />';
				$mailtxt .= '</td>';
				$mailtxt .= '<td style="width:400px; border:0; border-bottom:1px #ccc solid;">';
				$mailtxt .= '<br />'.$dal->Email.'<br />';
				$mailtxt .= '</td>';
				$mailtxt .= '</tr>';
				$mailtxt .= '<tr>';
				$mailtxt .= '<td style="width:200px; border:0; border-bottom:1px #ccc solid; text-align:center;">';
				$mailtxt .= '<br />內容：<br />';
				$mailtxt .= '</td>';
				$mailtxt .= '<td style="width:400px; border:0; border-bottom:1px #ccc solid;">';
				$mailtxt .= '<br />'.str_replace("\r\n","<br />",$dal->Content).'<br />';
				$mailtxt .= '</td>';
				$mailtxt .= '</tr>';
				$mailtxt .= '</table><br />';
				$mailtxt .= '<p style="padding:10px; color:#f00; font-weight:bolder;">此信件為系統自動發送!請勿直接回覆!</p>';
				$mailtxt .= '</body>';
				$mailtxt .= '</html>';

				//信件
				$sendMail = array();
				if(trim($this->registry->systemEmail1) != ''){
					$sendMail[] = trim($this->registry->systemEmail1);
				}
				if(trim($this->registry->systemEmail2) != ''){
					$sendMail[] = trim($this->registry->systemEmail2);
				}
				if(trim($this->registry->systemEmail3) != ''){
					$sendMail[] = trim($this->registry->systemEmail3);
				}
				if(trim($this->registry->systemEmail4) != ''){
					$sendMail[] = trim($this->registry->systemEmail4);
				}
				if(trim($this->registry->systemEmail5) != ''){
					$sendMail[] = trim($this->registry->systemEmail5);
				}
				if(count($sendMail) > 0){
					if($this->registry->sendMailType == ""){
						$email = new email();
						$email->fromAddress = "=?UTF-8?B?".base64_encode($this->registry->webName)."?=<".$this->registry->webEmail.">";
						$email->replyToAddress = "=?UTF-8?B?".base64_encode($dal->Name)."?=<".$dal->Email.">";
						$email->subject = "[".$_SERVER['HTTP_HOST']."][聯絡我們系統通知]".$dal->Title;
						$email->content = $mailtxt;
						$email->toAddress = implode(",",$sendMail);
						if($email->send()){
							if(!$dal->insertRecord()){
								parent::alert("很抱歉!聯絡我們送出不成功!");
							}else{
								//系統信件內容
								$mailtxt2 = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
								$mailtxt2 .= '<html xmlns="http://www.w3.org/1999/xhtml">';
								$mailtxt2 .= '<head>';
								$mailtxt2 .= '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
								$mailtxt2 .= '</head>';
								$mailtxt2 .= '<body>';
								$mailtxt2 .= '<p style="padding:10px; color:#f00; font-weight:bolder;">親愛的顧客您好，我們已收到你的來信，我們客服人員會在第一時間，立即與你聯系，請你耐心等候</p><br />';
								$mailtxt2 .= '<table border="1" align="center" cellpadding="0" cellspacing="0" style="width:660px; border:1px #000 solid;">';
								$mailtxt2 .= '<tr>';
								$mailtxt2 .= '<td colspan="2" style="border:0; border-bottom:1px #ccc solid; text-align:center;"><img src="';
								if($dalP->Image == ''){
									$mailtxt2 .= '';
								}else{
									$mailtxt2 .= 'http://'.$_SERVER["HTTP_HOST"].$dalP->Image;
								}
								$mailtxt2 .= '" style="margin:30px;" /></td>';
								$mailtxt2 .= '</tr>';
								$mailtxt2 .= '<tr>';
								$mailtxt2 .= '<td style="width:200px; border:0; border-bottom:1px #ccc solid; text-align:center;">';
								$mailtxt2 .= '<br />標題：<br />';
								$mailtxt2 .= '</td>';
								$mailtxt2 .= '<td style="width:400px; border:0; border-bottom:1px #ccc solid;">';
								$mailtxt2 .= '<br />'.$dal->Title.'<br />';
								$mailtxt2 .= '</td>';
								$mailtxt2 .= '</tr>';
								$mailtxt2 .= '<tr style="background:#E7E7E7;">';
								$mailtxt2 .= '<td style="width:200px; border:0; border-bottom:1px #ccc solid; text-align:center;">';
								$mailtxt2 .= '<br />姓名：<br />';
								$mailtxt2 .= '</td>';
								$mailtxt2 .= '<td style="width:400px; border:0; border-bottom:1px #ccc solid;">';
								$mailtxt2 .= '<br />'.$dal->Name.'<br />';
								$mailtxt2 .= '</td>';
								$mailtxt2 .= '</tr>';
								$mailtxt2 .= '<tr>';
								$mailtxt2 .= '<td style="width:200px; border:0; border-bottom:1px #ccc solid; text-align:center;">';
								$mailtxt2 .= '<br />電話：<br />';
								$mailtxt2 .= '</td>';
								$mailtxt2 .= '<td style="width:400px; border:0; border-bottom:1px #ccc solid;">';
								$mailtxt2 .= '<br />'.$dal->Phone.'<br />';
								$mailtxt2 .= '</td>';
								$mailtxt2 .= '</tr>';
								$mailtxt2 .= '<tr style="background:#E7E7E7;">';
								$mailtxt2 .= '<td style="width:200px; border:0; border-bottom:1px #ccc solid; text-align:center;">';
								$mailtxt2 .= '<br />信箱：<br />';
								$mailtxt2 .= '</td>';
								$mailtxt2 .= '<td style="width:400px; border:0; border-bottom:1px #ccc solid;">';
								$mailtxt2 .= '<br />'.$dal->Email.'<br />';
								$mailtxt2 .= '</td>';
								$mailtxt2 .= '</tr>';
								$mailtxt2 .= '<tr>';
								$mailtxt2 .= '<td style="width:200px; border:0; border-bottom:1px #ccc solid; text-align:center;">';
								$mailtxt2 .= '<br />內容：<br />';
								$mailtxt2 .= '</td>';
								$mailtxt2 .= '<td style="width:400px; border:0; border-bottom:1px #ccc solid;">';
								$mailtxt2 .= '<br />'.str_replace("\r\n","<br />",$dal->Content).'<br />';
								$mailtxt2 .= '</td>';
								$mailtxt2 .= '</tr>';
								$mailtxt2 .= '</table>';
								$mailtxt2 .= '</body>';
								$mailtxt2 .= '</html>';
									
								//信件
								$e2mail = new email();
								$e2mail->fromAddress = "=?UTF-8?B?".base64_encode($this->registry->webName)."?=<".$this->registry->webEmail.">";
								$e2mail->replyToAddress = "=?UTF-8?B?".base64_encode($dal->Name)."?=<".$dal->Email.">";
								$e2mail->subject = "[".$_SERVER['HTTP_HOST']."][聯絡我們送出確認信]".$dal->Title;
								$e2mail->content = $mailtxt2;
								$e2mail->toAddress = $dal->Email;
								$e2mail->send();
									
								parent::alert2("聯絡我們已經送出!我們會盡快回覆!",$this->htLink6.".html");
							}
						}else{
							parent::alert("很抱歉!聯絡我們送出不成功!!");
						}
					}else{
						$smtp_mail = new smtp_mail();
						$smtp_mail->toAry = $sendMail;
						$smtp_mail->name = "=?UTF-8?B?".base64_encode($this->registry->webName)."?=";
						$smtp_mail->subject = "[".$_SERVER['HTTP_HOST']."][聯絡我們系統通知]".$dal->Title;
						$smtp_mail->body = $mailtxt;
						if($smtp_mail->send()){
							if(!$dal->insertRecord()){
								parent::alert("很抱歉!聯絡我們送出不成功!");
							}else{
								//系統信件內容
								$mailtxt2 = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
								$mailtxt2 .= '<html xmlns="http://www.w3.org/1999/xhtml">';
								$mailtxt2 .= '<head>';
								$mailtxt2 .= '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
								$mailtxt2 .= '</head>';
								$mailtxt2 .= '<body>';
								$mailtxt2 .= '<p style="padding:10px; color:#f00; font-weight:bolder;">親愛的顧客您好，我們已收到你的來信，我們客服人員會在第一時間，立即與你聯系，請你耐心等候</p><br />';
								$mailtxt2 .= '<table border="1" align="center" cellpadding="0" cellspacing="0" style="width:660px; border:1px #000 solid;">';
								$mailtxt2 .= '<tr>';
								$mailtxt2 .= '<td colspan="2" style="border:0; border-bottom:1px #ccc solid; text-align:center;"><img src="';
								if($dalP->Image == ''){
									$mailtxt2 .= '';
								}else{
									$mailtxt2 .= 'http://'.$_SERVER["HTTP_HOST"].$dalP->Image;
								}
								$mailtxt2 .= '" style="margin:30px;" /></td>';
								$mailtxt2 .= '</tr>';
								$mailtxt2 .= '<tr>';
								$mailtxt2 .= '<td style="width:200px; border:0; border-bottom:1px #ccc solid; text-align:center;">';
								$mailtxt2 .= '<br />標題：<br />';
								$mailtxt2 .= '</td>';
								$mailtxt2 .= '<td style="width:400px; border:0; border-bottom:1px #ccc solid;">';
								$mailtxt2 .= '<br />'.$dal->Title.'<br />';
								$mailtxt2 .= '</td>';
								$mailtxt2 .= '</tr>';
								$mailtxt2 .= '<tr style="background:#E7E7E7;">';
								$mailtxt2 .= '<td style="width:200px; border:0; border-bottom:1px #ccc solid; text-align:center;">';
								$mailtxt2 .= '<br />姓名：<br />';
								$mailtxt2 .= '</td>';
								$mailtxt2 .= '<td style="width:400px; border:0; border-bottom:1px #ccc solid;">';
								$mailtxt2 .= '<br />'.$dal->Name.'<br />';
								$mailtxt2 .= '</td>';
								$mailtxt2 .= '</tr>';
								$mailtxt2 .= '<tr>';
								$mailtxt2 .= '<td style="width:200px; border:0; border-bottom:1px #ccc solid; text-align:center;">';
								$mailtxt2 .= '<br />電話：<br />';
								$mailtxt2 .= '</td>';
								$mailtxt2 .= '<td style="width:400px; border:0; border-bottom:1px #ccc solid;">';
								$mailtxt2 .= '<br />'.$dal->Phone.'<br />';
								$mailtxt2 .= '</td>';
								$mailtxt2 .= '</tr>';
								$mailtxt2 .= '<tr style="background:#E7E7E7;">';
								$mailtxt2 .= '<td style="width:200px; border:0; border-bottom:1px #ccc solid; text-align:center;">';
								$mailtxt2 .= '<br />信箱：<br />';
								$mailtxt2 .= '</td>';
								$mailtxt2 .= '<td style="width:400px; border:0; border-bottom:1px #ccc solid;">';
								$mailtxt2 .= '<br />'.$dal->Email.'<br />';
								$mailtxt2 .= '</td>';
								$mailtxt2 .= '</tr>';
								$mailtxt2 .= '<tr>';
								$mailtxt2 .= '<td style="width:200px; border:0; border-bottom:1px #ccc solid; text-align:center;">';
								$mailtxt2 .= '<br />內容：<br />';
								$mailtxt2 .= '</td>';
								$mailtxt2 .= '<td style="width:400px; border:0; border-bottom:1px #ccc solid;">';
								$mailtxt2 .= '<br />'.str_replace("\r\n","<br />",$dal->Content).'<br />';
								$mailtxt2 .= '</td>';
								$mailtxt2 .= '</tr>';
								$mailtxt2 .= '</table>';
								$mailtxt2 .= '</body>';
								$mailtxt2 .= '</html>';
									
								//信件
								$to2Ary = array();
								$to2Ary[] = $dal->Email;
								$smtp2_mail = new smtp_mail();
								$smtp2_mail->toAry = $to2Ary;
								$smtp2_mail->name = "=?UTF-8?B?".base64_encode($this->registry->webName)."?=";
								$smtp2_mail->subject = "[".$_SERVER['HTTP_HOST']."][聯絡我們送出確認信]".$dal->Title;
								$smtp2_mail->body = $mailtxt2;
								$smtp2_mail->send();
									
								parent::alert2("聯絡我們已經送出!我們會盡快回覆!",$this->htLink6.".html");
							}
						}else{
							parent::alert("很抱歉!聯絡我們送出不成功!!");
						}
					}
				}else{
					if(!$dal->insertRecord()){
						parent::alert("很抱歉!聯絡我們送出不成功!");
					}else{
						parent::alert2("聯絡我們已經送出!我們會盡快回覆!",$this->htLink6.".html");
					}
				}
			}else{
				parent::alert("驗證碼輸入錯誤喔");
			}
		}
	}

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