<?php
global $hooks;
	
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
$hooks->add_action('silex_action','the_custom_declaration'); 
function create_custom_declaration_title(){
    echo "Custom Declaration";
}

function create_custom_declaration_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_custom_declaration_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/declaration/create.min.js" type="text/javascript"></script>
 <?php
}

function the_custom_declaration(){
	global $app;
		
	$app->get('/custom-declarations', function(){
		
		global $hooks;
		global $app;
		
		$hooks->add_action('global_css',"create_custom_declaration_css");
		$hooks->add_action('global_js',"create_custom_declaration_js");
		$hooks->add_action('the_title',"create_custom_declaration_title");
	
		the_head();
		include 'declaration.tpl.php';
		the_footer();
	
	
		return "";
	}); 
	$app->post('/custom-declarations/list', function(Request $request)  {
		$curpage = $request->get('page');
        include 'declaration.list.php';
        return '';
    });
	$app->post('/custom-declarations/getSOitems', function(Request $request) {
		global $db;
		
		$db->bind("so_id", $request->get('so_id'));
		$orderselections = $db->query("SELECT * FROM shipping_order_items where so_id = :so_id");
		$table = "";
		$count = 1;
		foreach ($orderselections as $key => $orderselection){
			$table .= "<tr>"
						."<td>".$count."</td>"
						."<td><input name='custom_declaration_items[".$count."][hs_code]' id='custom_declaration_items[".$count."][hs_code]'/></td>"
						."<td>".$orderselection['product_name']."</td>"
						."<td>".$orderselection['description']."</td>"
						."<td>".$orderselection['packaging_quantity']."</td>"
						."<td>".$orderselection['packaging_type']."</td>"
						."<td>".getPOItem($orderselection['po_item_id'],"subtotal")."<input style='display:none;' name='custom_declaration_items[".$count."][so_item_id]' id='custom_declaration_items[".$count."][so_item_id]' value='".$orderselection['id']."'/> </td>"
					."</tr>";
			$count++;
		}
		return $table;
	});
	
	$app->post('/custom-declarations/add-custom-declaration', function (Request $request) {
		global $db;
		
		$user_id = $_SESSION['user_id'];
		$company_id = $_SESSION['company_id'];
		$so_id = $request->get('so_id');
		$ref_no = $request->get('ref_no');
		$exporter_name = $request->get('exporter_name');
		$custom_declaration_items = $request->get('custom_declaration_items');
		
		$db->bind("user_id", $user_id);
		$db->bind("company_id", $company_id);
		$db->bind("so_id", $so_id);
		$db->bind("ref_no", $ref_no);
		$db->bind("exporter_name", $exporter_name);
		
		$customdeclarationCreate = $db->query("INSERT INTO `custom_declarations`(`ref_no`, `exporter_name`, `so_id`, `user_id`, `company_id`) VALUES (:ref_no,:exporter_name,:so_id,:user_id,:company_id)");
		
		$custom_declaration_id = $db->lastInsertId();
		
		if($customdeclarationCreate){
			foreach ($custom_declaration_items as $key => $custom_declaration_item) {
				$db->bind("hs_code", $custom_declaration_item['hs_code']);
				$db->bind("so_item_id", $custom_declaration_item['so_item_id']);
				$db->bind("custom_declaration_id", $custom_declaration_id);
				
				$custom_declaration_itemsCreate = $db->query("INSERT INTO custom_declaration_items (hs_code, so_item_id, custom_declaration_id) VALUES (:hs_code, :so_item_id, :custom_declaration_id);");
			}
		}
		if($custom_declaration_itemsCreate){
			return new Response("SUCCESS", 200);
		}else {			
			return new Response('FAILED', 201);
		} 
		
	});
}
	
include 'declaration.func.php';