<?php 
if (!isset($_POST['customerNumber'])) {
	header("Location: index.php");
	exit();
}
include("session.php");
include("connect.php");
global $con, $config;
$isCustomer = $_SESSION['isCustomer'];
if(!$isCustomer){
	//If there is no items in the shopping cart, redirect to index.php
	$query = "SELECT id FROM PICKUP_CART WHERE SalesmanID LIKE '" . $_SESSION['SalesmanID'] . "'";
	$result = mysqli_query($con, $query);
	$rows = mysqli_num_rows($result);
	if($rows == 0){
		header("Location: pickup.php");
		exit;
	}
	$salesman = new Salesman($_SESSION['SalesmanID']);
	$customer = new Customer($_POST['customerNumber']);
}else {
	//header("Location: index.php");
	exit;
	
}

//clean up post entries
$pickupNotes = mysqli_real_escape_string($con, $_POST['pickupNotes']);
$packageAmount = mysqli_real_escape_string($con, $_POST['packageAmount']);
$pickupTicket = mysqli_real_escape_string($con, $_POST['pickupTicket']);
$requestedBy = mysqli_real_escape_string($con, $_POST['requestedBy']);
$pickupVia = mysqli_real_escape_string($con, $_POST['pickupVia']);
$_SESSION['pickupNotes'] = "";
$_SESSION['pickupTicket'] = "";
//creating new entry in orders db

if($pickupVia == "Vendedor"){
$query = "INSERT INTO PICKUP_HEAD (AccountNumber, PickupDate, Salesman, PickupNotes, Packages, RequestedBy, Via, ToPickup, ToPickupUser) VALUES ('$customer->AccountNumber', DATE_ADD(NOW(), INTERVAL -4 HOUR), '" . $salesman->ID . "', '$pickupNotes', $packageAmount, '$requestedBy', '$pickupVia', DATE_ADD(NOW(), INTERVAL -4 HOUR), '" . $salesman->Username . "')";	
}else{
	$query = "INSERT INTO PICKUP_HEAD (AccountNumber, PickupDate, Salesman, PickupNotes, Packages, RequestedBy, Via) VALUES ('$customer->AccountNumber', DATE_ADD(NOW(), INTERVAL -4 HOUR), '" . $salesman->ID . "', '$pickupNotes', $packageAmount, '$requestedBy', '$pickupVia')";
}


if(mysqli_query($con, $query)){
	$pickupID = mysqli_insert_id($con);	
}else{
	die(mysqli_error($con));
}


//with inserted entry id insert items in pickup_detail table
$query = "INSERT INTO PICKUP_DETAIL (`PickupID`, `Line`, `SKU`, `Qty`, `InvoiceNumber`, `OrderID`, `Reason`, `WarrantyReason`) ";

	$select = "SELECT $pickupID, `Line`, `SKU`, `Qty`, `InvoiceNumber`, `OrderID`, `Reason`, `WarrantyReason` FROM PICKUP_CART WHERE `SalesmanID` = '" . $salesman->ID . "' ORDER BY id";
	$query .= $select;
	

mysqli_query($con, $query);

$query = "DELETE FROM PICKUP_CART WHERE `SalesmanID` = '" . $salesman->ID . "'";


mysqli_query($con, $query);

//setup order object
$pickup = new Pickup($pickupID);


 ?>
 
 <!DOCTYPE html>
 <html lang="en">
   <head>
     <meta charset="utf-8">
     <meta http-equiv="X-UA-Compatible" content="IE=edge">
     <meta name="viewport" content="width=device-width, initial-scale=1">
     <title><?= $config['CompanyName'] ?></title>
 
     <!-- Bootstrap -->
     <link href="<?= $root ?>css/bootstrap.min.css" rel="stylesheet">
     <link rel="stylesheet" href="<?= $root ?>css/bootstrap-combobox.css" />
 
     <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
     <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
     <!--[if lt IE 9]>
       <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
       <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
       <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
           <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
           <!-- Include all compiled plugins (below), or include individual files as needed -->
           <script src="<?= $root ?>js/bootstrap.min.js"></script>
       	 <script src="<?= $root ?>js/bootstrap-combobox.js"></script>
           <script type="text/javascript">
             $(document).ready(function(){
               $('.combobox').combobox();
			   
			   
       //        $('.grpline').css("width", "100px");
             });
           </script>

     <![endif]-->
	<link rel="stylesheet" href="<?= $root ?>css/styles.css"/>
   </head>
   <body>
   
       <?php 
       		include("navbar.php"); 
       		

       ?>     

   
   <!-- Stack the columns on mobile by making one full-width and the other half-width -->
<div class="container-fluid" style="padding-top: 5px;">
	<div class="row">
	  <div class="col-xs-5">
	  	<img class="img-responsive" src="<?= $root ?>img/<?= $config['Logo'] ?>" alt="" />
	  	<p>
	  		<b>Customer Name: </b><?= $pickup->Customer->Name; ?><br>
	  		<b>City: </b><?= $pickup->Customer->City; ?><br>
	  		<b>Customer #: </b><?= $pickup->Customer->AccountNumber ?><br />
	  		<b>Requested By: </b><?= $pickup->RequestedBy ?><br />
	  		<b>Packages: </b><?= $pickup->Packages ?>
	  		
	  
	  </p>
	  
	  </div>
 	  <div class="col-xs-2"></div>
	  <div class="col-xs-5">
		  <h3>Pickup and Return Request</h3>
		  <p>
		  	<b>Pickup Number: </b><span style="font-style: italic;"><?= $pickup->PickupID ?></span><br>
			<b>Pickup Ticket: </b><?= $pickup->PickupTicket ?><br>
		  <b>Date: </b><?= date("m/d/Y",strtotime($pickup->PickupDate)) ?><br>
		  <b>Time: </b><?= date("g:i a",strtotime($pickup->PickupDate)) ?><br>
		  <b>Salesman: </b> <?= $pickup->Salesman->ID . " - " . $pickup->Salesman->Name ?><br>
		  </p>
	  </div>
	  <table class="table table-condensed">
	    <thead>
	      <tr>
	        <th style="width: 5%">Line</th>
	        <th style="width: 70%">Item</th>
	        <th style="width: 5%">Quantity</th>
	        <th style="width: 5%">Invoice Number</th>
	        <th style="width: 5%">Reason</th>
	      </tr>
	    </thead>
	  	<tbody>
	  	<?php
	  		$query = "SELECT * FROM PICKUP_DETAIL WHERE PickupID = '$pickupID'";
	  		$result = mysqli_query($con, $query);
	  		$index = 0;
	  		while ($row = mysqli_fetch_array($result)) {
	  			$item = new Item('%', $row['Line'], $row['SKU']);
	  	?>
	  		<tr>
	    			<td>
	        			<span class="itemInfo"><?= $row['Line'] ?></span>
	        		</td>
	        		<td>
	        			<span class="itemInfo"><b><?= $row['SKU'] ?></b></span><br />
	        			<span class="itemInfo"><?= $item->Description ?></span>
	        			<span class="itemInfo"><?= $item->Notes ?></span>
	        		</td>
	        		<td class="centered">
	        			<?= $row['Qty'] ?>
	        		</td>
	        		<td>
	        			<?= $row['InvoiceNumber'] ?>
	        		</td>
	        		<td style="text-align: right;">
	        			<?= getReasonText($row['Reason']) ?>
	        		</td>
	      	</tr>
	      	<?php 
	      	$index++;	 
	      	}?>
	      	
	    </tbody>
	  </table>
	  
	</div>
	

</div>
       <div class="panel panel-default">
         <div class="panel-body" style="float: left; width: 75%;">
           <span><b>Pickup Notes</b></span><br />
           <?= $pickup->PickupNotes ?>
           <br />
           
           
         </div>
         <div class="panel-body" style="float: right; width: 20%;">
			<span><b>Via: </b><?= $pickup->Via ?></span>
         </div>
       </div>
     <script type="text/javascript">
		var receiptWin = window.open("pickupreceipt.php?id=" + <?= $pickupID ?>, <?= $pickupID ?> ,'toolbar=no,width=415,height=1000');					
		if(!receiptWin || receiptWin.closed || typeof receiptWin.closed=='undefined') {
			receiptWin = window.open("pickupreceipt.php?id=" + <?= $pickupID ?>, <?= $pickupID ?> ,'toolbar=no,width=415,height=1000');					
		}
		setTimeout(function(){
			window.location = "index.php";
			},2000);
	 </script>
   </body>
 </html>
 <?php 
 sendPickup($pickupID);
  ?>