<?php

global $db;
ini_set("display_errors", "0");
error_reporting(E_ALL);
$requestData= $_REQUEST;
$columns = array( 
	0 => 'shipment_no',
	1 => 'container_no',
	2 => 'container_type',
	3 => 'loading_date',
	4 => 'etd',
	5 => 'eta',
	7 => 'loading_state',
	
);

$data = $db->query("SELECT COUNT(shipment_arrangement_items.id) as jumlah FROM shipment_arrangement_items left join shipment_arrangements on shipment_arrangement_items.shipment_arrangement_id = shipment_arrangements.id WHERE shipment_arrangements.company_id = '" . $_SESSION['company_id'] . "'");

$count = $data[0]["jumlah"];
$totalData = $count;
$totalFiltered = $totalData;

$sql = "SELECT shipment_arrangements.id as id,shipment_arrangements.shipment_no as shipment_no, shipment_arrangements.container_no as container_no, shipment_arrangements.container_type as container_type, shipment_arrangements.loading_date as loading_date, shipment_arrangements.eta as eta, shipment_arrangements.etd as etd, shipment_arrangements.loading_address as loading_address, shipment_arrangements.loading_city as loading_city, shipment_arrangements.loading_state as loading_state, shipment_arrangements.loading_country as loading_country, shipment_arrangements.loading_zip as loading_zip, shipment_arrangement_items.so_id as so_id  FROM shipment_arrangement_items left join shipment_arrangements on shipment_arrangement_items.shipment_arrangement_id = shipment_arrangements.id WHERE shipment_arrangements.company_id = '".$_SESSION['company_id']."'";



$data = $db->query($sql);
$totalFiltered = count($data);
$sql.=" ORDER BY ". $columns[$requestData['order'][0]['column']]."   ".$requestData['order'][0]['dir']."  LIMIT ".$requestData['start']." ,".$requestData['length']."   ";
$data = $db->query($sql);
$qweqwe = array();
foreach ($data as $key => $value) {
    $nestedData = array(); 
	$eta = getSAData($value["so_id"], "eta");
		$etd = getSAData($value["so_id"], "etd");
		$release_date = getSAData($value["so_id"], "release_date");
		$loading_date = getSAData($value["so_id"], "loading_date");
		$today = date("Y-m-d");
		if($today >= $loading_date && $today < $etd && $release_date == null){
			$status =  "Container Loading";
		}elseif($today >= $etd && $today < $eta && $release_date == null){
			$status =  "ETD";
		}elseif($today >= $eta && $release_date == null){
			$status = "ETA";
		}elseif($today >= $release_date && $release_date <> null){
			$status = "Custom Clearance";
		}else{
			$status = "Warehouse Received";
		}
	$nestedData[] = $value["shipment_no"];
	$nestedData[] = $value["container_no"];
	$nestedData[] = $value["container_type"];
	$nestedData[] = date("d/m/Y", strtotime($value["loading_date"]));
	$nestedData[] = date("d/m/Y", strtotime($value["etd"]));
	$nestedData[] = date("d/m/Y", strtotime($value["eta"]));
	$nestedData[] = getSOData($value["so_id"], "marking_no");
	$nestedData[] = $value["loading_address"] . ", " . $value["loading_zip"]. " " . $value["loading_city"]. ", " . $value["loading_state"]. ", " . $value["loading_country"];
	$nestedData[] = $status;
	$nestedData[] = '<div class="btn-group">
						<button class="btn green btn-xs btn-circle btn-outline dropdown-toggle" type="button" data-toggle="dropdown" aria-expanded="false"> Actions
							<i class="fa fa-angle-down"></i>
						</button>
						<ul class="dropdown-menu" role="menu">
							<li>
								<a href="/shipment-arrangements/details/'.$value["so_id"].'">
									Details </a>
							</li> 
							<li>
								<a class="customs-examination" data-id='.$value["id"].'>
									Customs Examination </a>
							</li>                                                                
						</ul>
					</div>';
	
	$qweqwe[] = $nestedData;
}
$json_data = array(
			"draw"            => intval( $requestData['draw'] ),   // for every request/draw by clientside , they send a number as a parameter, when they recieve a response/data they first check the draw number, so we are sending same number in draw. 
			"recordsTotal"    => intval( $totalData ),  // total number of records
			"recordsFiltered" => intval( $totalFiltered ), // total number of records after searching, if there is no searching then totalFiltered = totalData
			"data"            => $qweqwe   // total data array
			);
echo json_encode($json_data);
