<?php 
$where        = " WHERE 1=1 ";
$query_string = "";

if($_REQUEST['hide_top'])  { $next_page_condition .= "&hide_top=1"; }
if($_REQUEST['hide_left']) { $next_page_condition .= "&hide_left=1"; }

if($_REQUEST['po_id']){
	/*
	 * po_id arrives encrypted from the URL (utility::app_crypt + urlencode).
	 * Decrypt it before using in ANY query.
	 */
	$po_id  = utility::app_crypt($_REQUEST['po_id'], 'd');
	$po_id  = intval($po_id);   // cast to int for safety
	$where .= " AND id = '$po_id' ";
}

$rs  = mysqli_query($conn, "SELECT * FROM purchase_order $where ");
$row = mysqli_fetch_assoc($rs);

$purchase_order_number = $row["order_ref"];
$pu_supplier_id        = $gsi->getSuppliersById($row["supplier_id"]);
$created_by            = $gsi->getEmployeeById($row['created_by']);
$supplier_id           = $row['supplier_id'];
$created_on            = $row['created_on'];
$order_notes           = $row['order_notes'];
$deliver_to            = $row['deliver_to'];
$site_name             = "Thermotec Plastivs Ltd";

$supplier_rs      = mysqli_query($conn, "SELECT id,address_1,address_2,address_3,supplier_postcode FROM supplier_management WHERE id = '$supplier_id'");
$supplier_address = "";
if(mysqli_num_rows($supplier_rs) > 0){
	$supplier_row = mysqli_fetch_assoc($supplier_rs);
	$supplier_row = utility::validate_crypt_data($supplier_row);
	if(!empty($supplier_row["address_1"]))         $supplier_address .= $supplier_row["address_1"]."<br/>";
	if(!empty($supplier_row["address_2"]))         $supplier_address .= $supplier_row["address_2"]."<br/>";
	if(!empty($supplier_row["address_3"]))         $supplier_address .= $supplier_row["address_3"]."<br/>";
	if(!empty($supplier_row["supplier_postcode"])) $supplier_address .= $supplier_row["supplier_postcode"];
}
?>
<style>
body{ font-size:12px; }
table.equip_table{ width:100%; border-collapse:collapse; margin:15px 0; border:2px solid #000; }
table.equip_table_3{ width:100%; border-collapse:collapse; margin:15px 0; border:2px solid #000; }
table.equip_table_3, table.equip_table_3 td { border:2px solid #000; padding:10px; }
table.equip_table, table.equip_table th { border:2px solid #000; padding:10px; }
table.equip_table td { border-right:2px solid #000; padding:10px; }
.col-sm-1,.col-sm-2,.col-sm-3,.col-sm-4,.col-sm-5,.col-sm-6,
.col-sm-7,.col-sm-8,.col-sm-9,.col-sm-10,.col-sm-11,.col-sm-12{ float:left; }
.col-sm-12{width:100%}.col-sm-11{width:91.66666667%}.col-sm-10{width:83.33333333%}
.col-sm-9{width:75%}.col-sm-8{width:66.66666667%}.col-sm-7{width:58.33333333%}
.col-sm-6{width:50%}.col-sm-5{width:41.66666667%}.col-sm-4{width:33.33333333%}
.col-sm-3{width:25%}.col-sm-2{width:16.66666667%}.col-sm-1{width:8.33333333%}
@page{ size:A4; margin:0; }
@media print{
    table.equip_table,table.equip_table th,table.equip_table td{ border:2px solid #000; padding:10px; }
    table.equip_table_3,table.equip_table_3 td{ border:2px solid #000; padding:10px; }
    .col-sm-1,.col-sm-2,.col-sm-3,.col-sm-4,.col-sm-5,.col-sm-6,
    .col-sm-7,.col-sm-8,.col-sm-9,.col-sm-10,.col-sm-11,.col-sm-12{ float:left; }
    .col-sm-12{width:100%}.col-sm-11{width:91.66666667%}.col-sm-10{width:83.33333333%}
    .col-sm-9{width:75%}.col-sm-8{width:66.66666667%}.col-sm-7{width:58.33333333%}
    .col-sm-6{width:50%}.col-sm-5{width:41.66666667%}.col-sm-4{width:33.33333333%}
    .col-sm-3{width:25%}.col-sm-2{width:16.66666667%}.col-sm-1{width:8.33333333%}
}
</style>

<section class="content" id="print_section">
	<div class="row">
		<div class="col-lg-12">
			<div class="card box-primary" style="box-shadow:none;">
				<form class="form-horizontal" method="post" name="form1" id="form1" action="#" enctype="multipart/form-data">
				  <div class="card-body" style="padding:0;">
					<div class="form-group">
						<div class="col-sm-12 no-padding">

							<!-- Header -->
							<div class="row mb-2">
								<div class="col-sm-4">
									<img src="../images/thermo_po.jpg" style="width:280px;margin-top:0;"/>
								</div>
								<div class="col-sm-4" style="text-align:center;">
									<b><h1 style="text-align:center;margin-top:0;font-weight:bold;">PURCHASE ORDER</h1></b>
									<b>Order No: </b><?= $purchase_order_number; ?>
								</div>
								<div class="col-sm-4">&nbsp;</div>
								<div class="col-sm-12" style="text-align:center;">
									<hr style="margin-top:15px;border-top:2px solid #000;" />
								</div>
							</div>

							<!-- Address boxes -->
							<div class="row mb-2">
								<div class="col-sm-4">
									<div style="border:2px solid #000;height:245px;width:100%;float:left;">
										<b style="display:block;text-transform:uppercase;border-bottom:2px solid #000;padding:5px;">SUPPLIER Details</b> 
										<span style="padding:5px;float:left;text-transform:uppercase;font-weight:bold;"><?= $pu_supplier_id; ?></span><br>
										<p style="padding:5px;margin:5px 0 0;"><?= $supplier_address; ?></p>
									</div>
								</div>
								<div class="col-sm-4">
									<div style="border:2px solid #000;height:245px;width:100%;float:left;">
										<b style="display:block;text-transform:uppercase;border-bottom:2px solid #000;padding:5px;">DELIVERY To</b>
										<p style="padding:5px;margin:5px 0 0;line-height:2;"><strong><?= $site_name; ?></strong><br/>
										<div style="padding:5px;">
											<p style="margin:1px 0;">Electra Park Industrial Estate</p>
											<p style="margin:2px 0;">Electric Avenue</p>
											<p style="margin:2px 0;">Witton</p>
											<p style="margin:2px 0;">Birmingham</p>
											<p style="margin:2px 0;">B6 7EB</p>
										</div></p>
									</div>
								</div>
								<div class="col-sm-4">
									<div style="border:2px solid #000;height:245px;width:100%;float:left;">
										<b style="display:block;text-transform:uppercase;border-bottom:2px solid #000;padding:5px;">DELIVERY Details</b><br>
										<div style="padding:5px;margin-right:5px;">
											<p><strong>Our Order: </strong>&nbsp;<?= $purchase_order_number; ?></p>
											<p><strong>Order Issue:</strong>&nbsp;<?= $order_issue; ?></p>
											<p><strong>Order Date:</strong>&nbsp;<?= utility::getFormatedDate($created_on); ?></p>
											<p><strong>Placed By:</strong>&nbsp;<?= $created_by; ?></p>
											<p><strong>Page No:</strong>&nbsp;<?= $page_no; ?></p>
										</div>		
									</div>
								</div>
							</div>

							<?php
								$final_total = 0;

								/*
								 * Fetch all PO lines with the freetext part number joined in.
								 * $po_id is already decrypted and cast to int at the top of this file.
								 *
								 * resolved_part_number:
								 *   item_type 6  → the string from purchase_order_freetext_parts
								 *   everything else → the integer ID from purchase_order_items_mapping
								 */
								$purchaseorder_item_mapping_query = "
									SELECT m.*,
									       CASE
									           WHEN m.item_type = 6 THEN IFNULL(fp.part_number, '')
									           ELSE m.part_number
									       END AS resolved_part_number
									FROM purchase_order_items_mapping m
									LEFT JOIN purchase_order_freetext_parts fp ON fp.po_item_id = m.id
									WHERE m.po_id = '$po_id'
									ORDER BY m.ordering ASC
								";
								$purchaseorder_item_mapping_sql   = mysqli_query($conn, $purchaseorder_item_mapping_query);
								$purchase_item_mapping_count      = mysqli_num_rows($purchaseorder_item_mapping_sql);
							?>

							<!-- Items table -->
							<div class="row mb-4">
								<div class="col-sm-12 col-12">
									<table class="equip_table">
										<thead>
										  <tr>
											<th style="text-align:center;">Line</th>
											<th style="text-align:center;">Part Number</th>
											<th style="width:30%;">Description</th>
											<th>Qty</th>
											<th>Required</th>
											<th style="text-align:center;">Unit</th>
											<th style="text-align:center;">Total</th>
										  </tr>
										</thead>
										<tbody>
										<?php
										if($purchase_item_mapping_count > 0){
										    while($row_po_item_mapping = mysqli_fetch_array($purchaseorder_item_mapping_sql)){
											    $unit_price = $row_po_item_mapping["unit_price"];
											    $qty        = $row_po_item_mapping["quantity"];
											    $total      = $qty * $unit_price;
										?>
										  <tr>
											<td><?= $row_po_item_mapping['ordering'] . '.'; ?></td>
											<td style="text-align:center;">
												<?php
												/*
												 * item_type 6 → show the freetext string directly
												 *               (resolved_part_number = fp.part_number from JOIN)
												 * everything else → pass the integer ID to the normal lookup
												 */
												if($row_po_item_mapping['item_type'] == 6){
													echo htmlspecialchars($row_po_item_mapping['resolved_part_number']);
												} else {
													echo $gsi->getPartNumberById($row_po_item_mapping['resolved_part_number']);
												}
												?>
											</td>
											<td><?= stripslashes($row_po_item_mapping['description']); ?></td>
											<td><?= $row_po_item_mapping['quantity']; ?></td>
											<td style="text-align:center;"><?= utility::getFormatedDate($row_po_item_mapping['expected_date']); ?></td>
											<td style="text-align:center;"><?= $row_po_item_mapping['unit_price']; ?></td>
											<td style="text-align:center;"><?= $total; ?></td>
										  </tr>
										<?php
										        $final_total += $total;
										    }
										} ?>
										</tbody>
									</table>
								</div>
							</div>

							<!-- Notes + Documents -->
							<div class="row mb-4">
								<div class="col-sm-5">
									<div style="border:2px solid #000;height:180px;width:100%;float:left;">
										<b style="display:block;text-transform:uppercase;border-bottom:2px solid #000;padding:5px;">ORDER NOTES</b> 
										<p style="padding:5px;margin:5px 0 0;"><?= $order_notes; ?></p>
									</div>
								</div>
								<div class="col-sm-2"><p>&nbsp;</p></div>
								<div class="col-sm-5">
									<div style="border:2px solid #000;height:180px;width:100%;float:left;">
										<b style="display:block;text-transform:uppercase;border-bottom:2px solid #000;padding:5px;">THE FOLLOWING DOCUMENTS MUST BE PROVIDED:</b><br>
										<p><?= $documents; ?></p>				
									</div>
								</div>
							</div>

							<!-- Spacer -->
							<div class="row mb-2">
								<?php for($s=0;$s<10;$s++){ echo '<div class="col-sm-12">&nbsp;</div>'; } ?>
							</div>

							<!-- Footer -->
							<div class="row mb-2">
								<div class="col-sm-4">
									<div style="border:2px solid #000;height:100%;width:100%;float:left;">
										<b style="display:block;text-transform:uppercase;border-bottom:2px solid #000;padding:5px;">OUR DETAILS</b> 
										<div style="padding:5px;margin-right:5px;">
											<p style="margin:2px 0;">Thermotec Plastics Ltd</p>
											<p style="margin:2px 0;">Electra Park Industrial Estate</p>
											<p style="margin:2px 0;">Electric Avenue</p>
											<p style="margin:2px 0;">Witton</p>
											<p style="margin:2px 0;">Birmingham</p>
											<p style="margin:2px 0;">B6 7EB</p>
											<p><strong>Tel:&nbsp;&nbsp;0121 327 7800</strong></p>
											<p><strong>Fax:&nbsp;&nbsp;0121 327 4799</strong></p>
											<p><strong>Mail:&nbsp;&nbsp;info@thermotecplastics.co.uk</strong></p>
											<p><strong>Reg:&nbsp;&nbsp;00897776</strong></p>
										</div>
									</div>
								</div>
								<div class="col-sm-4">
									<p style="text-align:left;">
										<br/>
										Please acknowledge order to confirm<br/>
										price and delivery dates.<br/>
										Buyer email: <a href="mailto:paul.raybould@thermotecplastics.co.uk">paul.raybould@thermotecplastics.co.uk</a><br/><br>
										All documentation must refer to our order number<br/>
										035553.<br/><br>
										Terms &amp; Condition Available Upon Request.
									</p>
								</div>
								<div class="col-sm-4">
								    <div style="border:2px solid #000;height:140px;width:100%;float:left;margin-top:18px;text-align:right;">
										<b style="display:block;text-transform:uppercase;border-bottom:2px solid #000;padding:5px;">Total IN GBP - PRICES EXCLUDE VAT</b> 
										<p style="padding:5px;margin:5px 0 0;font-weight:bolder;font-size:2em;">
											<strong>£<?= number_format($final_total, 2); ?></strong>
										</p>
									</div>
									<div style="border:2px solid #000;height:140px;width:100%;float:left;margin-top:18px;text-align:right;">
										<div style="padding:5px;margin-right:5px;">
											<p style="margin:2px 0;"><strong>Delivery Terms:</strong></p>
											<p style="margin:2px 0;">DDP Delivery Duty Paid (Delivery Address)</p>
											<br>
											<p><strong>Payment Terms:</strong></p>
										</div>
									</div>
								</div>
							</div>

						</div>
					</div>
				  </div>
				</form>
			</div>
		</div>
	</div>
</section>