<?php
	require_once('source/session.php');
	require_once('source/error.php');
	require_once('source/dbconfig.php');
	require_once('source/function.php');
	
	if(isset($_POST['submit']) && $_POST['submit'] == "UPDATE")
	{
		try { 
			// INSERT RECORD
			$fullname			= strip_tags($_POST['fullname']);
			$order_number	    		= strip_tags($_POST['order_number']);
			$email	    		= strip_tags($_POST['c_email']);
			$phone	    		= strip_tags($_POST['phone']);
			$s_email	    		= strip_tags($_POST['email']);
			$item	    		= strip_tags($_POST['item']);
			$destination    		= strip_tags($_POST['destination']);
			$location	    		= strip_tags($_POST['location']);
			$locationi	    		= strip_tags($_POST['locationi']);
			$locationii	    		= strip_tags($_POST['locationii']);
			$address	    		= strip_tags($_POST['address']);
			$postalcode	    		= strip_tags($_POST['postalcode']);
			$suc_status	    		= strip_tags($_POST['success_status']);
			$c_status	    		= strip_tags($_POST['status']);
			$dt_stamp_end	    		= strip_tags($_POST['dt_stamp_end']);
			
					
			$rows1 = sql($DBH, "SELECT * FROM tbl_details where order_number = ?", array($order_number), "rows");							
			foreach($rows1 as $row1)
			{	
				$order_id			= $row1['id'];
				$order_number	   		= $row1['order_number'];
				$c_name 		   	= $row1['name'];
				$c_phone 			= $row1['phone'];
				$c_email 		   	= $row1['email'];
				$c_item 		   	= $row1['item'];
				$c_destination 		   	= $row1['destination'];
				$c_locationi 		   	= $row1['locationi'];
				$c_locationii 		   	= $row1['locationii'];
				$c_locationiii 		   	= $row1['locationiii'];
				$c_address 		   	= $row1['address'];
				$c_postalcode 		   	= $row1['postalcode'];
				$sales_rep 		   	= $row1['sales_rep'];
				$status 		   	= $row1['status'];
				$sales_repphone 		   	= $row1['sales_repphone'];
				$sales_repemail 		   	= $row1['sales_repemail'];
				$date_time 		   	= $row1['date_time'];
				$date_stamp_end 		   	= $row1['dt_stamp_end'];
			}
			if($status == "PROCESSED"){
			$message = '<html>
									<head>
									  <title>ORDER PROCESSED</title>
									</head>
									<body>
									  <p>Dear '.$fullname.',</p><br/><br/>
									  <h3>CONGRATULATIONS! YOUR ORDER HAS BEEN PROCESSED</h3>
									  <P>We appreciat your patronage and we are glad to inform you that your order has been processed.</p>
									  <p>You will be updated from time to time as about your order details as it moves from one location to another.</p>
									  <p>Here is your <b>track number: '.$key.' kindly make it private</b></p>
									  <p> Follow link (http://americanjetcargo.com/track_order.php) to use your track number to track your goods and know your order status.</p>
									  
									  <p>Thanks!</p>
									  <p>American Jet cargo</p>
									  <p>'.$DATE_NOW.'</p>
									</body>
								</html>';
			}else if ($status == "SHIPPED"){
			$message = '<html>
									<head>
									  <title>ORDER SHIPPED</title>
									</head>
									<body>
									  <p>Dear '.$fullname.',</p><br/><br/>
									  <h3>CONGRATULATIONS! YOUR ORDER HAS BEEN SHIPPED</h3>
									  <P>We appreciat your patronage and we are glad to inform you that your order has been shipped.</p>
									  <p>You will be updated from time to time as about your order details as it moves from one location to another.</p>
									  <p>Here is your <b>track number: '.$key.' kindly make it private</b></p>
									  <p> Follow link (http://americanjetcargo.com/track_order.php) to use your track number to track your goods and know your order status.</p>
									  
									  <p>Thanks!</p>
									  <p>American Jet cargo</p>
									  <p>'.$DATE_NOW.'</p>
									</body>
								</html>';
			}else if ($status == "ON HOLD"){
			$message = '<html>
									<head>
									  <title>ORDER ON HOLD</title>
									</head>
									<body>
									  <p>Dear '.$fullname.',</p><br/><br/>
									  <h3>CONGRATULATIONS! YOUR ORDER IS ON HOLD</h3>
									  <P>We appreciat your patronage and we are glad to inform you that your order is presently on hold.</p>
									  <p>Kindly contact us for more details.</p>
									  <p>Here is your <b>track number: '.$key.' kindly make it private</b></p>
									  <p> Follow link (http://americanjetcargo.com/track_order.php) to use your track number to track your goods and know your order status.</p>
									  
									  <p>Thanks!</p>
									  <p>American Jet cargo</p>
									  <p>'.$DATE_NOW.'</p>
									</body>
								</html>';
			}else if ($status == "SHIPPED"){
			$message = '<html>
									<head>
									  <title>ORDER SHIPPED</title>
									</head>
									<body>
									  <p>Dear '.$fullname.',</p><br/><br/>
									  <h3>CONGRATULATIONS! YOUR ORDER HAS BEEN SHIPPED</h3>
									  <P>We appreciat your patronage and we are glad to inform you that your order has been shipped.</p>
									  <p>You will be updated from time to time as about your order details as it moves from one location to another.</p>
									  <p>Here is your <b>track number: '.$key.' kindly make it private</b></p>
									  <p> Follow link (http://americanjetcargo.com/track_order.php) to use your track number to track your goods and know your order status.</p>
									  
									  <p>Thanks!</p>
									  <p>American Jet cargo</p>
									  <p>'.$DATE_NOW.'</p>
									</body>
								</html>';
			}else if ($status == "DELIVERED"){
			$message = '<html>
									<head>
									  <title>ORDER DELIVERED</title>
									</head>
									<body>
									  <p>Dear '.$fullname.',</p><br/><br/>
									  <h3>CONGRATULATIONS! YOUR ORDER HAS BEEN DELIVERED</h3>
									  <P>We appreciat your patronage and we are glad to inform you that your order has been deliver to repspective destination '.$c_destination.'.</p>
									  <p>Patiently wait for order to be reach you and be completed.</p>
									  <p>Here is your <b>track number: '.$key.' kindly make it private</b></p>
									  <p> Follow link (http://americanjetcargo.com/track_order.php) to use your track number to track your goods and know your order status.</p>
									  
									  <p>Thanks!</p>
									  <p>American Jet cargo</p>
									  <p>'.$DATE_NOW.'</p>
									</body>
								</html>';
			}else if ($status == "COMPLETED"){
			$message = '<html>
									<head>
									  <title>ORDER COMPLETED</title>
									</head>
									<body>
									  <p>Dear '.$fullname.',</p><br/><br/>
									  <h3>CONGRATULATIONS! YOUR ORDER IS COMPLETED</h3>
									  <P>We appreciat your patronage and we are glad to inform you that your order has been completed.</p>
									  <p>The <b>track number: '.$key.' has completed its task and cannot be used for another order </b></p>
									  <p> Follow link (http://americanjetcargo.com/) to contact us again for a new order if you intend at your convenience.</p>
									  
									  <p>Thanks!</p>
									  <p>American Jet cargo</p>
									  <p>'.$DATE_NOW.'</p>
									</body>
								</html>';
			}
			
				// If Duplicate Data Not Found
				sql($DBH, "UPDATE tbl_details set name =?, email =?, phone =?, item =?, destination =?, locationii =?, locationiii =?, address =?, postalcode =?, date_time =?, dt_stamp_end =?, success_status =?, success_status =?, status =? WHERE order_number = ? ", array($fullname, $email, $phone, $item, $destination, $locationii, $location, $address, $postalcode, $dt_stamp_end, $DATE_NOW, 1, $suc_status,$c_status,$order_number));
					// Subject
					$subject = 'AMERICAN JET CARGO';
					
					// Message
					$message;
					
					// To send HTML mail, the Content-type header must be set
					$headers[] = 'MIME-Version: 1.0';
					$headers[] = 'Content-type: text/html; charset=iso-8859-1';
					
					// Additional headers
					$headers[] = 'To: <'.$email.'>';
					$headers[] = 'From: AMERICAN JET CARGO <info@americanjetcargo.com>';
					/*$headers[] = 'Cc: birthdayarchive@example.com';
					$headers[] = 'Bcc: birthdaycheck@example.com';*/
					
					// Mail it
					mail($email, $subject, $message, implode("\r\n", $headers));

					$_SESSION['msg'] = "update";
					redirect('main.php');
		
	}
		catch(PDOException $e) { 
			file_put_contents('PDOErrors.txt', $e->getMessage(), FILE_APPEND); # Errors Log File
				$_SESSION['msg'] = "failed";
				redirect('new_order.php');
				
		}
	}
	else
	{
				$_SESSION['msg'] = "failed";
				redirect('new_order.php');
				
	}
?>