<?php
	require_once "../mobile/pageBase.php";
	require_once "../dal/contactusDAL.php";
	require_once "../bin/system/conver.php";
	require_once "../bin/system/email.php";
	class _contactus 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");

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

		private function saveData()
		{
			$MyContent = '姓名:'.$_POST["UserName"];
			$MyContent .= '<br /><br />電話:'.$_POST["Telephone"];
			$MyContent .= '<br /><br />Email:'.$_POST["Email"];
			$MyContent .= '<br /><br />內容:<br />'.$_POST["Content"];

			$contactus=new contactusDAL();
			$contactus->Visible=0;
			$contactus->UserName=conver::toDbString($_POST["UserName"]);
			$contactus->IpAddress=$_SERVER["REMOTE_ADDR"];
			$contactus->Email=conver::toDbString($_POST["Email"]);
			$contactus->Title=conver::toDbString($_POST["Title"]);
			$contactus->Content=conver::toDbString($MyContent);
			$contactus->Company=conver::toDbString($_POST["Company"]);
			$contactus->Address=conver::toDbString($_POST["Address"]);
			$contactus->Phone=conver::toDbString($_POST["Telephone"]);
			$contactus->Fax=conver::toDbString($_POST["Fax"]);
			$contactus->Mobile=conver::toDbString($_POST["Mobile"]);
			$contactus->Url=conver::toDbString($_POST["Url"]);

			$email=new email();
			$email->fromAddress=$_SERVER["SERVER_NAME"]."<".$this->registry->webCustomerEmail.">";
			$email->replyToAddress=$_SERVER["SERVER_NAME"]."<".$this->registry->webCustomerEmail.">";
			$email->subject="急件!客戶詢問信";
			$email->content=$contactus->Content;
			//客服發郵件
			$email->toAddress=$this->registry->webCustomerEmail;

			if($email->send())
			{
				if(!$contactus->insertRecord())
				{
					parent::alert("很抱歉,你的信送出不成功!");
				}
				else
				{
					parent::alert2("你的留言已經發送出去，我們儘快回覆你","contact.html");
				}
			}
			else
			{
				parent::alert("很抱歉,你的信送出不成功");
			}
			parent::redirect("contact.html");
		}
	}

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