datatable=null;
$(document).ready(function() 
{ 
	$('#btnAuditLogReportView').button().bind('click',viewAuditLogReport) ;
	$('#btnAuditLogReportPrint').button().bind('click',printAuditLogReport) ;
	$('#btnAuditLogReportExport').button().bind('click',exportAuditLogReport) ;
}) ;

var dteopt = {
		    todayHighlight: true,
		    orientation: "bottom left",
		    autoclose: true,
		    format: 'dd/mm/yyyy',
		    templates: {
		        leftArrow: '<i class="la la-angle-left"></i>',
		        rightArrow: '<i class="la la-angle-right"></i>'
		    }
		}
	$(".datepicker-comp").datepicker(dteopt) ;	

	$('#txtAuditLogReportStart').datepicker(dteopt) ;
	$('#txtAuditLogReportEnd').datepicker(dteopt) ;

	var date = new Date();
	var firstDay = new Date();
	var lastDay = new Date();
	
	firstDay.setFullYear(date.getFullYear(), date.getMonth(), 1);
	lastDay.setFullYear(date.getFullYear(), date.getMonth() + 1, 0);

function exportAuditLogReport() {
	if (validateAuditLogReport()) {
		var url = "report.pzx?c=" + audit_log_report_url + "&d=" + new Date().getTime() +
                "&code=" + $('#cobAuditLogReportUserId').val() + 
                "&dt=" + $('#txtAuditLogReportStart').val() + 
                "&dtend=" + $('#txtAuditLogReportEnd').val() + 
            "&t=" + C_EXPORT ;
		showReport(url); ;
	}
}
function printAuditLogReport() {
	var url = "report.pzx?c=" + audit_log_report_url + "&d=" + new Date().getTime() +
                "&code=" + $('#cobAuditLogReportUserId').val() + 
                "&dt=" + $('#txtAuditLogReportStart').val() + 
                "&dtend=" + $('#txtAuditLogReportEnd').val() + 
                "&t=" + C_REPORT ;
	showReport(url) ;
}
function viewAuditLogReport() {
	if (validateAuditLogReport()) {
        var data = { "type": C_LIST, 
                "code": $('#cobAuditLogReportUserId').val(), 
                "start": $('#txtAuditLogReportStart').val(), 
                "end": $('#txtAuditLogReportEnd').val()  } ;
        var url = "request.pzx?c=" + audit_log_report_url + "&d=" + new Date().getTime() ;
        callServer(url,"json",data,showAuditLogReport) ;
    }
}
function validateAuditLogReport() {
	if ($('#cobAuditLogReportUserId').val()=='') {
		alert('You must select a user for the report.') ;
		$('#cobAuditLogReportUserId').focus() ;
		return false ;
	}
	return true ;
}
function showAuditLogReport(obj,resp) {
	dataJSONArray = resp;
	init_dtt();
	$('#auditlogtable').mDatatable('destroy');
	datatable = $('#auditlogtable').mDatatable(option);
}

function init_dtt() {	
	option = {
			// datasource definition
			data: {
				type: 'local',
				source: dataJSONArray,
				pageSize: 10
			},

			// layout definition
			layout: {
				theme: 'default', // datatable theme
				class: '', // custom wrapper class
				scroll: false, // enable/disable datatable scroll both horizontal and vertical when needed.
				// height: 450, // datatable's body's fixed height
				footer: false // display/hide footer
			},

			// column sorting
			sortable: true,

			pagination: true,

			// search: {
			// 	input: $('#generalSearch')
			// },

			// inline and bactch editing(cooming soon)
			// editable: false,

			// columns definition
			columns: [{
				field: "Datetime",
				title: "Datetime",
				width: 120
			}, {
				field: "User",
				title: "User",
				width: 250
			}, {
				field: "Table",
				title: "Table",
				width: 250
			}, {
				field: "Description",
				title: "Description",
				width: 800
			}]
		};
}

