var activity_status_obj = null ;

$(document).ready(function() 
{ 
	$('#formActivityStatus').hide() ;
	$('#btnActivityStatusClear').button().bind('click',clearActivityStatus) ;
	$('#btnActivityStatusUpdate').button().bind('click',updateActivityStatus) ;
	$('#btnActivityStatusAdd').button().bind('click',addActivityStatus) ;
	$('#btnActivityStatusPrint').button().bind('click',printActivityStatus) ;
	// $('#activity-status-tabs').tabs() ;
	$('#txtActivityStatusDesc').focus() ;
	$(window).resize(resizeActivityStatusGrid) ;
	resizeActivityStatusGrid() ;
	clearActivityStatus();
	filterActivityStatus();
}) ;

function showFormActivityStatus(){
	clearActivityStatus();
	$('#formActivityStatus').show('slow') ;
}

function closeFormActivityStatus(){
	clearActivityStatus() ;
	$('#formActivityStatus').hide('slow') ;
}	

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

function filterActivityStatus() {
	var data = { "type": C_LIST} ;
	var url = "request.pzx?c=" + activity_status_url + "&d=" + new Date().getTime() ;		
	callServer(url,"json",data,showActivityStatusFilterList,activity_status_obj) ;
}

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

function init_dt() {
	var datatable = $('.sbg-activity-status-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: 'actions',
				title: 'Actions',
				sortable: true,
				width: 470,
				overflow: 'visible',
		}],
	});
}

function clearActivityStatus() {
	$('#txtActivityStatusId').val("Auto") ;
	$('#txtActivityStatusDesc').val("") ;
	$('#txtActivityStatusCode').val("") ;
	// $('#activity-status-tabs').tabs("option", "active", 0) ;
	$('#txtActivityStatusDesc').focus() ;
	$('#btnActivityStatusUpdate').prop('disabled','disabled') ;
	$('#activity_status_err_mesg').text('') ;
	$('#activity_status_err_desc').hide() ;
	$('#activity_status_err_code').hide() ;
	if (activity_status_obj != null && activity_status_obj != undefined)
	{
		$($(activity_status_obj).closest("tr")).removeClass("sbg-row-selected") ;
	}
	activity_status_obj = null;
}

function updateActivityStatus() {
	saveActivityStatus(C_UPDATE) ;
}

function addActivityStatus() {
	saveActivityStatus(C_ADD) ;
}

function saveActivityStatus(type) {
	if (!validateActivityStatus()) {
		return false;
	}
	var data = { "type": type, 
                    "id": $('#txtActivityStatusId').val(), 
                    "desc": $('#txtActivityStatusDesc').val(), 
                    "code": $('#txtActivityStatusCode').val()};
	var url = "request.pzx?c=" + activity_status_url + "&d=" + new Date().getTime() ;
	callServer(url,"json",data,onActivityStatusResponse,activity_status_obj) ;
}

function editActivityStatus(id,obj) {
	var data = { "type": C_GET,"id": id} ;
	var url = "request.pzx?c=" + activity_status_url + "&d=" + new Date().getTime();		
	clearActivityStatus();
	showFormActivityStatus()
	activity_status_obj = obj ;
	$($(obj).closest("tr")).addClass("sbg-row-selected");
	callServer(url,"json",data,showActivityStatus,obj) ;
}

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

	//======================
	swal("Confirm you want to delete Activity Status 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=" + activity_status_url + "&d=" + new Date().getTime() ;
			  callServer(url,"json",data,onActivityStatusResponse,obj) ;
		  break;
		}
	  });
}
function showActivityStatus(obj,resp) {
	if (resp.status != C_OK) {
		swal("System Message",resp.mesg) ;
		return false;
	}
	
	$('#txtActivityStatusId').val(resp.data.id) ;
	$('#txtActivityStatusDesc').val(resp.data.desc) ;
	$('#txtActivityStatusCode').val(resp.data.code) ;
	$('#btnActivityStatusUpdate').prop('disabled','') ;
	$('#txtActivityStatusDesc').focus() ;
}

function printActivityStatus() {
	var url = "report.pzx?c=" + activity_status_url + "&d=" + new Date().getTime();
	showReport(url) ;
}

function onActivityStatusResponse(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) ;
	clearActivityStatus() ;
	filterActivityStatus();
	closeFormActivityStatus();
}

function validateActivityStatus() {
	$('#activity_status_err_mesg').text('') ;
	if ($('#txtActivityStatusDesc').val()=="")
	{
		$('#activity_status_err_desc').show() ;
		$('#txtActivityStatusDesc').focus() ;
		swal("Expense Item description can not be blank.") ;
		return false ;
	}
	else 
		$('#activity_status_err_desc').hide() ;
    
	if ($('#txtActivityStatusCode').val()=="")
	{
		$('#activity_status_err_code').show() ;
		$('#txtActivityStatusCode').focus() ;
		swal("Expense Item code can not be blank.") ;
		return false ;
	}
	else 
		$('#activity_status_err_code').hide() ;
			
	return true ;
}