<?php 

$where 	= " WHERE 1=1 ";
$query_string = "";
$next_page_condition 	= "";
//echo "hiii";
if($_REQUEST['hide_top']){
	$next_page_condition.="&hide_top=1";
}
if($_REQUEST['hide_left']){
	$next_page_condition.="&hide_left=1";
}

if($_REQUEST['delivery_note_id']){
	$delivery_note_id = $_REQUEST['delivery_note_id'];
	$where .= " AND id = '$delivery_note_id' ";
}

// Query from delivery_notes table instead of sales_invoices
$sql = "SELECT * FROM delivery_notes $where ";
$rs = mysqli_query($conn,$sql);
$row = mysqli_fetch_assoc($rs);
$note_number = $row["note_number"];
$customer_id = $row["customer_id"];

// Get customer details
$customer_invoice = $gsi->getCustomerById($customer_id);

// Get customer address from client_management table
$customer_sql = "SELECT * FROM client_management WHERE id = '$customer_id'";
$customer_rs = mysqli_query($conn,$customer_sql);
$customer_row = mysqli_fetch_assoc($customer_rs);

$delivery_address = "";
if(isset($customer_row["delivery_address_1"]) && $customer_row["delivery_address_1"] !=""){
	$delivery_address .= $customer_row["delivery_address_1"]."<br/>";
}
if(isset($customer_row["delivery_address_2"]) && $customer_row["delivery_address_2"] !=""){
	$delivery_address .= $customer_row["delivery_address_2"]."<br/>";
}
if(isset($customer_row["delivery_address_3"]) && $customer_row["delivery_address_3"] !=""){
	$delivery_address .= $customer_row["delivery_address_3"]."<br/>";
}
if(isset($customer_row["delivery_postcode"]) && $customer_row["delivery_postcode"] !=""){
	$delivery_address .= $customer_row["delivery_postcode"];
}

// For print section, we can use the same address for both delivery and invoice
$invoice_address = $delivery_address;

$created_by = $gsi->getEmployeeById($row['created_by']); 
$ship_method = $gsi->getShipStatusById($row['ship_id']); 
$customer_code = utility::getTableFieldById("client_management","customer_code","id",$row['customer_id']);
$delivery_date = $row['delivery_date'];
$delivery_note_text = $row['delivery_note'];

$site_name = "Thermotec Plastivs Ltd";
?>
<style>
/* Keep all your existing print styles */
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{
		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, table.equip_table 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 new-page" 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="#" onsubmit="return validateForm(this)" enctype="multipart/form-data">
                  <div class="card-body" style="padding:0;">
					<div class="form-group">
						<div class="col-sm-12 no-padding">
							<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;">Delivery Note</h1></b>
									<b>Delivery Note : </b><? echo $note_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>
							<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;">DELIVERY TO</b> 
										<span style="padding: 5px;float:left;text-transform:uppercase;font-weight:bold;"><? echo $customer_invoice;?></span><br>
										<p style="padding: 5px;margin-bottom: 0;margin-top: 5px;">
										<? echo $delivery_address;?>
										</p>
									</div>
								</div>
								<div class="col-sm-4 ">
									&nbsp;
								</div>
								<div class="col-sm-4" style="">
									<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>Delivery Note : </strong>&nbsp;<? echo $note_number;?></p>
										<p><strong>Delivery Date:</strong>&nbsp;<? echo utility::getFormatedDate($delivery_date);?></p>
										
										<p><strong>Ship Method :</strong>&nbsp;<? echo $ship_method;?></p>
										<p><strong>Generated By :</strong>&nbsp;<? echo ucfirst($created_by);?></p>
										<p><strong>Page No:</strong>&nbsp;1 of 1</p>		
										</div>		
									</div>
								</div>
							</div>
							<?php
									// Query delivery_note_mapping table - group by part_number
									$delivery_item_mapping_query = "SELECT 
										sim.part_number,
										sim.description,
										SUM(sim.quantity) AS quantity,
										MAX(pp.RefSalesCode) AS RefSalesCode
									FROM delivery_note_mapping sim
									LEFT JOIN production_planning pp ON sim.order_number = pp.id
									WHERE sim.delivery_note_id='" . $delivery_note_id . "'
									GROUP BY sim.part_number, sim.description
									ORDER BY MIN(sim.id) ASC";

									$delivery_item_mapping_sql = mysqli_query($conn, $delivery_item_mapping_query);
									$delivery_item_mapping_count = mysqli_num_rows($delivery_item_mapping_sql);
									?>
							
							<div class="row mb-4">
								<div class="col-sm-12 col-12">
									<table class="equip_table">
										<thead>
										  <tr>
											<th style="text-align:center;">Item</th>
											<th style="width:20%;">Order Number</th>
											<th style="width:20%;">Part Number</th>
											<th style="width:30%;">Description</th>
											<th style="">Qty Delivered</th>
										  </tr>
										</thead>
										<tbody>
										<? 
										$i = 1; 
										if ($delivery_item_mapping_count > 0) {
											while ($row_item_mapping = mysqli_fetch_array($delivery_item_mapping_sql)) { 
												?>
												<tr>
													<td style="text-align:center;"><?php echo $i; ?></td>
													<td><?php echo $row_item_mapping['RefSalesCode']; ?></td>
													<td><?php echo $gsi->getPartNumberById($row_item_mapping['part_number']); ?></td>
													<td><?php echo stripslashes($row_item_mapping['description']); ?></td>
													<td style="text-align:center;"><?php echo $row_item_mapping['quantity']; ?></td>
												</tr>
												<?php
												$i++; // Increment row counter
											}
										}
										// Add empty rows if less than 15 rows
										if ($i <= 15) {
											for ($j = $i; $j <= 15; $j++) {
												?>
												<tr>
													<td></td>
													<td>&nbsp;</td>
													<td>&nbsp;</td>
													<td>&nbsp;</td>
													<td>&nbsp;</td>
												</tr>
												<?php
											}
										}
										?>
										</tbody>
									</table>
								</div>
							</div>
							
							<div class="row mb-2" style="">
								<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 CONTACT DETAILS</b> 
										<div style="padding: 5px; margin-right: 5px;">
											 <?$id = 1; // ID to fetch
													$query = "SELECT our_details FROM invoice_footer_detais WHERE id = $id";
													$result = mysqli_query($conn, $query);

													// Fetch the row
													$invoiceDetails = '';
													if ($result && mysqli_num_rows($result) > 0) {
														$row = mysqli_fetch_assoc($result);
														echo $invoiceDetails = $row['our_details'];
													}?>
										</div>
									</div>
								</div>
								<div class="col-sm-8">
								<div class="row mb-2">
									<div class="col-sm-12">
										<div style="border:2px solid #000;width: 100%;float: left;">
											<b style="display:block;text-transform:uppercase;border-bottom:2px solid #000;padding: 5px;">DELIVERY NOTE</b>
											<p style="padding: 5px;margin: 5px;">
												<? echo stripslashes($delivery_note_text);?>
											</p>
										</div>
									</div>
								</div>
									<div style="border: 2px solid #000; height: 250px; width: 100%; font-family: Arial, sans-serif; padding: 10px; margin: auto;">
										<p style="border-bottom: 2px solid #000; font-weight: bold; text-align: left; padding: 0px;">
											SIGN & DATE ON RECEIPT
										</p>
										<table style="width: 100%; border-spacing: 0; margin-top: 20px; font-size: 14px;">
											<tr>
												<td style="width: 20%; padding: 10px 5px;">Sign</td>
												<td style="width: 80%; border-bottom: 1px solid #000; height: 80px; vertical-align: bottom; padding: 10px 5px;"></td>
											</tr>
											<tr>
												<td style="width: 20%; padding: 10px 5px;">Print</td>
												<td style="width: 80%; border-bottom: 1px solid #000; padding: 10px 5px;"></td>
											</tr>
											<tr>
												<td style="width: 20%; padding: 10px 5px;">Date</td>
												<td style="width: 80%; padding: 10px 5px;"></td>
											</tr>
										</table>
									</div>
								</div>
							</div>
						</div>
					</div>
                  </div>
                </form>
              </div>
		</div>
	</div>
</section>