<?php 
require_once("header.php");

// Stop making changes below this line
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'phpmailer/PHPMailer-master/src/Exception.php';
require 'phpmailer/PHPMailer-master/src/PHPMailer.php';
require 'phpmailer/PHPMailer-master/src/SMTP.php';
?>

<?php
if(isset($_SESSION['USER_ID']))
{
	$urid = $_SESSION['USER_ID'];
}
else
{
	$urid = 0;
}

$sqlOrder2 = mysqli_fetch_array(mysqli_query($connection,"SELECT * FROM `og_orders` WHERE `user_id` = '".$urid."' AND `unique_id` = '".$session_id."'"));
    $full_name = $sqlOrder2['fname']." ".$sqlOrder2['lname'];

    //SEND EMAIL TO ADMINISTRATOR
    $emailc = $contact_email;

    // Validate email
	$email_account = "info@prismonline.in";
    $email_password = "~I5er1FU&bR0";

	// Recipient
	$mail = new PHPMailer;
	$mail->isSMTP();
	$mail->SMTPDebug = 0;

	$subject = 'A New Order Posted';	
	$message = '<table border="0" width="100%" cellspacing="2" cellpadding="2">
		<tr>
			<td>A new order has been posted. Details are below : </td>
		</tr>				
		<tr>
			<td>&nbsp;</td>
		</tr>
		<tr>
			<td><b>Customer Details</b></td>
		</tr>
		<tr>
			<td>First Name : '.$sqlOrder2['fname'].'</td>
		</tr>
		<tr>
			<td>Last Name : '.$sqlOrder2['lname'].'</td>
		</tr>
		<tr>
			<td>Email Address : '.$sqlOrder2['email'].'</td>
		</tr>
		<tr>
			<td>Phone No. : '.$sqlOrder2['phone'].'</td>
		</tr>
		<tr>
			<td>Address : '.$sqlOrder2['address'].'</td>
		</tr>
		<tr>
			<td>City : '.$sqlOrder2['city'].'</td>
		</tr>
		<tr>
			<td>Pincode : '.$sqlOrder2['zip'].'</td>
		</tr>
		<tr>
			<td>&nbsp;</td>
		</tr>
		<tr>
			<td><b>Order Details</b></td>
		</tr>';
$order_counter = 1;
$sqlOrder = "SELECT * FROM `og_temp_cart` WHERE `user_id` = '".$urid."' AND `unique_id` = '".$session_id."' ORDER BY `cart_id` ASC";
$exeOrder = mysqli_query($connection,$sqlOrder);
$numOrder = mysqli_num_rows($exeOrder);
if($numOrder>0)
{
while($arrOrder = mysqli_fetch_array($exeOrder))
{
    $fetch_product = mysqli_fetch_array(mysqli_query($connection,"SELECT * FROM `og_products` WHERE `product_id` = '".$arrOrder['product_id']."'"));
$message .= '<tr>
			<td>Item '.$order_counter.' : '.stripslashes($fetch_product['product_name_english']).' - &pound;'.$arrOrder['unit_price'].' X '.$arrOrder['quantity'].' = &pound;'.$arrOrder['subtotal'].'</td>
		</tr>';
$order_counter++;
}
}
$message .= '<tr>
			<td>&nbsp;</td>
		</tr>
		<tr>
			<td>Total Cost : &pound;'.$sqlOrder2['total_price'].'</td>
		</tr>
		<tr>
			<td>Discount : &pound;'.$sqlOrder2['discount'].'</td>
		</tr>
		<tr>
			<td>Shipping Charge : &pound;'.$sqlOrder2['delivery_charge'].'</td>
		</tr>
		<tr>
			<td><b>Grand Total : &pound;'.$sqlOrder2['grand_total'].'</b></td>
		</tr>
		</table>';
		
	$mail->Host = 'mail.prismonline.in';
	$mail->Port = 587;
	$mail->SMTPAuth = true;
	$mail->SMTPSecure = "tls";
	$mail->Username = $email_account;
	$mail->Password = $email_password;

	$mail->setFrom('info@prismonline.in', "Flaro Deals");
	$mail->addReplyTo('info@prismonline.in', "");
	$mail->addAddress($emailc);
	$mail->Subject = $subject;
	$TemplateMessage = $message;
	$mail->MsgHTML($TemplateMessage);
	$mail->send();
	
	//SEND EMAIL TO USER
    $namew = $full_name;
    $emailw = $sqlOrder2['email'];

    // Validate email
	$email_account = "info@prismonline.in";
    $email_password = "~I5er1FU&bR0";

	// Recipient
	$mail = new PHPMailer;
	$mail->isSMTP();
	$mail->SMTPDebug = 0;
	
	$subject2 = 'Order Confirmation';	
	$message2 = '<table border="0" width="100%" cellspacing="2" cellpadding="2">
		<tr>
			<td>Hi '.$sqlOrder2['fname'].' '.$sqlOrder2['lname'].',</td>
		</tr>
		<tr>
			<td>&nbsp;</td>
		</tr>
		<tr>
			<td>Your order details are below:</td>
		</tr>';
$order_counter = 1;
$sqlOrder = "SELECT * FROM `og_temp_cart` WHERE `user_id` = '".$urid."' AND `unique_id` = '".$session_id."' ORDER BY `cart_id` ASC";
$exeOrder = mysqli_query($connection,$sqlOrder);
$numOrder = mysqli_num_rows($exeOrder);
if($numOrder>0)
{
while($arrOrder = mysqli_fetch_array($exeOrder))
{
    $fetch_product = mysqli_fetch_array(mysqli_query($connection,"SELECT * FROM `og_products` WHERE `product_id` = '".$arrOrder['product_id']."'"));

$message2 .= '<tr>
			<td>Item '.$order_counter.' : '.stripslashes($fetch_product['product_name_english']).' - &pound;'.$arrOrder['unit_price'].' X '.$arrOrder['quantity'].' = &pound;'.$arrOrder['subtotal'].'</td>
		</tr>';
$order_counter++;
}
}
$message2 .= '<tr>
			<td>&nbsp;</td>
		</tr>
		<tr>
			<td>Total Cost : &pound;'.$sqlOrder2['total_price'].'</td>
		</tr>
		<tr>
			<td>Discount : &pound;'.$sqlOrder2['discount'].'</td>
		</tr>
		<tr>
			<td>Shipping Charge : &pound;'.$sqlOrder2['delivery_charge'].'</td>
		</tr>
		<tr>
			<td><b>Grand Total : &pound;'.$sqlOrder2['grand_total'].'</b></td>
		</tr>
		<tr>
			<td>&nbsp;</td>
		</tr>
		<tr>
			<td>Thanks & Regards,<br>Flaro Deals Team</td>
		</tr>
		</table>';
		
	$mail->Host = 'mail.prismonline.in';
	$mail->Port = 587;
	$mail->SMTPAuth = true;
	$mail->SMTPSecure = "tls";
	$mail->Username = $email_account;
	$mail->Password = $email_password;

	$mail->setFrom('info@prismonline.in', "Flaro Deals");
	$mail->addReplyTo('info@prismonline.in', "");
	$mail->addAddress($emailw);
	$mail->Subject = $subject2;
	$TemplateMessage = $message2;
	$mail->MsgHTML($TemplateMessage);
	$mail->send();

$sqlCart = "SELECT * FROM `og_temp_cart` WHERE `user_id` = '".$_SESSION['USER_ID']."' AND `unique_id` = '".$session_id."' ORDER BY `cart_id` ASC";
$exeCart = mysqli_query($connection,$sqlCart);
$numCart = mysqli_num_rows($exeCart);
if($numCart>0)
{
	while($arrCart = mysqli_fetch_array($exeCart))
	{
		$sqlInsert = "INSERT INTO `og_final_cart` SET `user_id` = '".$urid."',`order_id` = '".$sqlOrder2['orderid']."',`product_id` = '".$arrCart['product_id']."',`quantity` = '".$arrCart['quantity']."',`unit_price` = '".$arrCart['unit_price']."',`subtotal` = '".$arrCart['subtotal']."',`unique_id` = '".$session_id."'";
		$exeInsert = mysqli_query($connection,$sqlInsert);
	}
}

$sqlDelete = "DELETE FROM `og_temp_cart` WHERE `user_id` = '".$urid."' AND `unique_id` = '".$session_id."'";
$exeDelete = mysqli_query($connection,$sqlDelete);
?>

			<!-- page template area start -->
			<section id="about">
				<div class="container mt-10 pb-10 pt-0">
					<div class="section-content">
						<div class="row">
							<div class="col-sm-12 col-md-12">
							    <h1 class="title mt-5 mb-10" style="text-align:center;">Order Successful</h1>
								<p style="text-align:center;">Thanks for your order. Our support team will conact you soon to confirm shipping details for delivery.</p>
							</div>
						</div>
					</div>
				</div>
			</section>
			<!-- page template area end -->
  
<?php require_once("footer.php");?>