var resetpwd_obj = null;

$(document).ready(function() 
{ 
	$('#formResetPwd').hide() ;
	// $('#resetpwd-tabs').tabs() ;
	$('#btnResetPwdClear').button().bind('click',clearResetPwd) ;
	$('#btnResetPwd').button().bind('click',resetPassword) ;
	
	$(window).resize(resizeResetPwdGrid) ;
	//scrollbarWidth = getScrollbarWidth() ;
	resizeResetPwdGrid() ;
	clearResetPwd() ;
	filterResetPwd();
}) ;

function showFormResetPwd(){
	clearResetPwd() ;
	$('#formResetPwd').show('slow') ;
}

function closeFormResetPwd(){
	clearResetPwd() ;
	$('#formResetPwd').hide('slow') ;
}	

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

function clearResetPwd() {
	$('#lblResetPwdId').text("") ;
	$('#lblResetPwdName').text("") ;
	$('#lblResetPwdFull').text("") ;
	$('#btnResetPwd').prop('disabled','disabled') ;
}

function filterResetPwd() {
	var data = { "type": C_LIST} ;
	var url = "request.pzx?c=" + resetpwd_url + "&d=" + new Date().getTime() ;		
	callServer(url,"json",data,showResetPwdFilterList,resetpwd_obj) ;
}

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

function init_dt() {
	var datatable = $('.sbg-resetpwd-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: "name",
				title: "Name",
				width:150,
			}, {
				field: "fullname",
				title: "Full Name",
				width:200,
			}, {
				field: 'actions',
				title: 'Actions',
				sortable: true,
				width: 460,
				overflow: 'visible',
		}],
	});
}

function resetPassword() {
	if (validateResetPwd()) {
		var data = { "type": C_NEW, "id": $('#lblResetPwdId').text() } ;
		var url = "request.pzx?c=" + resetpwd_url + "&d=" + new Date().getTime() ;
		callServer(url,"json",data,onResetPwdResponse,null) ;
	}
}
function editResetPwd(id,obj) {
	var data = { "type": C_GET,"id": id} ;
	var url = "request.pzx?c=" + resetpwd_url + "&d=" + new Date().getTime();		
	showFormResetPwd();
	callServer(url,"json",data,showResetPwd,null) ;
}

function showResetPwd(obj,resp) {
	if (resp.status == C_OK) {
		$('#lblResetPwdId').text(resp.data.id) ;
		$('#lblResetPwdName').text(resp.data.name) ;
		$('#lblResetPwdFull').text(resp.data.fullname) ;
		$('#lblResetPwdLogin').text(resp.data.login) ;
		$('#btnResetPwd').prop('disabled','') ;
	}
	else {
		swal("System Message",resp.mesg) ;
	}
}
function onResetPwdResponse(obj,resp) {
	if (resp.status == C_OK) {
		swal("System Message",resp.mesg) ;
		clearResetPwd() ;
	}
	else 
		swal("Error",resp.mesg) ;
}
function validateResetPwd() {
	return true ;
}