<?php
header("Access-Control-Allow-Origin: *");
error_reporting(E_ALL);
ini_set('display_errors', 'on');
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require("Exception.php");
require("PHPMailer.php");
require("SMTP.php");

$mysqli = new mysqli("185.201.9.20:3306","root","InnoKnights@19","ensens");

if ($mysqli -> connect_errno) {
  echo "Failed to connect to MySQL: " . $mysqli -> connect_error;
  exit();
}
$orderid = 64;
$order_table_sql = "SELECT * FROM tbl_orders where id=".$orderid;
$items_product_sql = "SELECT * FROM tbl_orders_details where type='0' and oid=".$orderid;
$items_custom_sql = "SELECT * FROM tbl_orders_details where type='1' and oid=".$orderid;
$address_shipp_sql = "SELECT * FROM order_ship_address where oid=".$orderid;
$status_sql = "SELECT id,name FROM tbl_order_status ";

$result = $items_product = $items_custom = $address_shipp = $fullname = '';
if ($orderrow = $mysqli->query($order_table_sql)){
  $result = $orderrow->fetch_assoc();
  $orderrow->free_result();
  $name_sql = "SELECT fname,lname FROM tbl_customers where id=". $result['customer'];
	if ($namerow = $mysqli->query($name_sql)){
	  $name = $namerow->fetch_assoc();
	  $fullname = $name->fname .' '. $name->lname ;
	  $namerow->free_result();
	}
}

if ($productrow = $mysqli->query($items_product_sql)){
  $items_product = $productrow->fetch_all(MYSQLI_ASSOC);
  $productrow->free_result();
  $ingrediants_sql = "SELECT id,name FROM tbl_ingredients";
	if ($ingredients_row = $mysqli->query($ingrediants_sql)){
	  $ingredients = $ingredients_row->fetch_all(MYSQLI_ASSOC);
	  $ingredients_row->free_result();
	  $ing_array = array();
	  foreach($ingredients as $ing){
		 $ing_array[$ing['id']] = $ing['name'];
	  }
	}
}

if ($status_row = $mysqli->query($status_sql)){
  $o_status_arr = $status_row->fetch_all(MYSQLI_ASSOC);
  $status_row->free_result();
  $o_status = array();
  foreach($o_status_arr as $stat){
	 $o_status[$stat['id']] = $stat['name'];
  }
}

if ($customrow = $mysqli->query($items_custom_sql)){
  $items_custom = $customrow->fetch_all(MYSQLI_ASSOC);
  $customrow->free_result();
}

if ($addressrow = $mysqli->query($address_shipp_sql)){
  $address_shipp = $addressrow->fetch_assoc();
  $addressrow->free_result();
}

$mysqli->close();
$msg = "<p>Hi ".$name->fname.",</p>
<p>Order number: <b>".$result['order_id']."</b> created successfully.</p>				
<p>We will update you further. Please contact support from our website for any queries - FGIL Ensens.</p>";
		
$style = '<style>
table th, table td
{
	font-size:90% !important;
}
table, tr, th, td {
    border: 1px solid black !important;
}
table th
{
	color:#000 !important;
	font-weight:bolder !important;
}
table td
{
	color:#000 !important;
	font-weight:500 !important;
}
</style>';
$mail_content = '';
if($result != '' && $result != false) {
$mail_content .= '<br/>
<br/>
<div class="my-account-wrapper py-5">
    <div class="container">
    	 	<div class="row">
    
            <div style="border: 0.5px solid; background-color:#cddc3961; max-width: 800px;    margin: auto;padding: 2% 0;" class="col-lg-12 col-md-12">
			<div style="text-align: center;"><img src="https://ensensperfumery.in/front_assets/images/logo/logo.png" alt="ensens" style="width: 100px;text-align: center;">
				</div>
				<div style="padding:0 2%">'.$msg.'<hr />
				</div>
				<h2 style="    text-align: center;color: #383838;margin-bottom: 0;">Order Summary</h2>
				<div class="row my-2">';
					$shipaddress_content = '</div>';
                    if($address_shipp!='')
                    {
                        $shipaddress_content = '<div class="col-md-6" style="padding: 2%;">
                            <span class="text-info" style="font-weight: bolder;">Shipping Address</span><table class="table table-striped table-bordered table-hover table-sm mb-0 table-address">
                                <tr >
                                    <td >Name: </td>
                                    <td >'.$address_shipp['first_name'].' '.$address_shipp['last_name'].'</td>
                                </tr>';
                                
                                if($companyname='')
                                {                               
                                    $shipaddress_content .= '<tr >
                                        <td >Company Name: </td>
                                        <td >'.$address_shipp['company_name'].'</td>
                                    </tr>';
                                
                                }
                                
                                $shipaddress_content .= '<tr >
                                    <td >Address: </td>
                                    <td >'. $address_shipp['address_1'] .'<br />'. $address_shipp['address_2'] .'</td>
                                </tr>
                                <tr >
                                    <td >City: </td>
                                    <td >'.$address_shipp['town_city'].'</td>
                                </tr>
                                <tr >
                                    <td >State: </td>
                                    <td >'.$address_shipp['state'].'</td>
                                </tr>
                                <tr >
                                    <td >Postcode: </td>
                                    <td >'.$address_shipp['postalcode'].'</td>
                                </tr>
                                <tr >
                                    <td >Phone: </td>
                                    <td >'.$address_shipp['phone'].'</td>
                                </tr>
                                <tr >
                                    <td >Email: </td>
                                    <td >'.$address_shipp['email'].'</td>
                                </tr>
                            </table></div>';
                    }
				$mail_content .= $shipaddress_content;                
                if($items_product!=false){
					$items_product_content = '<div class="row my-2">
                        <div class="col-md-12" style="padding-left: 2%;"><span class="text-info" style="font-weight: bolder;">Products</span></div>
                        <div class="col-md-12">
                            <div class="table-responsive" style="padding: 2%;">
                                <table style="width:100%;border: 1px solid black;border-collapse: collapse;text-align:center" class="table table-striped table-bordered table-hover table-sm mb-0 table-list">
                                    <thead style="border: 1px solid black;border-collapse: collapse;">
                                        <tr style="border: 1px solid black;border-collapse: collapse;">
                                            <th style="border: 1px solid black;border-collapse: collapse;" >S.No</th>
                                            <!--<th style="border: 1px solid black;border-collapse: collapse;"  width="10%" class="text-center">Image</th>-->
                                            <th style="border: 1px solid black;border-collapse: collapse;"  >Product</th>
                                            <th style="border: 1px solid black;border-collapse: collapse;"  class="text-right">Price</th>
                                            <th style="border: 1px solid black;border-collapse: collapse;"  class="text-right">Qty</th>
                                            <th style="border: 1px solid black;border-collapse: collapse;"  class="text-right">Total</th>
                                        </tr>
                                    </thead>
                                    <tbody>';
                    
					$sn=1;
					foreach($items_product as $i)
					{                        
						$items_product_content .= '<tr style="border: 1px solid black;border-collapse: collapse;">
							<td style="border: 1px solid black;border-collapse: collapse;">'.$sn.'</td>
							<td style="border: 1px solid black;border-collapse: collapse;">'.$i["product_name"].' - '.$i["variation_name"].' '.$i["variation_unit"].'</td>
							<td style="border: 1px solid black;border-collapse: collapse;" class="text-right">'. number_format($i["price"]) .'</td>
							<td style="border: 1px solid black;border-collapse: collapse;" class="text-right">'. $i["qty"] .'</td>
							<td style="border: 1px solid black;border-collapse: collapse;" class="text-right">'. number_format($i["total"]) .'</td>
						</tr>';
						$sn++;
					}
					$items_product_content .= '</tbody></table></div></div></div>';
					$mail_content .= $items_product_content;
				}
                if($items_custom!=false)
                {
					$custom_items_content = '<div class="row my-2">
                        <div class="col-md-12" style="padding: 2%;"><span class="text-info" style="font-weight: bolder;">Custom Fragrance</span></div>
                        <div class="col-md-12">
                            <table style="width:96%;margin:auto;">
                                <table style="width:100%; margin: auto;border: 0.5px solid black;border-collapse: collapse;text-align:center" class="table table-striped table-bordered table-hover table-sm mb-0 table-list">
                                    <thead>
                                        <tr style="border: 0.5px solid black;border-collapse: collapse;" >
                                            <th style="border: 0.5px solid black;border-collapse: collapse;" >S.No</th>
                                            <th style="border: 0.5px solid black;border-collapse: collapse;">Guest Name</th>
                                            <th style="border: 0.5px solid black;border-collapse: collapse;">Whom</th>
                                            <th style="border: 0.5px solid black;border-collapse: collapse;">Gender</th>
                                            <th style="border: 0.5px solid black;border-collapse: collapse;">Fragrances</th>
                                            <th style="border: 0.5px solid black;border-collapse: collapse;" colspan="2">Ingredients</th>
                                            <th style="border: 0.5px solid black;border-collapse: collapse;">Modifiers</th>
                                            <th style="border: 0.5px solid black;border-collapse: collapse;" colspan="2">Ingredients</th>
                                            <th style="border: 0.5px solid black;border-collapse: collapse;">Bottle Size</th>
                                            <th style="border: 0.5px solid black;border-collapse: collapse;" class="text-right">Price</th>
                                            <th style="border: 0.5px solid black;border-collapse: collapse;" class="text-right">Qty</th>
                                            <th style="border: 0.5px solid black;border-collapse: collapse;" class="text-right">Total</th>
                                        </tr>
                                    </thead>
                                    <tbody style="border: 0.5px solid black;border-collapse: collapse;">';                    
                                    $sn=1;
                                    foreach($items_custom as $i) {                                        
                                        $custom_items_content .= '<tr style="border: 0.5px solid black;border-collapse: collapse;table-layout: fixed;width: 100%;height: 100%;">
                                            <td style="border: 0.5px solid black;border-collapse: collapse;">'. $sn .'</td>
                                            <td style="border: 0.5px solid black;border-collapse: collapse;">'. $i['cf_guest_name'] .'</td>
                                            <td style="border: 0.5px solid black;border-collapse: collapse;">'. $i['cf_to_whom'] .'</td>
                                            <td style="border: 0.5px solid black;border-collapse: collapse;">'. $i['cf_gender'] .'</td>
                                            <td style="border: 0.5px solid black;border-collapse: collapse;">'. $i['cf_type'] .'</td>
                                            <td style="border: 0.5px solid black;border-collapse: collapse;">'. $ing_array[$i['cf_ingredient_a']] .' ('. $i['ing_a_per'] .'%)</td>
                                            <td style="border: 0.5px solid black;border-collapse: collapse;">'. $ing_array[$i['cf_ingredient_b']] .' ('. $i['ing_b_per'] .'%)</td>
                                            <td style="border: 0.5px solid black;border-collapse: collapse;">'. $i['cf_type_2'] .'</td>
                                            <td style="border: 0.5px solid black;border-collapse: collapse;">'. $ing_array[$i['cf_ingredient_a_2']] .' ('. $i['ing_a2_per'] .'%)</td>
                                            <td style="border: 0.5px solid black;border-collapse: collapse;">'. $ing_array[$i['cf_ingredient_b_2']] .' ('. $i['ing_b2_per'] .'%)</td>
                                            <td style="border: 0.5px solid black;border-collapse: collapse;">'. $i['cf_bottle_size'] .'ML</td>
                                            <td style="border: 0.5px solid black;border-collapse: collapse;" class="text-right">'. number_format($i['price']) .'</td>
                                            <td style="border: 0.5px solid black;border-collapse: collapse;" class="text-right">'. $i['qty'] .'</td>
                                            <td style="border: 0.5px solid black;border-collapse: collapse;" class="text-right">'. number_format($i['total']) .'</td></tr>';
                                        $sn++;
                                    }
					$custom_items_content .= '</tbody></table></table></div></div>';
					$mail_content .= $custom_items_content;
                }
                
				$mail_content .= '<br/>
                <div class="row my-2" style="margin-right: 2%;">
                    <div class="col-lg-12">
                        <div class="table-responsive">
                            <table  style="width:100%;" id="" class="table table-striped table-hover mb-3 table-main">
							<tbody style="text-align:left; ">
                                <tr style="border: 1px solid black;border-collapse: collapse;" >
                                    <th style="width: 75%; text-align: right;" >Total Qty: </th>
                                    <th style="text-align: right;" >'. $result['total_qty'] .'</th>
                                </tr>
                                <tr>
                                    <th style="width: 75%; text-align: right;">Total Amount: </th>
                                    <th style="text-align: right;" >'. number_format($result['total_amt']) .'</th>
                                </tr>
                                <tr>
                                    <th style="width: 75%; text-align: right;">Payment Method: </th>
                                    <th style="text-align: right;">'. ucwords($result['payment_method']) .'</th>
                                </tr>
                            </tbody>
                            </table>
        
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>';
}
// PHPMailer object
$mail = new PHPMailer();

// SMTP configuration
$mail->isSMTP();
$mail->SMTPDebug = 2; // debugging: 1 = errors and messages, 2 = messages only
$mail->Host = 'localhost';
$mail->SMTPAuth = false;
$mail->SMTPAutoTLS = false; 
$mail->Port = 25;

$mail->setFrom('admin@ensensperfumery.com', 'Ensens');
// Add a recipient
$mail->addAddress('rajanbabu.babu9@gmail.com');
// Email subject
$mail->Subject = 'Your Order Confirmation - ';

// Set email format to HTML
$mail->isHTML(true);
// Email body content
$mail->Body = $mail_content;
// Send email
if(!$mail->send()){			
	$log_value = array('type'=>'mail', 'message' => $msg, 'error' => $mail->ErrorInfo);
//	$this->UDM->insert('notification_failed_log',$log_value);
print_r($mail->ErrorInfo);
} else {
	print_r("sent");
}
