<?php
global $hooks;
	
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
$hooks->add_action('silex_action','the_shipment_arrangement'); 
function create_shipment_arrangement_title(){
    echo "Shipment Arrangement";
}

function create_shipment_arrangement_css() {
    ?>
	<link href="/assets/global/plugins/bootstrap-daterangepicker/daterangepicker.min.css" rel="stylesheet" type="text/css" />
	<link href="/assets/global/plugins/bootstrap-datepicker/css/bootstrap-datepicker3.min.css" rel="stylesheet" type="text/css" />
    <link href="/assets/global/plugins/select2/css/select2.min.css" rel="stylesheet" type="text/css" />
	<link href="/assets/global/plugins/select2/css/select2-bootstrap.min.css" rel="stylesheet" type="text/css" />
    <?php
}

function create_shipment_arrangement_js() {
    ?>
	<script src="/assets/global/plugins/select2/js/select2.full.min.js" type="text/javascript"></script>
	<script src="/assets/global/plugins/jquery-validation/js/jquery.validate.min.js" type="text/javascript"></script>
	<script src="/assets/global/plugins/jquery-validation/js/additional-methods.min.js" type="text/javascript"></script>
	<script src="/assets/global/plugins/bootstrap-wizard/jquery.bootstrap.wizard.min.js" type="text/javascript"></script>
	<script src="/assets/global/plugins/jquery-repeater/jquery.repeater.js" type="text/javascript"></script>
	<script src="/assets/global/scripts/datatable.js" type="text/javascript"></script>
	<script src="/assets/global/plugins/datatables/datatables.min.js" type="text/javascript"></script>
	<script src="/assets/global/plugins/datatables/plugins/bootstrap/datatables.bootstrap.js" type="text/javascript"></script>
	<script src="/assets/global/plugins/bootstrap-daterangepicker/daterangepicker.min.js" type="text/javascript"></script>
	<script src="/assets/global/plugins/bootstrap-datepicker/js/bootstrap-datepicker.min.js" type="text/javascript"></script>
	<script src="/assets/global/plugins/bootstrap-timepicker/js/bootstrap-timepicker.min.js" type="text/javascript"></script>
	<script src="/assets/global/plugins/bootstrap-datetimepicker/js/bootstrap-datetimepicker.min.js" type="text/javascript"></script>
	<script src="/assets/global/plugins/clockface/js/clockface.js" type="text/javascript"></script>
	<script src="/assets/modul-js/arrangements/shipment-arrangement/create.min.js" type="text/javascript"></script>
 <?php
}


function the_shipment_arrangement(){
	
	global $app;

		$app->get('/shipment-arrangements', function(){  
			global $hooks;
			global $app;
		
			
			$hooks->add_action('global_css',"create_shipment_arrangement_css");
			$hooks->add_action('global_js',"create_shipment_arrangement_js");
			$hooks->add_action('the_title',"create_shipment_arrangement_title");
		
			the_head();
			include 'shipment.tpl.php';
			the_footer();
		
		
			return "";
		}); 
		
		
		
		$app->post('/shipment-arrangements/list', function(Request $request)  {
			$curpage = $request->get('page');
			include 'shipment.list.php';
			return '';
		}); 
		
		$app->post('/shipment-arrangements/getOrderSelection', function(Request $request) {		
			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)");
			$table = "";
			
			foreach ($orderselections as $key => $orderselection){
				$table .= "<tr>"
							."<td>".  date('d/m/Y', strtotime($orderselection['created_on'])). "</td>"
							."<td>". $orderselection['marking_no']. "</td>"
							."<td>". getWATotalCBM(getWAID($orderselection['so_id'])). "</td>"
							."<td>". getWATotalWeight(getWAID($orderselection['so_id'])). "</td>"
							."<td><div style='padding-top: 0px;padding-bottom: 0px;' class='mt-checkbox-list'><label class='mt-checkbox mt-checkbox-outline' style='margin-bottom: 0px;'><input style='margin-top: 0px;' type='checkbox' id='so_ids' name='so_ids[]' value=".$orderselection['so_id']."><span></span></label></div></td>"
						 ."</tr>";
				
			}
			return $table;
		}); 
	
		$app->post('/shipment-arrangements/edit-customs-examination', function(Request $request) {		
			global $db;
			$id = $request->get('id');
			
			$html = getModalSACustomsExamination($id);
			
			return $html;
		
		}); 
		
		$app->post('/shipment-arrangements/add-shipment-arrangements', function (Request $request) {
			global $db;
			
			$user_id = $_SESSION['user_id'];
			$company_id = $_SESSION['company_id'];
			$loading_date = $request->get('loading_date');
			$shipment_no = $request->get('shipment_no');
			$arrival_port = $request->get('arrival_port');
			$departure_port = $request->get('departure_port');
			$container_no = $request->get('container_no');
			$container_type = $request->get('container_type');
			$eta = $request->get('eta');
			$etd = $request->get('etd');
			$driver_contact_person = $request->get('driver_contact_person');
			$driver_contact_person_no = $request->get('driver_contact_person_no');
			$trailer_no = $request->get('trailer_no');
			$driver_ic_no = $request->get('driver_ic_no');
			$loading_address = $request->get('loading_address');
			$loading_city = $request->get('loading_city');
			$loading_state = $request->get('loading_state');
			$loading_contact_person = $request->get('loading_contact_person');
			$loading_zip = $request->get('loading_zip');
			$loading_country = $request->get('loading_country');
			$loading_contact_person_no = $request->get('loading_contact_person_no');
			$remark = $request->get('remark');
			$so_ids = $request->get('so_ids');
		
			$db->bind("user_id", $user_id);
			$db->bind("company_id", $company_id);
			$db->bind("loading_date", $loading_date);
			$db->bind("shipment_no", $shipment_no);
			$db->bind("arrival_port", $arrival_port);
			$db->bind("departure_port", $departure_port);
			$db->bind("container_no", $container_no);
			$db->bind("container_type", $container_type);
			$db->bind("eta", $eta);
			$db->bind("etd", $etd);
			$db->bind("driver_contact_person", $driver_contact_person);
			$db->bind("driver_contact_person_no", $driver_contact_person_no);
			$db->bind("trailer_no", $trailer_no);
			$db->bind("driver_ic_no", $driver_ic_no);
			$db->bind("loading_address", $loading_address);
			$db->bind("loading_city", $loading_city);
			$db->bind("loading_state", $loading_state);
			$db->bind("loading_contact_person", $loading_contact_person);
			$db->bind("loading_zip", $loading_zip);
			$db->bind("loading_country", $loading_country);
			$db->bind("loading_contact_person_no", $loading_contact_person_no);
			$db->bind("remark", $remark);
			
			$shipmentarrangementsCreate = $db->query("INSERT INTO shipment_arrangements (user_id, company_id, loading_date, shipment_no, departure_port, arrival_port, container_no, container_type, eta, etd, driver_contact_person, driver_contact_person_no, trailer_no, driver_ic_no, loading_address, loading_city, loading_state, loading_contact_person, loading_zip, loading_country, loading_contact_person_no, remark) VALUES (:user_id, :company_id, :loading_date, :shipment_no, :departure_port, :arrival_port, :container_no, :container_type, :eta, :etd, :driver_contact_person, :driver_contact_person_no, :trailer_no, :driver_ic_no, :loading_address, :loading_city, :loading_state, :loading_contact_person, :loading_zip, :loading_country, :loading_contact_person_no, :remark);");
			$shipment_arrangement_id = $db->lastInsertId();
			
			if($shipmentarrangementsCreate){
				foreach ($so_ids as $key => $so_id) {
					$db->bind("so_id", $so_id);
					$db->bind("shipment_arrangement_id", $shipment_arrangement_id);
					
					$shipment_arrangement_itemsCreate = $db->query("INSERT INTO shipment_arrangement_items (so_id, shipment_arrangement_id) VALUES (:so_id, :shipment_arrangement_id);");
					if($shipment_arrangement_itemsCreate){
						$db->bind("status", "2");
						$db->bind("so_id", $so_id);
						$soUpdate = $db->query("UPDATE `shipping_orders` SET `status`=:status WHERE so_id = :so_id");
					}
					
					//create messages
					$db->bind("message", "Your Shipment Arrangement have been created.");
					$db->bind("chat_type", "8");
					$db->bind("from_id", $user_id);
					$createMessage  = $db->query("INSERT INTO notice_messages(message,chat_type,from_id) VALUES (:message,:chat_type,:from_id)");
					$message_id = $db->lastInsertId();
					
					//find room for current so
					$room_id = getRoomIDFromSO($so_id);
					
					//connecting messages with room
					$db->bind("room_id", $room_id);
					$db->bind("message_id", $message_id);
					$createMembers  = $db->query("INSERT INTO notice_room_message(room_id,message_id) VALUES (:room_id, :message_id)");
					
					$insideRoom = getUserInRoom($room_id);
					foreach($insideRoom as $key => $value){
						$db->bind("room_id", $room_id);
						$db->bind("message_id", $message_id);
						$db->bind("user_id", $value['user_id']);
						$createMembers  = $db->query("INSERT INTO notice_unread(room_id,message_id,user_id, status) VALUES (:room_id, :message_id,:user_id, 0)");
					}
				}
			}
			
			
			if ($shipment_arrangement_itemsCreate) {
				return new Response("SUCCESS", 200);
			} else {			
				return new Response('FAILED', 201);
			} 
		}); 
		
		$app->post('/shipment-arrangements/update-shipment-arrangements', function (Request $request) {
			global $db;
			$id = $request->get('id');
			
			$db->bind("id", $id);
			$shipment_arrangements = $db->query("SELECT * FROM shipment_arrangements WHERE id = :id");
			$last_eta = $shipment_arrangements[0]["eta"];
			$last_etd = $shipment_arrangements[0]["etd"];
				
			
			$eta = $request->get('eta');
			$etd = $request->get('etd');
			$release_date = $request->get('release_date');
				
			
			$db->bind("id", $id);
			$db->bind("eta", $eta);
			$db->bind("etd", $etd);
			$db->bind("last_eta", $last_eta);
			$db->bind("last_etd", $last_etd);
			$db->bind("release_date", $release_date);
			$shipmentarrangementsUpdate = $db->query("UPDATE `shipment_arrangements` SET `etd`=:etd,`eta`=:eta,`release_date`=:release_date,`last_eta`=:last_eta,`last_etd`=:last_etd where`id`=:id");
			
			
			if ($shipmentarrangementsUpdate) {
				$db->bind("id", $id);
				$shipment_arrangements = $db->query("SELECT * FROM shipment_arrangements WHERE id = :id");
				$release_date = $shipment_arrangements[0]["release_date"];
				
				if($release_date <> null){
					$db->bind("id", $id);	
					$db->bind("status", 3);	 
					$shipmentarrangementsUpdate = $db->query("UPDATE `shipment_arrangements` SET `etd`=:etd,`eta`=:eta,`release_date`=:release_date where`id`=:id");
				}
				return new Response("SUCCESS", 200);
			} else {			
				return new Response('FAILED', 201);
			} 
		}); 
		$app->get('/shipment-arrangements/details/{id}', function ($id) use ($app) {
			global $db;
			global $hooks;
			global $so_id;
			
			$so_id = $id;
			
			if (!isset($id) || !isSOMine($id)) {
				return $app->redirect('/shipment-arrangements');
			}
			
			$hooks->add_action('global_css', "create_shipment_arrangement_css");
			$hooks->add_action('global_js', "create_shipment_arrangement_js");
			$hooks->add_action('the_title',"create_shipment_arrangement_title");
			
			the_head();
			include 'shipment.details.php';
			the_footer();
			return "";
		
		}); 
	}
	
include 'shipment.func.php';