var brand_obj = null ;

$(document).ready(function() 
{ 
	$('#formBrand').hide() ;
	$('#btnBrandClear').button().bind('click',clearBrand) ;
	$('#btnBrandUpdate').button().bind('click',updateBrand) ;
	$('#btnBrandAdd').button().bind('click',addBrand) ;
	$('#btnBrandPrint').button().bind('click',printBrand) ;
	// $('#brand-tabs').tabs() ;
	$('#txtBrandDesc').focus() ;
	$(window).resize(resizeBrandGrid) ;
	resizeBrandGrid() ;
	clearBrand();
	filterBrand();
}) ;

function showFormBrand(){
	clearBrand();
	$('#formBrand').show('slow') ;
}

function closeFormBrand(){
	clearBrand() ;
	$('#formBrand').hide('slow') ;
}	

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

function clearBrand() {
	$('#txtBrandId').val("Auto") ;
	$('#txtBrandDesc').val("") ;
	$('#txtBrandCode').val("") ;
	$('#txtBrandRef').val("") ;
	// $('#brand-tabs').tabs("option", "active", 0) ;
	$('#txtBrandDesc').focus() ;
	$('#btnBrandUpdate').prop('disabled','disabled') ;
	$('#brand_err_mesg').text('') ;
	$('#brand_err_desc').hide() ;
	$('#brand_err_code').hide() ;
	if (brand_obj != null && brand_obj != undefined)
	{
		$($(brand_obj).closest("tr")).removeClass("sbg-row-selected") ;
	}
	brand_obj = null;
}

function filterBrand() {
	var data = { "type": C_LIST} ;
	var url = "request.pzx?c=" + brand_url + "&d=" + new Date().getTime() ;		
	callServer(url,"json",data,showBrandFilterList,brand_obj) ;
}

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

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

function updateBrand() {
	saveBrand(C_UPDATE) ;
}
function addBrand() {
	saveBrand(C_ADD) ;
}
function saveBrand(type) {
	if (!validateBrand()) {
		return false;
	}
	var data = { "type": type, 
                    "id": $('#txtBrandId').val(), 
                    "code": $('#txtBrandCode').val(),
                    "desc": $('#txtBrandDesc').val(), 
                    "ref": $('#txtBrandRef').val()};
	var url = "request.pzx?c=" + brand_url + "&d=" + new Date().getTime() ;
	callServer(url,"json",data,onBrandResponse,brand_obj) ;
}

function editBrand(id,obj) {
	var data = { "type": C_GET,"id": id} ;
	var url = "request.pzx?c=" + brand_url + "&d=" + new Date().getTime();		
	clearBrand();
	showFormBrand();
	brand_obj = obj ;
	$($(obj).closest("tr")).addClass("sbg-row-selected");
	callServer(url,"json",data,showBrand,obj) ;
}

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

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

	swal("Confirm you want to delete Brand 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=" + brand_url + "&d=" + new Date().getTime() ;
			  callServer(url,"json",data,onBrandResponse,obj) ;
		  break;
		}
	  });
}
function showBrand(obj,resp) {
	if (resp.status != C_OK) {
		swal("System Message",resp.mesg) ;
		return false;
	}
	
	$('#txtBrandId').val(resp.data.id) ;
	$('#txtBrandDesc').val(resp.data.desc) ;
	$('#txtBrandCode').val(resp.data.code) ;
	$('#txtBrandRef').val(resp.data.ref) ;
	$('#btnBrandUpdate').prop('disabled','') ;
	$('#txtBrandDesc').focus() ;
}
function printBrand() {
	var url = "report.pzx?c=" + brand_url + "&d=" + new Date().getTime();
	showReport(url) ;
}
function onBrandResponse(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) ;
	clearBrand() ;
	filterBrand();
	closeFormBrand();
}
function validateBrand() {
	$('#brand_err_mesg').text('') ;
	if ($('#txtBrandDesc').val()=="")
	{
		$('#brand_err_desc').show() ;
		$('#txtBrandDesc').focus() ;
		swal("Brand description can not be blank.") ;
		return false ;
	}
	else 
		$('#brand_err_desc').hide() ;
    
	if ($('#txtBrandCode').val()=="")
	{
		$('#brand_err_code').show() ;
		$('#txtBrandCode').focus() ;
		swal("Brand code can not be blank.") ;
		return false ;
	}
	else 
		$('#brand_err_code').hide() ;
			
	return true ;
}