var activity_consultant_obj = null ;

$(document).ready(function() 
{ 
	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>'
		    }
	};
	$('#txtActivityBillHour').datepicker(dteopt) ;
	$('#txtActivityBillHour').val(getCurrentDate(0)) ;

	$('#txtActivityNonBillHour').datepicker(dteopt) ;
	$('#txtActivityNonBillHour').val(getCurrentDate(0)) ;

	$('#txtFromProsCDateSearch').datepicker(dteopt) ;
	$('#txtFromProsCDateSearch').val(getCurrentDate(0)) ;

	$('#txtProsCToDateSearch').datepicker(dteopt) ;
	$('#txtProsCToDateSearch').val(getCurrentDate(0)) ;

	$('#btnActivityConsultantClear').button().bind('click',clearActivityConsultant) ;
	$('#btnActivityConsultantUpdate').button().bind('click',updateActivityConsultant) ;
	$('#btnActivityConsultantAdd').button().bind('click',addActivityConsultant) ;
	
	$('#btnConsultantList').button().bind('click',filterActivityConsultant) ;

	$('#btnConsultantPdf').button().bind('click',printActivityConsultantPdf) ;
	$('#btnConsultantExcel').button().bind('click',printActivityConsultantExcel) ;

	// $('#activity-consultant-tabs').tabs() ;
	$(window).resize(resizeActivityConsultantGrid) ;
	resizeActivityConsultantGrid() ;
	clearActivityConsultant();
}) ;

function printActivityConsultantPdf() {
	var url = "report.pzx?c=" + activity_consultant_url + 
	"&empIdBegin="+$('#cmbConsultantName').val()+
	"&pr="+$('#comConsultantProject').val()+
	"&dt="+$("#txtFromProsCDateSearch").val()+
	"&dtend="+$("#txtProsCToDateSearch").val() +
	"&t=r"+"&d=" + new Date().getTime();
	showReport(url) ;
}
function printActivityConsultantExcel() {
	var url = "report.pzx?c=" + activity_consultant_url +
	"&empIdBegin="+$('#cmbConsultantName').val()+
	"&pr="+$('#comConsultantProject').val()+
	"&dt="+$("#txtFromProsCDateSearch").val()+
	"&dtend="+$("#txtProsCToDateSearch").val() +
	"&t=e"+ "&d=" + new Date().getTime();
	showReport(url) ;
}

function getCurrentDate(addDays) {
	var year, month, day, now ;
	now = new Date() ;
	now.setDate(now.getDate() + addDays); 
    year = String(now.getFullYear()); 
    month = String(now.getMonth() + 1); 
    if (month.length == 1) { 
        month = "0" + month; 
    } 
    day = String(now.getDate()); 
    if (day.length == 1) { 
        day = "0" + day; 
    } 
    return day + "/" + month + "/" + year ;
}

function filterActivityConsultant() {
	
	var data = { "type": C_LIST, 
				"consultant": $('#cmbConsultantName').val(), 
				"project": $('#comConsultantProject').val(),  
				"fromdate": $("#txtFromProsCDateSearch").val(), 
				"todate": $("#txtProsCToDateSearch").val() } ;
	var url = "request.pzx?c=" + activity_consultant_url + "&d=" + new Date().getTime() ;
	
	callServer(url,"json",data,showConsultantList,activity_consultant_obj) ;
}

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

function init_dt() {
	var datatable = $('.sbg-activity-consultant-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: "name",
				title: "Name",
				width:150,
			}, {
				field: "jobnumber",
				title: "Job Number",
				width:120,
			}, {
				field: "jobname",
				title: "Job Name",
				width:250,
			}, {
				field: "billable",
				title: "Billable",
				width:80,
			}, {
				field: "nonbillable",
				title: "Non Billable",
				sortable: true,
				width: 420,
				overflow: 'visible',
			}],
	});
}

function resizeActivityConsultantGrid() {
	var h = $("#sbg-center-panel").outerHeight() - $("#sbg-activity-consultant-entry").outerHeight() - 55;
	$("div#sbg-activity-consultant-data").css("height", h +'px') ;		
}
function clearActivityConsultant() {
	$('#txtActivityConsultantId').val("Auto") ;
	$('#cmbConsultantName').val(0) ;
	$('#comConsultantProject').val(0) ;
	// $('#activity-consultant-tabs').tabs("option", "active", 0) ;
	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>'
		    }
	};
	$('#txtActivityBillHour').datepicker(dteopt) ;
	$('#txtActivityBillHour').val(getCurrentDate(0)) ;

	$('#txtActivityNonBillHour').datepicker(dteopt) ;
	$('#txtActivityNonBillHour').val(getCurrentDate(0)) ;

	$('#cmbConsultantName').focus() ;
	$('#btnActivityConsultantUpdate').prop('disabled','disabled') ;
	$('#activity_consultant_err_mesg').text('') ;
	$('#activity_consultant_err_desc').hide() ;
	$('#activity_consultant_err_code').hide() ;
	if (activity_consultant_obj != null && activity_consultant_obj != undefined)
	{
		$($(activity_consultant_obj).closest("tr")).removeClass("sbg-row-selected") ;
	}
	activity_consultant_obj = null;
}
function updateActivityConsultant() {
	saveActivityConsultant(C_UPDATE) ;
}
function addActivityConsultant() {
	saveActivityConsultant(C_ADD) ;
}
function saveActivityConsultant(type) {
	if (!validateActivityConsultant()) {
		return false;
	}
	
	var data = { "type": type, 
                    "id": $('#txtActivityConsultantId').val(), 
                    "consultant": $('#txtActivityConsultantName').val(), 
                    "project": $('#txtActivityConsultantProject').val(),
                	"bill_hour": $("#txtActivityBillHour").val(), 
					"none_bill_hour": $("#txtActivityBillHour").val() };
	var url = "request.pzx?c=" + activity_consultant_url + "&d=" + new Date().getTime() ;
	callServer(url,"json",data,onActivityConsultantResponse,activity_consultant_obj) ;
}

function editActivityConsultant(id,obj) {
	var data = { "type": C_GET,"id": id} ;
	var url = "request.pzx?c=" + activity_consultant_url + "&d=" + new Date().getTime();		
	clearActivityConsultant();
	activity_consultant_obj = obj ;
	$($(obj).closest("tr")).addClass("sbg-row-selected");
	callServer(url,"json",data,showActivityConsultant,obj) ;
}

function deleteActivityConsultant(id,obj) {
	if (confirm("Confirm you want to delete Expense Item id : " + id + "?") == false) {
		return false;
	}
	
	var data = {"type": C_DELETE,"id": id} ;
	var url = "request.pzx?c=" + activity_consultant_url + "&d=" + new Date().getTime() ;
	callServer(url,"json",data,onActivityConsultantResponse,obj) ;
}
function showActivityConsultant(obj,resp) {
	if (resp.status != C_OK) {
		swal("System Message",resp.mesg) ;
		return false;
	}
	
	$('#txtActivityConsultantId').val(resp.data.id) ;
	$('#txtActivityConsultantName').val(resp.data.consultant) ;
	$('#txtActivityConsultantProject').val(resp.data.project) ;
	$('#txtActivityNonBillHour').val(resp.data.none_bill_hour) ;
	$('#txtActivityBillHour').val(resp.data.bill_hour) ;
	$('#btnActivityConsultantUpdate').prop('disabled','') ;
	$('#txtActivityStatusDesc').focus() ;
}

function onActivityConsultantResponse(obj,resp) {
	if (resp.status != C_OK) {
		swal("Error",resp.mesg) ;
		return;
	}
	if (resp.type == C_ADD) {
		$('#sbg-activity-consultant-table tr:first').after("<tr><td>" + resp.data + "</td>" + 
			"<td>" + $('#txtActivityConsultantName option:selected').text() + "</td>" + 
			"<td>" + $('#txtActivityConsultantProject option:selected').text() + "</td>" + 
			"<td>" + $('#txtActivityBillHour').val() + "</td>" + 
			"<td>" + $('#txtActivityNonBillHour').val() + "</td>" + 
			"<td style='text-align:center'><a href='javascript:' onclick='editActivityConsultant(" + resp.data + ",this)'><img src='image/edit_16.png' title='Edit'></img></a></td>" +
			"<td style='text-align:center'><a href='javascript:' onclick='deleteActivityConsultant(" + resp.data + ",this)'><img src='image/delete_16.png' title='Delete'></img></a></td>" +
			"</tr>"); 
	} else if (resp.type == C_DELETE) {
		$($(obj).closest("tr")).remove() ;
	} else if (resp.type == C_UPDATE) {
		$($(obj).closest('tr')).children('td:eq(1)').html($('#txtActivityConsultantName option:selected').text()) ;
		$($(obj).closest('tr')).children('td:eq(2)').html($('#txtActivityConsultantProject option:selected').text()) ;
		$($(obj).closest('tr')).children('td:eq(3)').html($('#txtActivityBillHour').val()) ;
		$($(obj).closest('tr')).children('td:eq(4)').html($('#txtActivityNonBillHour').val()) ;
	}
	$('#sbg-popup-box').html(resp.mesg) ;
	$('#sbg-popup-box').show() ;
	$('#sbg-popup-box').fadeOut(5000) ;
	clearActivityConsultant() ;
}
function validateActivityConsultant() {
	$('#activity_consultant_err_mesg').text('') ;
	if ($('#txtActivityConsultantName').val()==0)
	{
		$('#activity_consultant_err_desc').show() ;
		$('#txtActivityConsultantName').focus() ;
		$('#activity_consultant_err_mesg').text("Please select consultant name.") ;
		return false ;
	}
	else 
		$('#activity_consultant_err_desc').hide() ;
    
	if ($('#txtActivityConsultantProject').val()==0)
	{
		$('#activity_consultant_err_code').show() ;
		$('#txtActivityConsultantProject').focus() ;
		$('#activity_consultant_err_mesg').text("Please select Job.") ;
		return false ;
	}
	else 
		$('#activity_consultant_err_code').hide() ;
			
	return true ;
}