// JavaScript Document
var web_doc_alias = '';
$(document).ready(function(){

	web_doc_alias = $('#web_doc_alias').val();
	showTable();

	$('#sarOrderRireki').click(function(){
		searchOrderList();
	});

	$('#btnBack').on('click',function(){
		$('#prgname').val('A020');
		$('#param1').val('0');
		$('#menuForm').submit();
	});

	$(document).on( 'click', '#table1 a', function () {
        var btn_name = $(this).attr('btn-name');
        var target_no = $(this).attr('target-no');
		if (btn_name){
			if (btn_name == "shipname") {
				$('#prgname2').val("A133");
				$('#param2_1').val("A140");
				$('#param2_2').val(target_no);
				$('#param2_3').val("0");
				$('#menuOpen').submit();
			} else if (btn_name == "invoiceno") {
				$(this).removeClass('text-primary');
				$(this).addClass('text-dark');
				$('#prgname2').val("A145");
				$('#param2_1').val(target_no);
				$('#menuOpen').submit();				
				//alert(target_no);
			} else if (btn_name == "copy1") {
				$('#prgname').val("A121");
				$('#param1').val("A140");
				$('#param2').val("1");
				$('#param3').val(target_no);
				$('#menuForm').submit();
				//alert("接岸依頼" + target_no);
			} else if (btn_name == "copy2") {
				$('#prgname').val("A127");
				$('#param1').val("A140");
				$('#param2').val("1");
				$('#param3').val(target_no);
				$('#menuForm').submit();
				//alert("新規依頼" + target_no);
			} else {
			} 
		}
    } );
});

function showTable(){
	// datatableの設定を変更
	varTable1 =	$("#table1").DataTable({
		dom:"<'row'<'col-sm-6'i><'col-sm-6 right mb-1'f>>" +
		"<'row'<'col-sm-12'tr>>" +
		"<'row'<'col-sm-6 mt-1'l><'col-sm-6 right mt-1'p>>",
		'paging'      :true,
		'pageLength'  :10,
		'lengthChange':true,
		'searching'   :true,
		'ordering'    :true,
		'info'        :true,
		'autoWidth'   :false,
		'scrollX'     :true,
		'scrollY'     :true,
		'order'       :[[0,'asc']],
		'columns'     :[
		{data:"datetime"},
		{data:"basyo"},
		{data:"agency"},
		{data:"shipname",width: "20%"},
		{data:"tanto"},
		{data:"invoiceto"},
		{data:"invoiceno"},
		{data:"copy"}
		],
		"language": {
			"url": "../webnais/lib/DataTables/plug-ins/Japanese.json"
		}
	});
};

function searchOrderList(){
	var param = {
		'appname': nasiappname,
		'prgname':'A141',
		'param1': nasiappname,
		'param2': $('#user').val(),
		'param3' : $('#inpMonthFrom').val(),
		'param4' : $('#inpMonthTo').val(),
		'param5' : $('#inpVesselName').val(),
		'param6' : $('#inpInvoiceNo').val(),
		'arguments':'param1,param2,param3,param4,param5,param6'
	};
	var retXml = ajaxFunction(param);
	var kekka = $(retXml).find('ret').text();
	if(kekka === 'I001'){
		varTable1.state.clear();
		varTable1.destroy();
		$("#table1 tbody > tr").remove();
		showTable();
		$(retXml).find('lines').each(function(){
			var status = $(this).find('status').text();
			var kyodo_date = $(this).find('kyodo_date').text();
			var kyodo_time = $(this).find('kyodo_time').text();
			var k_order_no = $(this).find('k_order_no').text();
			var josen = $(this).find('josen').text();
			var gesen = $(this).find('gesen').text();
			var operator = $(this).find('operator').text();
			var agency = $(this).find('agency').text();
			var ship_name = $(this).find('ship_name').text();
			var tanto = $(this).find('tanto').text();
			var order_no = $(this).find('order_no').text();
			var invoice_no1 = $(this).find('invoice_no1').text();
			var invoice_no2 = $(this).find('invoice_no2').text();
			var billing_to1 = $(this).find('billing_to1').text();
			var billing_to2 = $(this).find('billing_to2').text();
			var invoice_color1 = $(this).find('invoice_color1').text();
			var invoice_color2 = $(this).find('invoice_color2').text();
			var copy1_link ="";
			if (k_order_no !=""){
				copy1_link ='<a href="#" btn-name="copy1" target-no="' + k_order_no +'">接岸依頼作成</a>' + '<br>'
				+ '<a href="#" btn-name="copy2" target-no="' + k_order_no +'">新規依頼作成</a>'
			}
			varTable1.row.add({
				datetime:kyodo_date + '<br>' + kyodo_time
				,basyo:josen + '<br>' + gesen
				,agency:operator + '<br>' + agency
				,shipname:
				'<a href="#" btn-name="shipname" target-no="' + k_order_no +'">' + ship_name + '</a>'
				,tanto:tanto
				,invoiceto:billing_to1 + '<br>' + billing_to2
				,invoiceno:
				'<a href="#" class="'+ invoice_color1 +'" btn-name="invoiceno" target-no="' + invoice_no1 +'">' + invoice_no1 + '</a>' + '<br>'
				+ '<a href="#" class="'+ invoice_color2 +'" btn-name="invoiceno" target-no="' + invoice_no2 +'">' + invoice_no2 + '</a>'
				,copy:copy1_link
				});
		});
		varTable1.draw(false);
	}
}
