<?php
function getSAID($so_id){
	global $db;
	
	$db->bind('so_id', $so_id);
	$data = $db->query("SELECT shipment_arrangement_id FROM shipment_arrangement_items WHERE so_id = :so_id");
	
	return $data[0]['shipment_arrangement_id'];
	
}
function getOrderSelectionCount(){
	global $db;
	$db->bind("company_id", $_SESSION['company_id']);
	$orderselections = $db->query("SELECT * FROM shipping_orders where forwarder_id = :company_id and so_id in (select so_id from warehouse_arrangements where 1)");
	
	return count($orderselections);
}
function getSAData($so_id, $data){
	global $db;
	
	$SAID = getSAID($so_id);
	$db->bind('id', $SAID);
	
	$shipment_arrangements = $db->query("SELECT * FROM shipment_arrangements WHERE id = :id");
	
	return (count($shipment_arrangements) > 0 ? $shipment_arrangements[0][$data] : "");
	
}

function getSAItems($id){
	global $db;
	$db->bind('shipment_arrangement_id', $id);
	$data = $db->query("SELECT * FROM shipment_arrangement_items WHERE shipment_arrangement_id = :shipment_arrangement_id");
	

    return $data;
}
function getModalSACustomsExamination($id){
	global $db;
	
	$db->bind('id', $id);
	$shipment_arrangements = $db->query("SELECT * FROM shipment_arrangements WHERE id = :id");
	$x = 1;
	$html = "";
	$eta = $shipment_arrangements[0]["eta"];
	$etd = $shipment_arrangements[0]["etd"];
	$release_date = $shipment_arrangements[0]["release_date"];
	$SAItems = getSAItems($id);
	
	$html .= '<table class="table table-bordered table-hover">
				<thead>
					<tr>
						<th width="5">No</th>
						<th width="10">Date</th>
						<th width="10">Order No</th>						
					</tr>
				</thead>
				<tbody>';
				foreach($SAItems as $key => $SAItem){
					$html .= '<tr>'
								.'<td>'.$x .'</td>'
								.'<td>'.date("d/m/Y", strtotime(getSOData($SAItem['so_id'], "created_on"))) .'</td>'
								.'<td>'.getSOData($SAItem['so_id'], "marking_no") .'</td>'
							.'</tr>';

							$x++; 
						}
	$html .= "</tbody>"
			."</table>"
			."<h4 class='block'>Customs Examination Departure Port</h4>"
			.'<div class="row">										
				<div class="col-md-12">
					<div class="col-md-6">
						<div class="form-group">
							<label class="control-label col-md-4">New ETD</label>
							<div class="col-md-8">
								<input class="form-control date-picker" id="etd" name="etd" type="text" value="'.$etd.'">
								<input class="form-control" id="id" name="id" type="text" value="'.$id.'" style="display:none;">
							</div>
						</div>
						<div class="form-group">
							<label class="control-label col-md-4">New ETA</label>
							<div class="col-md-8">
								<input class="form-control date-picker" id="eta" name="eta" type="text" value="'.$eta.'">
							</div>
						</div>
					</div>
				</div>
			</div>
			<h4 class="block">Customs Examination Arrival Port</h4>
			<div class="row">										
				<div class="col-md-12">
					<div class="col-md-6">
						<div class="form-group">
							<label class="control-label col-md-4">Release Date</label>
							<div class="col-md-8">
								<input class="form-control date-picker" id="release_date" name="release_date" type="text" value="'.$release_date.'">
							</div>
						</div>
					</div>
				</div>
			</div>';

	return $html;

}