var payment_terms_obj = null ;

$(document).ready(function() 
{ 
	$('#formPaymentTerms').hide() ;
	$('#btnPaymentTermsClear').button().bind('click',clearPaymentTerms) ;
	$('#btnPaymentTermsUpdate').button().bind('click',updatePaymentTerms) ;
	$('#btnPaymentTermsAdd').button().bind('click',addPaymentTerms) ;
	$('#btnPaymentTermsPrint').button().bind('click',printPaymentTerms) ;
	// $('#payment-terms-tabs').tabs() ;
	$('#txtPaymentTermsDesc').focus() ;
	$(window).resize(resizePaymentTermsGrid) ;
	resizePaymentTermsGrid() ;
	clearPaymentTerms();
	filterPaymentTerms();
}) ;

function showFormPaymentTerms(){
	clearPaymentTerms();
	$('#formPaymentTerms').show('slow') ;
}

function closeFormPaymentTerms(){
	clearPaymentTerms() ;
	$('#formPaymentTerms').hide('slow') ;
}	

function resizePaymentTermsGrid() {
	var h = $("#sbg-center-panel").outerHeight() - $("#sbg-payment-terms-entry").outerHeight() - 55;
	$("div#sbg-payment-terms-data").css("height", h +'px') ;		
}

function clearPaymentTerms() {
	$('#txtPaymentTermsId').val("Auto") ;
	$('#txtPaymentTermsCode').val("") ;
	$('#txtPaymentTermsDesc').val("") ;
	$('#txtPaymentTermsDefault').attr("checked", false) ;
	// $('#payment-terms-tabs').tabs("option", "active", 0) ;
	$('#txtPaymentTermsDesc').focus() ;
	$('#btnPaymentTermsUpdate').prop('disabled','disabled') ;
	$('#payment_terms_err_mesg').text('') ;
	$('#payment_terms_err_desc').hide() ;
	$('#payment_terms_err_code').hide() ;
	if (payment_terms_obj != null && payment_terms_obj != undefined)
	{
		$($(payment_terms_obj).closest("tr")).removeClass("sbg-row-selected") ;
	}
	payment_terms_obj = null;
}

function filterPaymentTerms() {
	var data = { "type": C_LIST } ;
	var url = "request.pzx?c=" + payment_terms_url + "&d=" + new Date().getTime() ;		
	callServer(url,"json",data,showPaymentTermsFilterList,payment_terms_obj) ;
}

function showPaymentTermsFilterList (obj,resp) {
	dataJSONArray = resp;
	$('.sbg-payment-terms-table').KTDatatable('destroy');
	init_dt();
}

function init_dt() {
	var datatable = $('.sbg-payment-terms-table').KTDatatable({
		// datasource definition
		data: {
			type: 'local',
			source: dataJSONArray,
			pageSize: 5, // display 20 records per page
			serverPaging: true,
			serverFiltering: true,
			serverSorting: true,
		},

		// layout definition
		layout: {
			scroll: true, // enable/disable datatable scroll both horizontal and vertical when needed.
			height: 'auto', // datatable's body's fixed height
			footer: false, // display/hide footer
		},

		// column sorting
		sortable: true,

		// toolbar
		toolbar: {
			// toolbar placement can be at top or bottom or both top and bottom repeated
			placement: ['bottom'],

			// toolbar items
			items: {
				// pagination
				pagination: {
					// page size select
					pageSizeSelect: [5, 10, 20, 30, 50], // display dropdown to select pagination size. -1 is used for "ALl" option
				},
			},
		},

		search: {
			input: $('#generalSearch'),
		},

		columns: [
			{
				field: "id",
				title: "ID",
				width: 30,
			}, {
				field: "code",
				title: "Code",
				width: 70,
			}, {
				field: "desc",
				title: "Description",
				width: 200,
			}, {
				field: "default",
				title: "Default",
				width: 100,
			}, {
				field: 'actions',
				title: 'Actions',
				sortable: true,
				width: 470,
				overflow: 'visible',
		}],
	});
}

function updatePaymentTerms() {
	savePaymentTerms(C_UPDATE) ;
}
function addPaymentTerms() {
	savePaymentTerms(C_ADD) ;
}
function savePaymentTerms(type) {
	if (!validatePaymentTerms()) {
		return false;
	}
	var data = { "type": type, 
                    "id": $('#txtPaymentTermsId').val(),
                    "code": $('#txtPaymentTermsCode').val(),
                    "desc": $('#txtPaymentTermsDesc').val(), 
                    "default": $('#txtPaymentTermsDefault').is(':checked') ? 1 : 0,
				};
	var url = "request.pzx?c=" + payment_terms_url + "&d=" + new Date().getTime() ;
	callServer(url,"json",data,onPaymentTermsResponse,payment_terms_obj) ;
}

function editPaymentTerms(id,obj) {
	var data = { "type": C_GET,"id": id} ;
	var url = "request.pzx?c=" + payment_terms_url + "&d=" + new Date().getTime();		
	clearPaymentTerms();
	showFormPaymentTerms();
	payment_terms_obj = obj ;
	$($(obj).closest("tr")).addClass("sbg-row-selected");
	callServer(url,"json",data,showPaymentTerms,obj) ;
}

function deletePaymentTerms(id,obj) {
	// if (confirm("Confirm you want to delete PaymentTerms id : " + id + "?") == false) {
	// 	return false;
	// }
	
	// var data = {"type": C_DELETE,"id": id} ;
	// var url = "request.pzx?c=" + payment_terms_url + "&d=" + new Date().getTime() ;
	// callServer(url,"json",data,onPaymentTermsResponse,obj) ;

	//============================

	swal("Confirm you want to delete PaymentTerms id : " + id + "?", {
		buttons: {
			ok: true,
		  cancel: "Cancel",
		},
	  })
	  .then((value) => {
		switch (value) {
		  case "ok":
			  var data = {"type": C_DELETE,"id": id} ;
			  var url = "request.pzx?c=" + payment_terms_url + "&d=" + new Date().getTime() ;
			  callServer(url,"json",data,onPaymentTermsResponse,obj) ;
		  break;
		}
	  });
}
function showPaymentTerms(obj,resp) {
	if (resp.status != C_OK) {
		swal("System Message",resp.mesg) ;
		return false;
	}
	
	$('#txtPaymentTermsId').val(resp.data.id) ;
	$('#txtPaymentTermsCode').val(resp.data.code) ;
	$('#txtPaymentTermsDesc').val(resp.data.desc) ;
	$('#txtPaymentTermsDefault').attr("checked", resp.data.default) ;
	$('#btnPaymentTermsUpdate').prop('disabled','') ;
	$('#txtPaymentTermsDesc').focus() ;
}
function printPaymentTerms() {
	var url = "report.pzx?c=" + payment_terms_url + "&d=" + new Date().getTime();
	showReport(url) ;
}
function onPaymentTermsResponse(obj,resp) {
	if (resp.status != C_OK) {
		swal("Error",resp.mesg) ;
		return;
	}
	if (resp.type == C_ADD) {
		swal("Successfull!", "Your Item Successfully add to the system", "success");
	} else if (resp.type == C_DELETE) {
		swal("Successfull!", "Your Item Successfully delete to the system", "success");
	} else if (resp.type == C_UPDATE) {
		swal("Successfull!", "Your Item Successfully update to the system", "success");
	}
	$('#sbg-popup-box').html(resp.mesg) ;
	$('#sbg-popup-box').show() ;
	$('#sbg-popup-box').fadeOut(5000) ;
	clearPaymentTerms() ;
	filterPaymentTerms();
	closeFormPaymentTerms();
}
function validatePaymentTerms() {
	$('#payment_terms_err_mesg').text('') ;
	if ($('#txtPaymentTermsDesc').val()=="")
	{
		$('#payment_terms_err_desc').show() ;
		$('#txtPaymentTermsDesc').focus() ;
		swal("PaymentTerms description can not be blank.") ;
		return false ;
	}
	else 
		$('#payment_terms_err_desc').hide() ;
    
	if ($('#txtPaymentTermsCode').val()=="")
	{
		$('#payment_terms_err_code').show() ;
		$('#txtPaymentTermsCode').focus() ;
		swal("PaymentTerms code can not be blank.") ;
		return false ;
	}
	else 
		$('#payment_terms_err_code').hide() ;
			
	return true ;
}