var ind = 0;
var tot_rep=0;
var effective_sale_tot = 0;
var tot_response = 0;
var allow_toexport = false;
jQuery(document).ready(function(){
	$("input[name='from'], input[name='to']").attr("autocomplete", "off");
	
	$( "#from-date" ).datepicker({	
		changeMonth: true,
		changeYear: true,
		dateFormat: 'yy-mm-dd',     
		onClose: function( selectedDate ) {
			$( "#to-date" ).datepicker( "option", "minDate", selectedDate );
		 }
    });
	
	$( "#to-date" ).datepicker({
		changeMonth: true,
		changeYear: true,
		dateFormat: 'yy-mm-dd',
		onClose: function( selectedDate ) {
			$( "#from-date" ).datepicker( "option", "maxDate", selectedDate );
		}
    });
	$('#export_report').click(function(){
		if(allow_toexport){
			 
			 $(".tbl-list").table2excel({
					exclude: ".tbl-list",
					name: "Summary Report",
					filename: $('#p-title').html()
				});
			 
			 return false;
		}else{
			alert("Report is loading, Please wait for a while...");
		}
	});
	tot_rep = locationsObj.length;	
	
	if($( "#from-date" ).val().length > 0){		
		get_summary_report(locationsObj[ind]);
	}else{
		alert("Please select dates.");
	}
	
});

function get_summary_report(reportObj){
	var colid = reportObj.id;
	allow_toexport = false;
	jQuery.ajax({
		type: 'POST',
		url: siteUrl+'overall/getapireport.json',
		data: {url:reportObj.url, d1:$( "#from-date" ).val(), d2:$( "#to-date" ).val(), tmonth:$( "#month" ).val()},
		dataType: 'json',
		beforeSend: function(){
			ind = ind+1;
			$('#'+colid).append('<img src="'+siteUrl+'img/loading20.gif" class="loading" />');
		},
		success: function(data){
			$('.loading').remove();
			do_column_val(data, reportObj);
			if(tot_rep > ind){
				get_summary_report(locationsObj[ind]);
			}else{
				allow_toexport=true;
			}
			//alert(data);
		},
		statusCode: {
			404: function() {
				$('.loading').remove();
				alert( message.noPage );
			}
		}
	});
}
function do_column_val(data, reportObj){
	var tot_reports = reportsObj.length;
	var colId = reportObj.id;
	var dataObj = new Array();
	if(data.data){
		dataObj = jQuery.parseJSON(data.data);
	}
	if(data.error == "1"){
		$('#error_mess').html("Response not got from \""+reportObj.title+"\"");
	}
	for (var i=0; i < tot_reports; i++){
		var cur_rep = reportsObj[i];
		//alert(cur_rep);
		if(dataObj[cur_rep]){
			
			var cur_val = parseFloat(dataObj[cur_rep]);
			var getTotal = $('#'+cur_rep+'_total').html();
			if(getTotal){
				getTotal = parseFloat(getTotal);
			}else{
				getTotal = 0;
			}
			
			if(cur_rep == "effective_sale_call"){
				tot_response += 1;
				effective_sale_tot += cur_val;
				var effsc = cur_val.toFixed(2);
				var totEffCall = effective_sale_tot / tot_response ;
				totEffCall = totEffCall.toFixed(2);
				$('#'+cur_rep+'_'+colId).html(effsc+' %');
				$('#'+cur_rep+'_total').html(totEffCall+' %');
				
			}else if(cur_rep == "per_active_users"){
				$('#'+cur_rep+'_'+colId).html((dataObj[cur_rep]).toFixed(2)+' %');
				
				var actual_users_total = $('#actual_users_total').text();
				var active_users_total = $('#active_users_total').text();
				
				var per_active_users_total = ((parseFloat(active_users_total)/parseFloat(actual_users_total)) * 100).toFixed(2); 
				
				$('#'+cur_rep+'_total').html(per_active_users_total+' %');
			}
			else{
				
				$('#'+cur_rep+'_'+colId).html(dataObj[cur_rep]);
				$('#'+cur_rep+'_total').html((cur_val+getTotal).toFixed(2));
			}
			
		}else{
			$('#'+cur_rep+'_'+colId).html(0);			
		}
	}
}