<?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'] == "Confirm")
	{
		try { 
			// INSERT RECORD
			$fullname			= strip_tags($_POST['fullname']);
			$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']);
			$locationi	    		= strip_tags($_POST['locationi']);
			$locationii	    		= strip_tags($_POST['locationii']);
			$address	    		= strip_tags($_POST['address']);
			$postalcode	    		= strip_tags($_POST['postalcode']);
			$status	    		= strip_tags($_POST['status']);
			
			$rand = (rand());
			$key = $rand;
			
			
			$rows1 = sql($DBH, "SELECT * FROM tbl_user where email = ?", array("$s_email"), "rows");							
			foreach($rows1 as $row1)
			{	
				$s_name	   		= $row1['fullname'];
				$s_phone	   		= $row1['phone'];
				$s_mail   		= $row1['email'];
				
			if($s_mail == $s_email){
			
				// If Duplicate Data Not Found
				sql($DBH,
					"INSERT INTO tbl_details (order_number, name, email, phone, item, destination, locationi, locationii, sales_rep, sales_repphone, sales_repemail, address, postalcode, date_time, dt_stamp_end, success_status, status)
					VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
					array($key,$fullname, $email, $phone, $item, $destination, $locationi, $locationi, $s_name, $s_phone, $s_mail, $address, $postalcode, $DATE_NOW, $DATE_NOW, 1, $status));
					

					// Subject
					$subject = 'AMERICAN JET CARGO';
					
					// Message
					$message = '<html>
									<head>
									  <title>YOUR ORDER RECEIVED</title>
									</head>
									<body>
									  <p>Dear '.$fullname.',</p><br/><br/>
									  <h3>CONGRATULATIONS! YOUR ORDER IS RECEIVED</h3>
									  <P>We appreciat your patronage and we are glad to inform you that your order has been received.</p>
									  <p>You will be updated from time to time about your order 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>';
					
					// 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'] = "signup";
					redirect('new_order.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');
				
	}
?>