<?php 
include '../backend/db/db.php';
session_start();

date_default_timezone_set('Asia/Colombo');
$date = date('Y-m-d H:i:s');

require("../PHPMailer/class.phpmailer.php");

/**************************** Customer Feedback ***************************/

if (isset($_POST['comm_submit'])) {
	$name = htmlspecialchars(strip_tags(trim($_POST['con_name'])), ENT_QUOTES, 'UTF-8');
	$email = htmlspecialchars(strip_tags(trim($_POST['con_email'])), ENT_QUOTES, 'UTF-8');
	$tel_no = htmlspecialchars(strip_tags(trim($_POST['con_phone'])), ENT_QUOTES, 'UTF-8');
	$title = htmlspecialchars(strip_tags(trim($_POST['con_sub'])), ENT_QUOTES, 'UTF-8');
	$message = htmlspecialchars(strip_tags(trim($_POST['con_message'])), ENT_QUOTES, 'UTF-8');
	
	/*********************************************/

	$message1 = '<!DOCTYPE html>';
	$message1 .= '<html><head><title></title></head><body>';
	$message1 .= '<h3 style="margin-bottom: 20px;"><u>Customer Message</u></h3>';
	$message1 .= '<table style = "width: 100%;margin-top: 20px;" border="0">';
	$message1 .= '<tr><td style = "width: 15%;"><b>Name:</b> </td><td>'.$name.'</td></tr>';
	$message1 .= '<tr><td><b>Contact No:</b> </td><td>'.$tel_no.'</td></tr>';
	$message1 .= '<tr><td><b>Email:</b> </td><td>'.$email.'</td></tr>';
	$message1 .= '<tr><td><b>Subject:</b> </td><td>'.$title.'</td></tr>';
	$message1 .= '<tr><td><b>Message:</b> </td><td>'.$message.'</td></tr>';
	$message1 .= '<tr><td><b>Date:</b> </td><td>'.date('Y-m-d H:i:s').'</td></tr>';
	$message1 .= '</table><img src="https://buddhistbooksonline.com/images/logo/logo.png" style = "margin : 20px 0;" width="250"></body></html>';
	
	//echo $message1;
	  
	try {
		$mail1 = new PHPMailer();
		$mail1->IsSMTP(); // set mailer to use SMTP
		$mail1->Host = $email_host;  // specify main and backup server
		$mail1->SMTPAuth = true;     // turn on SMTP authentication
		$mail1->Username = $server_email_add;  // SMTP username
		$mail1->Password = $server_email_pw; // SMTP password
		$mail1->SMTPSecure = 'ssl';
		$mail1->Port = 465;
		$mail1->From = $server_email_from;
		$mail1->FromName = "buddhistbooksonline.com";
		$mail1->AddAddress("nirmal@samudrabooks.com", "Nirmal Ranasinghe");
		if(!empty($email)) { $mail1->AddReplyTo($email, $name); }
		$mail1->IsHTML(true); // set email format to HTML
		$mail1->Subject = "Customer Message - ".$title;
		$mail1->Body    = $message1;

		if($mail1->Send()) {
			$response['status'] = '1';
			$response['message'] = 'Successfully sent your message. Thank You!';
		
		} else {
			$response['status'] = 'error';
			$response['message'] = 'Something went wrong, please try again...';
		}
	
	} catch(phpmailerException $ex) {
		//$err_msg = $ex->errorMessage();
		$response['status'] = 'error';
		$response['message'] = 'Something went wrong, please try again...';
	}
	
	echo json_encode($response);
}

/*************************** Customer Review *****************************/

if (isset($_POST['rev_submit'])) {
	$name = htmlspecialchars(strip_tags(trim($_POST['rev_name'])), ENT_QUOTES, 'UTF-8');
	$email = htmlspecialchars(strip_tags(trim($_POST['rev_email'])), ENT_QUOTES, 'UTF-8');
	$comment = htmlspecialchars(strip_tags(trim($_POST['rev_message'])), ENT_QUOTES, 'UTF-8');
	$rating = htmlspecialchars(strip_tags(trim($_POST['rev_star'])), ENT_QUOTES, 'UTF-8');
	$item_id = htmlspecialchars(strip_tags(trim($_POST['book_id'])), ENT_QUOTES, 'UTF-8');
	
	if(empty($rating)) {
		$rating = 1;
	}
	$sql = "INSERT INTO cus_review SET name='$name', email='$email', item_id='$item_id', comment='$comment', review='$rating', accept=0, date=now()";
	
	if(empty($item_id) || empty($comment)) {
	  $response['status'] = 'error';
	  $response['message'] = 'Invalid Item Code, please try again...';
	
	} else {
		if (mysqli_query($conn, $sql)) {			
			$response['status'] = '1';
			$response['message'] = 'Thank for your review.';
	  
		} else {
			  $response['status'] = 'error';
			  $response['message'] = 'Something went wrong, please try again...';
		}
	}
	echo json_encode($response);
}

?>