<?php
// 如果有錯誤訊息則顯示
error_reporting(E_ALL);
define('IN_AUTH', true);
require(dirname(dirname(__FILE__)) . '/includes/init.php');

if(isset($_POST['ajax'])){
	if($_POST['ajax'] == 'send_mail'){
		if(isset($_POST['contact_name'])&&isset($_POST['contact_phone'])&&isset($_POST['contact_mail'])&&isset($_POST['contact_need'])){
			$contact_name = $_POST['contact_name'];
			$contact_phone = $_POST['contact_phone'];
			$contact_mail = $_POST['contact_mail'];
			$contact_need = $_POST['contact_need'];
			$source = $_POST['source'];
			$is_google_form_success = $_POST['is_google_form_success'];
			$agree_contact = $_POST['agree_contact'] == 1 ? "是" : "否";
			
			if($_POST['agree_contact'] == 1){
				/*
				$txt = '<table style="width:500px;border:1px solid #E3E3E3;padding:1px;">
						  <thead>
							<tr>
							  <th colspan="2" style="border-radius: .25rem .25rem 0 0;cursor: auto;background: #f0f0f0;text-align:left;color:rgba(0,0,0,.8);
								padding: .7em .8em;font-style:none;font-weight:700;text-transform:none;border-bottom:1px solid #d4d4d5;">新市一丁目-客戶詢問信</th>
							</tr>
						  </thead>
						  <tbody>
							<tr>
							  <td style="height:40px;border-bottom:1px solid #ccc;color:#777676;padding:0px 12px;">姓名</td>
							  <td style="border-bottom:1px solid #ccc">'.$contact_name.'</td>
							</tr>
							<tr>
							  <td style="height:40px;border-bottom:1px solid #ccc;color:#777676;padding:0px 12px;">電話</td>
							  <td style="border-bottom:1px solid #ccc">'.$contact_phone.'</td>
							</tr>
							<tr>
							  <td style="height:40px;border-bottom:1px solid #ccc;color:#777676;padding:0px 12px;">Email</td>
							  <td style="border-bottom:1px solid #ccc">'.$contact_mail.'</td>
							</tr>
							<tr>
							  <td style="height:40px;border-bottom:1px solid #ccc;color:#777676;padding:0px 12px;">方便聯繫時間</td>
							  <td style="border-bottom:1px solid #ccc">'.$contact_need.'</td>
							</tr>
							<tr>
							  <td style="height:40px;border-bottom:1px solid #ccc;color:#777676;padding:0px 12px;">同意透過電話或E-mail方式聯絡</td>
							  <td style="border-bottom:1px solid #ccc">'.$agree_contact.'</td>
							</tr>
							<tr>
							  <td style="height:40px;border-bottom:1px solid #ccc;color:#777676;padding:0px 12px;">寫入google表單結果</td>
							  <td style="border-bottom:1px solid #ccc">'.$is_google_form_success.'</td>
							</tr>
						  </tbody>
						</table>';
				*/
				$txt = "姓名：$contact_name<br/>".
						"電話：$contact_phone<br/>".
						"Email：$contact_mail<br/>".
						"方便聯絡時間：$contact_need<br/>".
						"日期：" . date("Y-m-d H:i:s") . "<br/><br/>".
						"來源媒體：$source<br/>";
				
				require ("includes/PHPMailer/class.phpmailer.php");
  
				$mail= new PHPMailer();                          			//建立新物件  
				$mail->IsSMTP();                                    		//設定使用SMTP方式寄信  
				$mail->SMTPAuth = true;                        				//設定SMTP需要驗證  
				$mail->SMTPSecure = "ssl";                    				// Gmail的SMTP主機需要使用SSL連線  
				
				$mail->Host = "smtp.gmail.com";            					//Gamil的SMTP主機  
				$mail->Port = 465;                                			//Gamil的SMTP主機的埠號(Gmail為465)。  
				$mail->CharSet = "utf-8";                       			//郵件編碼  
				$mail->Username = MAIL_FROM_ACC; 							//Gamil帳號  
				$mail->Password = MAIL_FROM_PSW; 			                //Gmail密碼  
				$mail->From = MAIL_FROM_MAIL;								//件者信箱  
					
				$mail->FromName = htmlspecialchars(MAIL_FROM);         		//寄件者姓名  
				$mail->Subject = MAIL_SUBJECT;								//郵件標題  
				$mail->Body = $txt;  
				$mail->IsHTML(true);                             			//郵件內容為html  
				
				$emails = explode(",", MAIL_TO);
				foreach($emails as $key => $val){
					$mail->AddAddress($val);          						//收件者郵件及名稱  
				}
				if(!$mail->Send()){  
					echo json_encode(array('status'=>'0','msg'=>'寄信失敗，請再重新提交一次，謝謝'));
				}else{  
					echo json_encode(array('status'=>'1','msg'=>'送出預約成功，感謝您'));	 
				}  
				
				/*
				$charset = 'UTF-8';
				$to      = MAIL_TO;
				$subject = MAIL_SUBJECT;
				$message = $txt;
				$headers = "From: ".MAIL_FROM_MAIL."\r\n" .
						   "MIME-Version: 1.0\r\n" .
						   "Content-type: text/html; charset=$charset\r\n";
				
				if(mail($to, $subject, $message, $headers)){
					echo json_encode(array('status'=>'1','msg'=>'送出預約成功，感謝您'));	
				}else{
					echo json_encode(array('status'=>'0','msg'=>'寄信失敗，請再重新提交一次，謝謝'));
				}
				*/
			}else{
				echo json_encode(array('status'=>'0','msg'=>'請同意透過電話或E-mail方式與您聯絡'));
			}			
		}else{
			echo json_encode(array('status'=>'0','msg'=>'請確認預約資訊欄位是否有未填寫'));
		}
	}else{
		echo json_encode(array('status'=>'0','msg'=>'請重試一次'));
	}
}else{
	echo json_encode(array('status'=>'0','msg'=>'失敗'));
}