var footerinfo_obj = null ;

$(document).ready(function() 
{ 
	$('#formFooterInfo').hide() ;
	$('#btnFooterInfoClear').button().bind('click',clearFooterInfo) ;
	$('#btnFooterInfoUpdate').button().bind('click',updateFooterInfo) ;
	$('#btnFooterInfoAdd').button().bind('click',addFooterInfo) ;
	$('#btnFooterInfoPrint').button().bind('click',printFooterInfo) ;
	// $('#footerinfo-tabs').tabs() ;
	$('#txtFooterInfoDesc').focus() ;
	$(window).resize(resizeFooterInfoGrid) ;
	resizeFooterInfoGrid() ;
	clearFooterInfo();
	filterFooterInfo();
}) ;

function showFormFooterInfo(){
	clearFooterInfo();
	$('#formFooterInfo').show('slow') ;
}

function closeFormFooterInfo(){
	clearFooterInfo() ;
	$('#formFooterInfo').hide('slow') ;
}	

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

function clearFooterInfo() {
	$('#txtFooterInfoId').val("Auto") ;
	$('#txtFooterInfoDesc').val("") ;
	$('#txtFooterInfoCode').val("") ;
	// $('#footerinfo-tabs').tabs("option", "active", 0) ;
	$('#txtFooterInfoDesc').focus() ;
	$('#btnFooterInfoUpdate').prop('disabled','disabled') ;
	$('#footerinfo_err_mesg').text('') ;
	$('#footerinfo_err_desc').hide() ;
	$('#footerinfo_err_code').hide() ;
	if (footerinfo_obj != null && footerinfo_obj != undefined)
	{
		$($(footerinfo_obj).closest("tr")).removeClass("sbg-row-selected") ;
	}
	footerinfo_obj = null;
}

function filterFooterInfo() {
	var data = { "type": C_LIST} ;
	var url = "request.pzx?c=" + footerinfo_url + "&d=" + new Date().getTime() ;		
	callServer(url,"json",data,showFooterInfoFilterList,footerinfo_obj) ;
}

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

function init_dt() {
	var datatable = $('.sbg-footerinfo-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: "Footer Information",
				width:500,
			}, {
				field: 'actions',
				title: 'Actions',
				sortable: true,
				width: 400,
				overflow: 'visible',
		}],
	});
}

function updateFooterInfo() {
	saveFooterInfo(C_UPDATE) ;
}
function addFooterInfo() {
	saveFooterInfo(C_ADD) ;
}
function saveFooterInfo(type) {
	if (!validateFooterInfo()) {
		return false;
	}
	var data = { "type": type, 
                    "id": $('#txtFooterInfoId').val(), 
                    "desc": $('#txtFooterInfoDesc').val(), 
                    "code": $('#txtFooterInfoCode').val()};
	var url = "request.pzx?c=" + footerinfo_url + "&d=" + new Date().getTime() ;
	callServer(url,"json",data,onFooterInfoResponse,footerinfo_obj) ;
}

function editFooterInfo(id,obj) {
	var data = { "type": C_GET,"id": id} ;
	var url = "request.pzx?c=" + footerinfo_url + "&d=" + new Date().getTime();		
	clearFooterInfo();
	showFormFooterInfo();
	footerinfo_obj = obj ;
	$($(obj).closest("tr")).addClass("sbg-row-selected");
	callServer(url,"json",data,showFooterInfo,obj) ;
}

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

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

	swal("Confirm you want to delete FooterInfo 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=" + footerinfo_url + "&d=" + new Date().getTime() ;
			  callServer(url,"json",data,onFooterInfoResponse,obj) ;
		  break;
		}
	  });
}
function showFooterInfo(obj,resp) {
	if (resp.status != C_OK) {
		swal("System Message",resp.mesg) ;
		return false;
	}
	
	$('#txtFooterInfoId').val(resp.data.id) ;
	$('#txtFooterInfoDesc').val(resp.data.desc) ;
	$('#txtFooterInfoCode').val(resp.data.code) ;
	$('#btnFooterInfoUpdate').prop('disabled','') ;
	$('#txtFooterInfoDesc').focus() ;
}
function printFooterInfo() {
	var url = "report.pzx?c=" + footerinfo_url + "&d=" + new Date().getTime();
	showReport(url) ;
}
function onFooterInfoResponse(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) ;
	clearFooterInfo() ;
	filterFooterInfo();
	closeFormFooterInfo();
}
function validateFooterInfo() {
	$('#footerinfo_err_mesg').text('') ;
	if ($('#txtFooterInfoDesc').val()=="")
	{
		$('#footerinfo_err_desc').show() ;
		$('#txtFooterInfoDesc').focus() ;
		swal("FooterInfo description can not be blank.") ;
		return false ;
	}
	else 
		$('#footerinfo_err_desc').hide() ;
    
	if ($('#txtFooterInfoCode').val()=="")
	{
		$('#footerinfo_err_code').show() ;
		$('#txtFooterInfoCode').focus() ;
		swal("FooterInfo code can not be blank.") ;
		return false ;
	}
	else 
		$('#footerinfo_err_code').hide() ;
			
	return true ;
}