<?php
error_reporting(0);
if (isset($_POST['itemName']) && isset($_POST['TrackingCode']) && isset($_POST['senderName']) && isset($_POST['receiverName']) && isset($_POST['parcelStat']) && isset($_POST['currLocation']) 
&& isset($_POST['arrivalDate']) &&  isset($_POST['shippingType']) && isset($_POST['taxFee']) && isset($_POST['Pieces']) && isset($_POST['serviceType']) && isset($_POST['destinationAddr']) && 
isset($_POST['itemDesc']) ) {

if (!empty(trim($_POST['itemName'])) && !empty(trim($_POST['TrackingCode'])) && !empty(trim($_POST['senderName'])) && !empty(trim($_POST['receiverName'])) && !empty(trim($_POST['parcelStat'])) && !empty(trim($_POST['currLocation'])) && !empty(trim($_POST['arrivalDate'])) &&  !empty(trim($_POST['shippingType'])) && !empty(trim($_POST['taxFee'])) && !empty(trim($_POST['Pieces'])) && !empty(trim($_POST['serviceType'])) && !empty(trim($_POST['destinationAddr'])) && !empty(trim($_POST['itemDesc'])) ) {
require 'connect.php';
$itemName = mysqli_real_escape_string($connect, $_POST['itemName']);
$TrackingCode = mysqli_real_escape_string($connect, $_POST['TrackingCode']);
$senderName = mysqli_real_escape_string($connect, $_POST['senderName']);
$receiverName = mysqli_real_escape_string($connect, $_POST['receiverName']);
$parcelStat = mysqli_real_escape_string($connect, $_POST['parcelStat']);
$currLocation = mysqli_real_escape_string($connect, $_POST['currLocation']);
$arrivalDate = mysqli_real_escape_string($connect, $_POST['arrivalDate']);
$shippingType = mysqli_real_escape_string($connect, $_POST['shippingType']);
$taxFee = mysqli_real_escape_string($connect, $_POST['taxFee']);
$Pieces = mysqli_real_escape_string($connect, $_POST['Pieces']);
$Phone = mysqli_real_escape_string($connect, $_POST['Phone'] ?? '');
$serviceType = mysqli_real_escape_string($connect, $_POST['serviceType']);
$destinationAddr = mysqli_real_escape_string($connect, $_POST['destinationAddr']);
$itemDesc = mysqli_real_escape_string($connect, $_POST['itemDesc']);


$Email = mysqli_real_escape_string($connect, $_POST['receiverEmail'] ?? '');

$InsurFee = mysqli_real_escape_string($connect, $_POST['InsurFee'] ?? '');
$Vat = mysqli_real_escape_string($connect, $_POST['Vat'] ?? '');
$TotalFee = mysqli_real_escape_string($connect, $_POST['TotalFee'] ?? '');

$TotalPaid = mysqli_real_escape_string($connect, $_POST['TotalPaid'] ?? '');
$Map = mysqli_real_escape_string($connect, $_POST['Map'] ?? '');

$myStr = $_SERVER['SERVER_NAME'];
$subStr =  substr($myStr,0,1);

$insertQuery = mysqli_query($connect, "INSERT INTO WParcel(ID, trackCode, itemName, senderName, receiverName, Email, itemDesc, destinationAddr, Piece, currLocation, status, shippingType, serviceType, arrivalDate, taxFee, InsurFee, Vat, TotalFee, TotalPaid, Map, Phone) VALUES(NULL, '$TrackingCode', '$itemName', '$senderName', '$receiverName', '$Email', '$itemDesc', '$destinationAddr', '$Pieces', '$currLocation', '$parcelStat', '$shippingType', '$serviceType', '$arrivalDate', '$taxFee', '$InsurFee', '$Vat', '$TotalFee', '$TotalPaid', '$Map', '$Phone')");


if ($insertQuery) {
if($subStr == "c") {
require "PHPMailerAutoload.php";
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'mail.courierdeliveryservicex.online';
$mail->Port = 465;
$mail->SMTPAuth = true;
$mail->Username = 'support@courierdeliveryservicex.online';
$mail->Password = 'AdminAccess160';
$mail->SMTPSecure = 'ssl';          

$mail->setFrom('support@courierdeliveryservicex.online', 'Courier Delivery ServiceX ');
$mail->addAddress($Email, $receiverName); 
//Content
$mail->isHTML(true); 
$mail->Subject = 'PARCEL INVOICE';
$mail->Body = <<<EOD
<body style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif;text-align: center;color: #777;">
<div class="invoice-box" style="width: 100%;margin: auto;padding: 10px;border: 1px solid #eee;box-shadow: 0 0 10px rgba(0, 0, 0, 0.15);font-size: 16px;line-height: 24px;font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif;color: #555;box-sizing: border-box;">
<table style="width: 100%;line-height: inherit;text-align: left;border-collapse: collapse;">
<tr class="top">
<td colspan="2" style="padding: 5px;vertical-align: top;">
<table style="width: 100%;line-height: inherit;text-align: left;border-collapse: collapse;">
<tr>
<td class="title" style="padding: 5px;vertical-align: top;padding-bottom: 20px;font-size: 45px;line-height: 45px;color: #333;">
<img src="https://courierdeliveryservicex.online/home/assets/img/Servicex.png" alt="Company logo" style="width: 100%; max-width: 150px">
</td>

<td style="padding: 5px;vertical-align: top;text-align: right;padding-bottom: 20px;">
Invoice #: 00282882<br>
Receiver's Name: <br> $receiverName<br>
$destinationAddrt <br>
Email: $Email
</td>
</tr>
</table>
</td>
</tr>

<tr class="information">
<td colspan="2" style="padding: 5px;vertical-align: top;">
<table style="width: 100%;line-height: inherit;text-align: left;border-collapse: collapse;">
<tr>
<td style="padding: 5px;vertical-align: top;padding-bottom: 40px;">

<!-- Mid Logistics Express is a courier <br>and logistics service in the United States. -->
</td>

<td style="padding: 5px;vertical-align: top;text-align: right;padding-bottom: 40px;">
Sender:<br>
$senderName<br>
</td>
</tr>
</table>
</td>
</tr>

<tr class="heading">
<td style="padding: 5px;vertical-align: top;background: #eee;border-bottom: 1px solid #ddd;font-weight: bold;">Destination:</td>

<td style="padding: 5px;vertical-align: top;text-align: right;background: #eee;border-bottom: 1px solid #ddd;font-weight: bold;"></td>
</tr>

<tr class="details">
<td style="padding: 5px;vertical-align: top;padding-bottom: 20px;">$destinationAddr</td>

</tr>

<tr class="heading">
<td style="padding: 5px;vertical-align: top;background: #eee;border-bottom: 1px solid #ddd;font-weight: bold;">More info</td>

<td style="padding: 5px;vertical-align: top;text-align: right;background: #eee;border-bottom: 1px solid #ddd;font-weight: bold;"></td>
</tr>


<tr class="item">
<td style="padding: 5px;vertical-align: top;border-bottom: 1px solid #eee;">Item Tracking Code:</td>

<td style="padding: 5px;vertical-align: top;text-align: right;border-bottom: 1px solid #eee;">$TrackingCode</td>
</tr>

<tr class="item">
<td style="padding: 5px;vertical-align: top;border-bottom: 1px solid #eee;">Item Name:</td>

<td style="padding: 5px;vertical-align: top;text-align: right;border-bottom: 1px solid #eee;">$itemName</td>
</tr>

<tr class="item last">
<td style="padding: 5px;vertical-align: top;border-bottom: none;">Current Status:</td>

<td style="padding: 5px;vertical-align: top;text-align: right;border-bottom: none;">$parcelStat</td>
</tr>


<tr class="item">
<td style="padding: 5px;vertical-align: top;border-bottom: 1px solid #eee;">Shipping Type:</td>

<td style="padding: 5px;vertical-align: top;text-align: right;border-bottom: 1px solid #eee;">$shippingType</td>
</tr>

<tr class="item">
<td style="padding: 5px;vertical-align: top;border-bottom: 1px solid #eee;">Service Type:</td>

<td style="padding: 5px;vertical-align: top;text-align: right;border-bottom: 1px solid #eee;">$serviceType</td>
</tr>

<tr class="item last">
<td style="padding: 5px;vertical-align: top;border-bottom: none;">Pieces:</td>

<td style="padding: 5px;vertical-align: top;text-align: right;border-bottom: none;">$Pieces</td>
</tr>

<tr class="item">
<td style="padding: 5px;vertical-align: top;border-bottom: 1px solid #eee;">Tax:</td>

<td style="padding: 5px;vertical-align: top;text-align: right;border-bottom: 1px solid #eee;">$taxFee</td>
</tr>

<tr class="item">
<td style="padding: 5px;vertical-align: top;border-bottom: 1px solid #eee;">Vat:</td>

<td style="padding: 5px;vertical-align: top;text-align: right;border-bottom: 1px solid #eee;">$Vat</td>
</tr>

<tr class="item last">
<td style="padding: 5px;vertical-align: top;border-bottom: none;">Insurance Fee:</td>

<td style="padding: 5px;vertical-align: top;text-align: right;border-bottom: none;">$InsurFee</td>
</tr>
<tr class="item">
<td style="padding: 5px;vertical-align: top;border-bottom: 1px solid #eee;">Total Fee:</td>

<td style="padding: 5px;vertical-align: top;text-align: right;border-bottom: 1px solid #eee;">$TotalFee</td>
</tr>

<tr class="item last">
<td style="padding: 5px;vertical-align: top;border-bottom: none;">Total Fee Paid:</td>

<td style="padding: 5px;vertical-align: top;text-align: right;border-bottom: none;">$TotalPaid</td>
</tr>

</table>
<p>
$itemDesc
</p>
</div>
EOD;
if ($mail->send()) {
	echo "<p style='text-align: center; color: green;'> PARCEL successfully sent.</p>";
	
	 // == sms ====

$SendTo = $Phone;
$Message = "Dear customer, Your parcel has been confirmed successfully, please check your email for more details.";


$Message = str_replace(" ","%20", $Message);
$url = 'https://kullsms.com/customer/api/?username=jephthah160@gmail.com&password=Mankind160&message='.$Message.'&sender=ASD&mobiles='.$SendTo.'.';

function get_http_response_code($theURL) {
$headers = get_headers($theURL);
return substr($headers[0], 9, 3);
}
get_http_response_code($url);

}else {
  echo "<p style='text-align: center; color: red;'> Error: PARCEL successfully sent but email invoice was not sent due to unknown error</p>";
}	

}else {
    	echo "<p style='text-align: center; color: green;'> PARCEL successfully sent.</p>";
}	
} else {
	echo "<p style='text-align: center; color: red;'> An error occured, could not send PARCEL.</p>";
}

	

}else {
	echo "<p style='text-align: center; color: red;'> All fields are important</p>";
}

}else {

	header("Location: /home/");
	die();
}
     
  



  ?>