<?php
include_once('connect.php');
include_once('functions.php');
//setup order object
$estimate = new Estimate($_REQUEST['id']);
$key = $_REQUEST['key'];
if($estimate->getLinkKey() != $key){
	header("Location: index.php");
}
//var_dump($estimate);
if($estimate->DeleteEstimate){
	die($estimate->HoldDeleteReason);
}

 ?>

 <!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">
		 <meta name="robots" content="noindex">
     <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"/>
	<style>
		@media print {
			body {
				font-size: small;
			}
			#estimateTable {
				font-size: .7em;
			}
			#logo{
				width: 125px;
			}
		}
	</style>
   </head>
   <body style="margin-top: -50px;">


   <!-- 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">
			<span><h2><img src="./img/Logo_250.png" /></h2></span>
	  	<!--<img id="logo" class="img-responsive" style="width: 100px;" src="<?= $root ?>img/<?= $config['Logo'] ?>" alt="" />-->
	  	<p>
	  		<b>Name: </b><?= $estimate->Customer->Name; ?><br>
			<b>Address:<br>
			</b><?= $estimate->Customer->Address1?$estimate->Customer->Address1 . "<br>":""; ?>
			<b></b><?= $estimate->Customer->Address2?$estimate->Customer->Address2 . "<br>":""; ?>
	  		<b></b><?= $estimate->Customer->City . ", " .  $estimate->Customer->State . " " . $estimate->Customer->ZipCode ; ?><br>
	  		<b>Customer #: </b><?= $estimate->Customer->AccountNumber ?><br />


	  </p>

	  </div>
 	  <div class="col-xs-2"></div>
	  <div class="col-xs-5">
		  <h3>Estimate Confirmation</h3>
		  <p>
		  	<b>Estimate Number: </b><span style="font-style: italic;"><?= $estimate->EstimateID ?></span><br>

		  	<b>Estimate By: </b> <?= $estimate->Salesman->Name ?><br>
		  <b>Date: </b><?= date("m/d/Y",strtotime($estimate->EstimateDate)) ?><br>
		  <b>Time: </b><?= date("g:i a",strtotime($estimate->EstimateDate)) ?><br>

		  </p>
	  </div>
	</div>
	<div class="row">
		<br>

			<span style="font-size: large;">Saludos <?= $estimate->Customer->ContactTitle ?> <?= $estimate->Customer->Contact ?>,<br>
			<br>
			Gracias por la oportunidad que nos brinda de poder cotizarle nuestros productos Westinghouse. A continuación encontrará el listado de los productos que ofrecemos.
			Si le surge alguna duda, puede comunicarse de inmediato al correo electr&oacute;nico: <?= $config['OrderEmail'] ?>.
			<br>Quedamos a sus &oacute;rdenes.
			<br>
			<br>
			<br>
			<br>
			<br>
			<br>
			<b><?= $estimate->Salesman->Name ?></b><br>
			<?= formatPhone($estimate->Salesman->Phone) ?><br>
			<?= $config['CompanyName'] ?><br>
			<?= $root ?></span>
<br>
<br>
<br>
	  <table id="estimateTable" class="table table-condensed">
	    <thead>
	      <tr>
	        <th style="width: 5%">Line<br>Group</th>
	        <th style="width: 70%">Item</th>
	        <th style="width: 5%; text-align: center;">Qty</th>
					<!--<th style="width: 5%;">MSRP</th>-->
	        <th style="width: 5%">Price</th>
	        <th style="width: 5%">Extension</th>
	      </tr>
	    </thead>
	  	<tbody>
	  	<?php
	  		$query = "SELECT * FROM ESTIMATE_DETAIL WHERE EstimateID = '{$estimate->EstimateID}' ORDER BY ID";
	  		$result = mysqli_query($con, $query);
	  		$index = 0;
				$itemImages = [];
	  		while ($row = mysqli_fetch_array($result)) {
					$item = new Item(null, $row['Line'], $row['SKU']);
					array_push($itemImages, $item);
	  	?>
	  		<tr>
	    			<td>
	        			<span class="itemInfo"><?= $row['Line'] ?></span>
	        			<span class="itemInfo"><?= $row['Group'] ?></span>
	        		</td>
	        		<td>
	        			<span class="itemInfo"><b><?= $row['SKU'] ?></b></span><br />
	        			<span class="itemInfo"><?= $row['Description'] ?></span>
	        			<span class="itemInfo"><?= $row['Notes'] . $item->getAttribute('Master Carton')?(" / Box: <b>" . $item->getAttribute('Master Carton') . "</b> units"):"" ?>

						<?php
						$upc = $item->getUPC();
						if($upc){
							echo " / UPC: " . $upc;
						}
						?>
						</span>
	        		</td>
	        		<td class="centered">
	        			<?= $row['EstimateQty'] ?>
	        		</td>
							<!--<td>
									<?= moneyFormat($item->MSRP) ?>
							</td>-->
	        		<td>
	        			<?= moneyFormat($row['Price']); ?>
	        			<?php
	        				if(floatval($row['Core'])>0){
	        					echo "<br><i><b>Core:</b> ". moneyFormat($row['Core']) . "</i>";
	        				}
	        			?>
	        		</td>
	        		<td style="text-align: right;">
	        			<?= moneyFormat($row['EstimateQty'] * $row['Price']) ?>
	        			<?php
	        				if(floatval($row['Core'])>0){
	        					echo "<br><i><b>Core:</b> ". moneyFormat($row['EstimateQty'] * $row['Core']) . "</i>";
	        				}
	        			?>
	        		</td>
	      	</tr>
	      	<?php
	      	$index++;
	      	}?>
	      	<tr>
	      		<td></td>
	      		<td colspan="3" style="text-align: right;"><span style="font-weight: bold;  text-align: right;">Subtotal:</span></td>
	      		<td style="font-weight: bold; text-align: right;"><span><?= moneyFormat($estimate->EstimateTotal) ?></span></td>
	      	</tr>
					<tr>
	      		<td></td>
	      		<td colspan="3" style="text-align: right;">
	      		<span style="font-weight: bold;"><?php
						$tax1 = $estimate->EstimateTotal*($estimate->Customer->TaxExempt1?0:$config['Tax1Rate']);
						echo $config['Tax1Name'] . " (" . round($config['Tax1Rate']*100,1) . "%):" ?></span></td>
	      		<td style="font-weight: bold; text-align: right;"><span style="font-weight: bold; text-align: right;"><?= moneyFormat($tax1) ?></span></td>
	      	</tr>
					<tr>
	      		<td></td>
	      		<td colspan="3" style="text-align: right;">
	      		<span style="font-weight: bold;"><?php
						$tax2 = $estimate->EstimateTotal*($estimate->Customer->TaxExempt2?0:$config['Tax2Rate']);
						echo $config['Tax2Name'] . " (" . round($config['Tax2Rate']*100,1) . "%):" ?></span></td>
	      		<td style="font-weight: bold; text-align: right;"><span ><?= moneyFormat($tax2) ?></span></td>
	      	</tr>
					<tr>
	      		<td></td>
	      		<td colspan="3" style="text-align: right;"><span style="font-weight: bold; text-align: right;">Total:</span></td>
	      		<td style="font-weight: bold; text-align: right;"><span style="font-weight: bold; text-align: right;"><?= moneyFormat($estimate->EstimateTotal+$tax1+$tax2) ?></span></td>
	      	</tr>
	      </tr>
	    </tbody>
	  </table>

	</div>


</div>
       <div class="panel panel-default">
         <div class="panel-body">
           <span><b>Estimate Notes</b></span><br />
           <?= $estimate->EstimateNotes ?>

         </div>
       </div>
     <?php
		 if($estimate->IncludeProductImage){

			?>
			<div class="row">
				<?php
				if($_REQUEST['test']){
					//var_dump($itemImages);
				}
				foreach($itemImages as $key=>$item){

					$imageUrl = $item->getImage();
				?>
				<div class="col-xs-3">
						<center>
							<span><b><?= $item->SKU ?></b></span><br>
							<img src="<?= $imageUrl?>" style="height: 200px;"/>
							<br><br><br>
						</center>
				</div>
				<?php
				}
				?>
			</div>
			<?php
		 }
	 if($estimate->ImageURL){
	?>
		<center>
		<img src="<?= $estimate->ImageURL  ?>" style="width: 60%;"/>
		</center>
	<?php

	 }
	 ?>
   </body>
 </html>
 <?php
 //sendEstimate($estimate->EstimateID);
  ?>
