var product_obj = null ;

$(document).ready(function() 
{ 
	$('#formProduct').hide() ;
	$('#btnProductClear').button().bind('click',clearProduct) ;
	$('#btnProductUpdate').button().bind('click',updateProduct) ;
	$('#btnProductAdd').button().bind('click',addProduct) ;
	$('#btnProductPrint').button().bind('click',printProduct) ;
	// $('#product-tabs').tabs() ;
	$('#txtProductDesc').focus() ;
	$(window).resize(resizeProductGrid) ;
	resizeProductGrid() ;
	clearProduct();
	filterProduct();
}) ;

function showFormProduct(){
	clearProduct();
	$('#formProduct').show('slow') ;
}

function closeFormProduct(){
	clearProduct() ;
	$('#formProduct').hide('slow') ;
}	

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

function clearProduct() {
	$('#txtProductId').val("Auto") ;
	$('#txtProductDesc').val("") ;
	$('#txtProductCode').val("") ;
	// $('#product-tabs').tabs("option", "active", 0) ;
	$('#txtProductDesc').focus() ;
	$('#btnProductUpdate').prop('disabled','disabled') ;
	$('#product_err_mesg').text('') ;
	$('#product_err_desc').hide() ;
	$('#product_err_code').hide() ;
	if (product_obj != null && product_obj != undefined)
	{
		$($(product_obj).closest("tr")).removeClass("sbg-row-selected") ;
	}
	product_obj = null;
}

function filterProduct() {
	var data = { "type": C_LIST} ;
	var url = "request.pzx?c=" + product_url + "&d=" + new Date().getTime() ;		
	callServer(url,"json",data,showProductFilterList,product_obj) ;
}

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

function init_dt() {
	var datatable = $('.sbg-product-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 updateProduct() {
	saveProduct(C_UPDATE) ;
}
function addProduct() {
	saveProduct(C_ADD) ;
}
function saveProduct(type) {
	if (!validateProduct()) {
		return false;
	}
	var data = { "type": type, 
                    "id": $('#txtProductId').val(), 
                    "desc": $('#txtProductDesc').val(), 
                    "code": $('#txtProductCode').val()};
	var url = "request.pzx?c=" + product_url + "&d=" + new Date().getTime() ;
	callServer(url,"json",data,onProductResponse,product_obj) ;
}

function editProduct(id,obj) {
	var data = { "type": C_GET,"id": id} ;
	var url = "request.pzx?c=" + product_url + "&d=" + new Date().getTime();		
	clearProduct();
	showFormProduct();
	product_obj = obj ;
	$($(obj).closest("tr")).addClass("sbg-row-selected");
	callServer(url,"json",data,showProduct,obj) ;
}

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

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

	swal("Confirm you want to delete Product 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=" + product_url + "&d=" + new Date().getTime() ;
			  callServer(url,"json",data,onProductResponse,obj) ;
		  break;
		}
	  });
}
function showProduct(obj,resp) {
	if (resp.status != C_OK) {
		swal("System Message",resp.mesg) ;
		return false;
	}
	
	$('#txtProductId').val(resp.data.id) ;
	$('#txtProductDesc').val(resp.data.desc) ;
	$('#txtProductCode').val(resp.data.code) ;
	$('#btnProductUpdate').prop('disabled','') ;
	$('#txtProductDesc').focus() ;
}
function printProduct() {
	var url = "report.pzx?c=" + product_url + "&d=" + new Date().getTime();
	showReport(url) ;
}
function onProductResponse(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) ;
	clearProduct() ;
	filterProduct();
	closeFormProduct();
}
function validateProduct() {
	$('#product_err_mesg').text('') ;
	if ($('#txtProductDesc').val()=="")
	{
		$('#product_err_desc').show() ;
		$('#txtProductDesc').focus() ;
		swal("Product description can not be blank.") ;
		return false ;
	}
	else 
		$('#product_err_desc').hide() ;
    
	if ($('#txtProductCode').val()=="")
	{
		$('#product_err_code').show() ;
		$('#txtProductCode').focus() ;
		swal("Product code can not be blank.") ;
		return false ;
	}
	else 
		$('#product_err_code').hide() ;
			
	return true ;
}