var location_obj = null ;

$(document).ready(function() 
{ 
	$('#formLocation').hide() ;
	$('#btnLocationClear').button().bind('click',clearLocation) ;
	$('#btnLocationUpdate').button().bind('click',updateLocation) ;
	$('#btnLocationAdd').button().bind('click',addLocation) ;
	$('#btnLocationPrint').button().bind('click',printLocation) ;
	// $('#location-tabs').tabs() ;
	$('#txtLocationDesc').focus() ;
	$(window).resize(resizeLocationGrid) ;
	resizeLocationGrid() ;
	clearLocation();
	filterLocation();
}) ;

function showFormLocation(){
	clearLocation();
	$('#formLocation').show('slow') ;
}

function closeFormLocation(){
	clearLocation() ;
	$('#formLocation').hide('slow') ;
}	

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

function filterLocation() {
	var data = { "type": C_LIST} ;
	var url = "request.pzx?c=" + location_url + "&d=" + new Date().getTime() ;		
	callServer(url,"json",data,showLocationFilterList,location_obj) ;
}

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

function init_dt() {
	var datatable = $('.sbg-location-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: "cust",
				title: "Customer",
				width:200,
			}, {
				field: 'actions',
				title: 'Actions',
				sortable: true,
				width: 260,
				overflow: 'visible',
		}],
	});
}

function clearLocation() {
	$('#txtLocationId').val("Auto") ;
	$('#txtLocationDesc').val("") ;
	$('#txtLocationCode').val("") ;
	$('#txtlocationCust').val(0) ;
	// $('#location-tabs').tabs("option", "active", 0) ;
	$('#txtLocationDesc').focus() ;
	$('#btnLocationUpdate').prop('disabled','disabled') ;
	$('#location_err_mesg').text('') ;
	$('#location_err_desc').hide() ;
	$('#location_err_code').hide() ;
	if (location_obj != null && location_obj != undefined)
	{
		$($(location_obj).closest("tr")).removeClass("sbg-row-selected") ;
	}
	location_obj = null;
}
function updateLocation() {
	saveLocation(C_UPDATE) ;
}
function addLocation() {
	saveLocation(C_ADD) ;
}
function saveLocation(type) {
	if (!validateLocation()) {
		return false;
	}
	var data = { "type": type, 
                    "id": $('#txtLocationId').val(), 
                    "desc": $('#txtLocationDesc').val(), 
                    "code": $('#txtLocationCode').val(),
                    "customer": $('#txtlocationCust').val()};
	var url = "request.pzx?c=" + location_url + "&d=" + new Date().getTime() ;
	callServer(url,"json",data,onLocationResponse,location_obj) ;
}

function editLocation(id,obj) {
	var data = { "type": C_GET,"id": id} ;
	var url = "request.pzx?c=" + location_url + "&d=" + new Date().getTime();		
	clearLocation();
	showFormLocation();
	location_obj = obj ;
	$($(obj).closest("tr")).addClass("sbg-row-selected");
	callServer(url,"json",data,showLocation,obj) ;
}

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

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

	swal("Confirm you want to delete Location 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=" + location_url + "&d=" + new Date().getTime() ;
			  callServer(url,"json",data,onLocationResponse,obj) ;
		  break;
		}
	  });
}
function showLocation(obj,resp) {
	if (resp.status != C_OK) {
		swal("System Message",resp.mesg) ;
		return false;
	}
	
	$('#txtLocationId').val(resp.data.id) ;
	$('#txtLocationDesc').val(resp.data.desc) ;
	$('#txtLocationCode').val(resp.data.code) ;
	$('#txtlocationCust').val(resp.data.customer) ;
	$('#btnLocationUpdate').prop('disabled','') ;
	$('#txtLocationDesc').focus() ;
}
function printLocation() {
	var url = "report.pzx?c=" + location_url + "&d=" + new Date().getTime();
	showReport(url) ;
}
function onLocationResponse(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) ;
	clearLocation() ;
	filterLocation();
	closeFormLocation();
}
function validateLocation() {
	$('#location_err_mesg').text('') ;
	if ($('#txtLocationDesc').val()=="")
	{
		$('#location_err_desc').show() ;
		$('#txtLocationDesc').focus() ;
		swal("Location description can not be blank.") ;
		return false ;
	}
	else 
		$('#location_err_desc').hide() ;
    
	if ($('#txtLocationCode').val()=="")
	{
		$('#location_err_code').show() ;
		$('#txtLocationCode').focus() ;
		swal("Location code can not be blank.") ;
		return false ;
	}
	else 
		$('#location_err_code').hide() ;
			
	return true ;
}