var item_obj = null ;

$(document).ready(function() 
{ 
	$('#formItem').hide() ;
	$('#btnItemClear').button().bind('click',clearItem) ;
	$('#btnItemUpdate').button().bind('click',updateItem) ;
	$('#btnItemAdd').button().bind('click',addItem) ;
	$('#btnItemPrint').button().bind('click',printItem) ;
	// $('#item-tabs').tabs() ;
	$('#txtItemDesc').focus() ;
	$(window).resize(resizeItemGrid) ;
	resizeItemGrid() ;
	clearItem();
	filterItem();
}) ;

function showFormItem(){
	clearItem();
	$('#formItem').show('slow') ;
}

function closeFormItem(){
	clearItem() ;
	$('#formItem').hide('slow') ;
}	

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

function clearItem() {
	$('#txtItemId').val("Auto") ;
	$('#txtItemDesc').val("") ;
	$('#txtItemCode').val("") ;
	$('#txtItemBrand').val("") ;
	// $('#item-tabs').tabs("option", "active", 0) ;
	$('#txtItemDesc').focus() ;
	$('#btnItemUpdate').prop('disabled','disabled') ;
	$('#item_err_mesg').text('') ;
	$('#item_err_desc').hide() ;
	$('#item_err_code').hide() ;
	if (item_obj != null && item_obj != undefined)
	{
		$($(item_obj).closest("tr")).removeClass("sbg-row-selected") ;
	}
	item_obj = null;
}

function filterItem() {
	var data = { "type": C_LIST,"code":$('#filterCode').val(),"desc":$('#filterDesc').val(),"brand":$('#filterBrand').val()} ;
	var url = "request.pzx?c=" + item_url + "&d=" + new Date().getTime() ;		
	callServer(url,"json",data,showItemFilterList,item_obj) ;
}

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

function init_dt() {
	var datatable = $('.sbg-item-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:60,
			}, {
				field: "code",
				title: "Code",
				width:100,
			}, {
				field: "desc",
				title: "Description",
				width:200,
			}, {
				field: "brand",
				title: "Brand",
				width: 100,
			}, {
				field: 'actions',
				title: '',
				sortable: true,
				width: 470,
				overflow: 'visible',
		}],
	});
}

function updateItem() {
	saveItem(C_UPDATE) ;
}
function addItem() {
	saveItem(C_ADD) ;
}
function saveItem(type) {
	if (!validateItem()) {
		return false;
	}
	var data = { "type": type, 
                    "id": $('#txtItemId').val(), 
                    "code": $('#txtItemCode').val(),
                    "desc": $('#txtItemDesc').val(), 
                    "brand": $('#txtItemBrand').val()};
	var url = "request.pzx?c=" + item_url + "&d=" + new Date().getTime() ;
	callServer(url,"json",data,onItemResponse,item_obj) ;
}

function editItem(id,obj) {
	var data = { "type": C_GET,"id": id} ;
	var url = "request.pzx?c=" + item_url + "&d=" + new Date().getTime();		
	clearItem();
	showFormItem();
	item_obj = obj ;
	$($(obj).closest("tr")).addClass("sbg-row-selected");
	callServer(url,"json",data,showItem,obj) ;
}

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

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

	swal("Confirm you want to delete Item 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=" + item_url + "&d=" + new Date().getTime() ;
			  callServer(url,"json",data,onItemResponse,obj) ;
		  break;
		}
	  });
}
function showItem(obj,resp) {
	if (resp.status != C_OK) {
		swal("System Message",resp.mesg) ;
		return false;
	}
	
	$('#txtItemId').val(resp.data.id) ;
	$('#txtItemDesc').val(resp.data.desc) ;
	$('#txtItemCode').val(resp.data.code) ;
	$('#txtItemBrand').val(resp.data.brand) ;
	$('#btnItemUpdate').prop('disabled','') ;
	$('#txtItemDesc').focus() ;
}
function printItem() {
	var url = "report.pzx?c=" + item_url + "&d=" + new Date().getTime();
	showReport(url) ;
}
function onItemResponse(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) ;
	clearItem() ;
	filterItem();
	closeFormItem();
}
function validateItem() {
	$('#item_err_mesg').text('') ;
	if ($('#txtItemDesc').val()=="")
	{
		$('#item_err_desc').show() ;
		$('#txtItemDesc').focus() ;
		swal("Item description can not be blank.") ;
		return false ;
	}
	else 
		$('#item_err_desc').hide() ;
    
	if ($('#txtItemCode').val()=="")
	{
		$('#item_err_code').show() ;
		$('#txtItemCode').focus() ;
		swal("Item code can not be blank.") ;
		return false ;
	}
	else 
		$('#item_err_code').hide() ;
			
	return true ;
}