<%  @calendar_select_interval = 2    %>
<div id="stat_export_background"></div>
<div id="stat_export_left_container">
		<div class="stat_export_table_head"  style="top:0px"><%= t(:filters_s) %>:</div>
		<div style="width:280px;;height:90px;position:absolute;top:30px">
		<% 
			@types = []
			@types.push({:name => "operation", :text => t('.by_oper')})
			@types.push({:name => "user", :text => t('.by_login')})
			@types.push({:name => "office", :text => t('.by_office')})
		%>
		<% @types.each do |type|%>
			<div class="stat_export_filter_container">
				<%= render partial: "shared/check_box_v2", locals: {
					id_name:type[:name],
					class_name:'',
					style_custom:'',
					top:'0px',
					left:'5px',
					width:'100%',
					click_action:"cb_action_#{type[:name]}()",
					hover_action:'',
					text:type[:text]
				}%>
			</div>
		<% end %>
		</div>
		<div class="stat_export_table_head"  style="top:120px"><%= t(:group)%>:</div>
		<div style="width:280px;;height:150px;position:absolute;top:150px">
		<% 
			@group = []
			@group.push({:name => "operation_group", :text => t('.by_oper')})
			@group.push({:name => "user_group", :text => t('.by_login')})
			@group.push({:name => "office_group", :text => t('.by_office')})
			@group.push({:name => "hour_group", :text => t('.by_hour')})
			@group.push({:name => "day_group", :text => t('.by_days')})
		%>
		<% @group.each do |type|%>
			<div class="stat_export_filter_container">
				<%= render partial: "shared/check_box_v2", locals: {
					id_name:type[:name],
					class_name:'',
					style_custom:'',
					top:'0px',
					left:'5px',
					width:'100%',
					click_action:'statistic_report_update_columns()',
					hover_action:'',
					text:type[:text]
				}%>
			</div>
		<% end %>
		</div>
	<div class="button" id="stat_export_button" onclick="statistic_report_build_report()" ><%= t('.build_report')%></div>	
</div>
<div id="stat_export_main_container">
	<% @groups=[
		{:name => 'oper', :full_name => 'operation', :rus => t(:oper_search), :cb => 'oper'},
		{:name => 'login', :full_name => 'login', :rus => t(:login_search), :cb => 'user'},
		{:name => 'office', :full_name => 'office', :rus => t(:office_search), :cb => 'office'}
	] 
	@groups.each do |group|
	%>
	<div class="stat_export_block_outer_container">
		<div class="stat_export_block_inner_container">
			<div class="stat_export_block_search_container">
				<%= render partial: "shared/search_list", locals: {
					select_id:group[:name],
					select_name:group[:rus],
					search_command:"#{group[:full_name]}_sort",
					drop_direction:'down',
					delete_function:0
				} %>
			</div>
			<div class="stat_export_block_table_container">
				<table id="statistic_report_table_with_<%= group[:cb] %>" class="table_body"></table>
			</div>
			<div class="stat_export_block_cb_container">
				<%= render partial: "shared/check_box_v2", locals: {
					id_name:"#{group[:cb]}_inverse",
					class_name:'',
					style_custom:'',
					top:'0px',
					left:'5px',
					width:'100%',
					click_action:"cb_action_#{group[:cb]}_inverse()",
					hover_action:'',
					text:t('.exclude')
				}%>
			</div>
			<div id="statistic_report_<%= group[:cb] %>_hider" class="stat_export_block_table_hider">
				<div class="stat_export_block_table_hider_text" ><%= t('.inactive')%></div>
			</div>
		</div>
	</div>
	<% end %>
</div>
<%
columns_list=[
	{:id => 0, :name => 'oper_name', :description => t('.oper_name'), :show => 1, :select => 1},
	{:id => 1, :name => 'login_name', :description => t('.login_name'), :show => 1, :select => 1},
	{:id => 2, :name => 'start_time', :description => t('.start_time'), :show => 1, :select => 1},
	{:id => 3, :name => 'end_time', :description => t('.end_time'), :show => 1, :select => 1},
	{:id => 4, :name => 'duration', :description => t('.duration'), :show => 1, :select => 1},
	{:id => 5, :name => 'user_time', :description => t('.user_time'), :show => 1, :select => 1},
	{:id => 6, :name => 'sys_time', :description => t('.sys_time'), :show => 1, :select => 1},
	{:id => 7, :name => 'type_name', :description => t('.type_name'), :show => 1, :select => 0},
	{:id => 8, :name => 'avg_duration', :description => t('.avg_duration'), :show => 1, :select => 0},
	{:id => 9, :name => 'avg_user_time', :description => t('.avg_user_time'), :show => 1, :select => 0},
	{:id => 10, :name => 'avg_sys_time', :description => t('.avg_sys_time'), :show => 1, :select => 0},
	{:id => 11, :name => 'office_name', :description => t('.office_name'), :show => 1, :select => 0},
	{:id => 12, :name => 'Hour', :description => t('.hour'), :show => 0, :select => 0},
	{:id => 13, :name => 'Day', :description => t('.day'), :show => 0, :select => 0},
	{:id => 14, :name => 'count', :description => t('.count'), :show => 0, :select => 0}	
]
	
%>
<div style="width:calc(100% - 335px);left:310px;top:calc(50% + 0px);height:calc(50% - 10px);background:<%= $white %>;position:absolute;border:1px solid <%= $gray %>">
	<div class="text_header_red_line" style="width:500px;height:30px;top:0px;left:10px;">Выбор полей</div>
	<div id="export_columns_selector_container" style="width:calc(100% - 10px);height:calc(100% - 50px);position:absolute;top:40px;left:5px;oveflow:scroll;overflow-x:hidden">
	<% columns_list.each do |column|%>
		<div id="export_column_container_<%= column[:id] %>" style="width:400px;float:left;height:40px;position:relative;<%= column[:show] == 0 ? 'display:none' : '' %>">
			<%= render partial: "shared/check_box_v2", locals: {
				id_name:"#{column[:name]}_column",
				class_name:'',
				style_custom:'',
				top:'5px',
				left:'5px',
				width:'calc(100% - 10px)',
				click_action:"statistic_report_select_column(#{column[:id]},cb_#{column[:name]}_column_value)",
				hover_action:'',
				text:column[:description],
				selected:column[:select]
			}%>
		</div>
	<% end %>
	</div>
</div>

<script>
var check_report_active=0
var statistic_columns_list=<%= raw columns_list.to_json %>
function statistic_report_build_report(){
	if (check_report_active == 0){
		check_report_active=0
		oper_string=''
		login_string=''
		office_string=''
		if (cb_operation_value == 1){
			for (i=0;i<statistic_report_foid_list.length;i++){oper_string+=','+statistic_report_foid_list[i]+','}
		}
		if (cb_user_value == 1){
			for (i=0;i<statistic_report_loginid_list.length;i++){login_string+=','+statistic_report_loginid_list[i]+','}
		}
		if (cb_office_value == 1){
			for (i=0;i<statistic_report_officeid_list.length;i++){office_string+=','+statistic_report_officeid_list[i]+','}
		}
		oper_inverse=cb_oper_inverse_value
		login_inverse=cb_user_inverse_value
		office_inverse=cb_office_inverse_value
		group_oper=cb_operation_group_value
		group_user=cb_user_group_value
		group_office=cb_office_group_value
		group_hour=cb_hour_group_value
		group_day=cb_day_group_value
		columns_list=JSON.stringify(statistic_columns_list)
		//$.post('/export/MainExportForm' ,{
		window.open('stat_export_main?'+
			'start_time='+calendar_from_date.date_format()+'&'+
			'end_time='+calendar_to_date.date_format()+'&'+
			'oper_string='+oper_string+'&'+
			'login_string='+login_string+'&'+
			'office_string='+office_string+'&'+
			'oper_inverse='+oper_inverse+'&'+
			'login_inverse='+login_inverse+'&'+
			'office_inverse='+office_inverse+'&'+
			'group_oper='+group_oper+'&'+
			'group_user='+group_user+'&'+
			'group_office='+group_office+'&'+
			'group_hour='+group_hour+'&'+
			'group_day='+group_day+'&'+
			'columns_list='+columns_list ,'_self')
		//}, function(data) {
		//	console.log(1)
		//	check_report_active=0
		//})
	}
}
function cb_action_operation(){
	if (cb_operation_value == 1){
		document.getElementById('statistic_report_oper_hider').style.height='0px'
	}else{
		document.getElementById('statistic_report_oper_hider').style.height='100%'
	}
}
function cb_action_user(){
	if (cb_user_value == 1){
		document.getElementById('statistic_report_user_hider').style.height='0px'
	}else{
		document.getElementById('statistic_report_user_hider').style.height='100%'
	}
}
function cb_action_office(){
	if (cb_office_value == 1){
		document.getElementById('statistic_report_office_hider').style.height='0px'
	}else{
		document.getElementById('statistic_report_office_hider').style.height='100%'
	}
}

function office_select_search(foid){
		if (foid>0){
			document.getElementById("office_select_field").value=''
			statistic_report_add_element(foid,'office')
			
		}
}
function oper_select_search(foid){
		if (foid>0){
			document.getElementById("oper_select_field").value=''
			statistic_report_add_element(foid,'oper')
		}
}
function login_select_search(foid){
		if (foid>0){
			document.getElementById("login_select_field").value=''
			statistic_report_add_element(foid,'user')
		}
}
var user_list= <%= raw Login.select(:id).login_name('name').to_json  %>
var oper_list= <%= raw Operation.joins(:operation_type).operation_name('oper_name').select(:id,'"operation_types"."name" as type_name').to_json  %>
var office_list= <%= raw Office.select(:name,:id).to_json  %>

var statistic_report_foid_list=[]
var statistic_report_loginid_list=[]
var statistic_report_officeid_list=[]
function statistic_report_add_element(id,type){
		if (type == 'oper'){
			element=cb_oper_inverse_value
			for(i=0;i<oper_list.length;i++){
				if (id == oper_list[i].id){
					name=oper_list[i].oper_name+' ('+oper_list[i].type_name+')'
					statistic_report_foid_list.push(id)
				}
			}
		}else if (type == 'user'){
			element=cb_user_inverse_value
			for(i=0;i<user_list.length;i++){
				if (id == user_list[i].id){
					name=user_list[i].name
					statistic_report_loginid_list.push(id)
				}
			}
		}else{
			element=cb_office_inverse_value
			for(i=0;i<office_list.length;i++){
				if (id == office_list[i].id){
					name=office_list[i].name
					statistic_report_officeid_list.push(id)
				}
			}
		}
		if (element == 1){color='<%= $red %>'}else{color='<%= $blue %>'}
		$('<tr onclick="statistic_report_remove_element('+id+','+"'"+type+"'"+')" id="statistic_report_element_in_table_'+type+'_'+id+'"><td class="statistic_report_element_in_table_'+type+'" style="cursor:pointer;color:'+color+';border-bottom:1px solid <%= $gray %>">'+name+'</td></tr>').appendTo($('#statistic_report_table_with_'+type));
}
function statistic_report_remove_element(id,type){
		document.getElementById('statistic_report_element_in_table_'+type+'_'+id).remove()
		position_id=0
		if (type == 'oper'){
			for (i=0;i<statistic_report_foid_list.length;i++){if (statistic_report_foid_list[i]==id){position_id=i}}
			statistic_report_foid_list.splice(position_id,1)
		}else if (type == 'user'){
			for (i=0;i<statistic_report_loginid_list.length;i++){if (statistic_report_loginid_list[i]==id){position_id=i}}
			statistic_report_loginid_list.splice(position_id,1)
		}else{
			for (i=0;i<statistic_report_officeid_list.length;i++){if (statistic_report_officeid_list[i]==id){position_id=i}}
			statistic_report_officeid_list.splice(position_id,1)
		}
		console.log(statistic_report_foid_list.length+' '+statistic_report_loginid_list.length+' '+statistic_report_officeid_list.length)
}
function cb_action_oper_inverse(){
	statistic_report_change_color(cb_oper_inverse_value,'oper')
}
function cb_action_user_inverse(){
	statistic_report_change_color(cb_user_inverse_value,'user')
}
function cb_action_office_inverse(){
	statistic_report_change_color(cb_office_inverse_value,'office')
}
function statistic_report_change_color(element,type){
	class_e=document.getElementsByClassName('statistic_report_element_in_table_'+type)
	for (i=0;i<class_e.length;i++){
		if (element == 1){
			class_e[i].style.color='<%= $red %>'
		}else{
			class_e[i].style.color='<%= $blue %>'
		}
	}
}

$(document).ready(
	function() { 
		$("#export_columns_selector_container").niceScroll(scroll_settings);
		$(".stat_export_block_table_container").niceScroll(scroll_settings);
	}
);

function statistic_report_update_columns(){
	if (cb_operation_group_value == 1){
	 	columns=[0,14,8,9,10,7]
		for(i=0;i<columns.length;i++){
			document.getElementById('export_column_container_'+columns[i]).style.display=''
			statistic_columns_list[columns[i]].show=1
		}
	}
	if (cb_user_group_value == 1){
	 	columns=[11,1,14]
		for(i=0;i<columns.length;i++){
			document.getElementById('export_column_container_'+columns[i]).style.display=''
			statistic_columns_list[columns[i]].show=1
		}
	}
	if (cb_office_group_value == 1){
		document.getElementById('export_column_container_11').style.display=''
		document.getElementById('export_column_container_14').style.display=''
		statistic_columns_list[14].show=1
		statistic_columns_list[11].show=1
	}
	if (cb_hour_group_value == 1){
		document.getElementById('export_column_container_12').style.display=''
		document.getElementById('export_column_container_14').style.display=''
		statistic_columns_list[12].show=1
		statistic_columns_list[14].show=1
	}
	if (cb_day_group_value == 1){
		document.getElementById('export_column_container_13').style.display=''
		document.getElementById('export_column_container_14').style.display=''
		statistic_columns_list[13].show=1
		statistic_columns_list[14].show=1
	}
	/////////////////////////////////////////////////
	if (cb_operation_group_value == 0){
		columns=[0,8,9,10,7]
		for(i=0;i<columns.length;i++){
			document.getElementById('export_column_container_'+columns[i]).style.display='none'
			statistic_columns_list[columns[i]].show=0
		}
	}
	if (cb_user_group_value == 0){
		document.getElementById('export_column_container_1').style.display='none'
		statistic_columns_list[1].show=0
	}
	if (cb_hour_group_value == 0){
		document.getElementById('export_column_container_12').style.display='none'
		statistic_columns_list[12].show=0
	}
	if (cb_day_group_value == 0){
		document.getElementById('export_column_container_13').style.display='none'
		statistic_columns_list[13].show=0
	}
	if (cb_user_group_value == 0 && cb_office_group_value == 0){
		document.getElementById('export_column_container_11').style.display='none'
		statistic_columns_list[11].show=0
	}
	if (cb_operation_group_value == 0 && cb_user_group_value == 0 && cb_hour_group_value == 0 && cb_day_group_value == 0 && cb_office_group_value == 0){
	 	columns=[12,13,14]
		for(i=0;i<columns.length;i++){
			document.getElementById('export_column_container_'+columns[i]).style.display='none'
			statistic_columns_list[columns[i]].show=0
		}
		for (i=0;i<12;i++){
			document.getElementById('export_column_container_'+i).style.display=''
			statistic_columns_list[i].show=1
		}
	}else{
		document.getElementById('export_column_container_2').style.display='none'
		document.getElementById('export_column_container_3').style.display='none'
		statistic_columns_list[2].show=0
		statistic_columns_list[3].show=0
	}
	
	
}

function statistic_report_select_column(id,value){
	statistic_columns_list[id].select=value
	console.log(statistic_columns_list[id].select)
}
</script>