<%  @calendar_view_disable=1   %>

<% 
	$page_calendar_select_oper="display:none;"
	$page_calendar_check_count_of_selected_day_action=0
	$page_calendar_after_action=1
	$page_calendar_without_multiple_days=1

%>

<div id="stat_day_left_container">
	<div id="stat_day_left_container_search">
		<%= render partial: "shared/search_list", locals: {
			select_id:'login',
			select_name:t(:login_search),
			search_command:'login_sort',
			drop_direction:'down',
			delete_function:0
		} %>
	</div>
	<div id="stat_day_left_container_calendar">
		<%= render "shared/calendar" %>
	</div>

</div>

<div id="stat_compare_lines_container">
</div>

<script>
window.onload = function(){
	show_loading(1)
	//document.getElementById('stat_compare_user_year').value=parseInt(calendar_end_time.split('.')[2])
	//document.getElementById('stat_compare_user_month').value=parseInt(calendar_end_time.split('.')[1])
	//stat_compare_user_build_days()
	// document.getElementById('stat_compare_user_day').value=parseInt(calendar_end_time.split('.')[0])
	stat_compare_build_calendar()
	now=new Date()
	document.getElementById('stat_compare_calendar_body').scrollTop=$('#day_selected_'+now.getDate()+'_'+now.getMonth()+'_'+now.getFullYear()).position().top
	$.getJSON('get_day_stat_full_activity' , function(data) {
		for (i=0;i<data.length;i++){
			if (data[i].date !== null){
				dd=parseInt(data[i].date.split('-')[2])
				mm=parseInt(data[i].date.split('-')[1])-1
				yy=parseInt(data[i].date.split('-')[0])
				day_block=document.getElementById('day_selected_'+dd+'_'+mm+'_'+yy)
				if (day_block !== null){
					document.getElementById('day_selected_'+dd+'_'+mm+'_'+yy).innerHTML=dd+'<div style="width:15px;height:2px;bottom:3px;left:5px;position:absolute;background:<%= $blue %>"></div>'
				}
			}
		}
		show_loading(0)
		//document.getElementById('stat_compare_calendar_body').scrollTop=0
		//document.getElementById('stat_compare_calendar_body').scrollTop=$(last_id).position().top
	}).fail(function(error) {
		show_loading(0)
		error_show(error)
 })
}


var stat_compare_selected_loginid=0
function login_select_search(foid){
		if (foid>0){
			stat_compare_selected_loginid=foid
		}
}

var stat_selected_lines=[]
var stat_selected_lines_id=0
var stat_compare_lines_id=0
var stat_compare_abs_count=0

<%
	fields=[]
		fields.push({:left => 25,:field => "count", :width => 'width:150px/width:120px', :name => "#{t(:count_opers)}:"});
		fields.push({:left => 195,:field => "duration", :width => 'width:170px/width:250px', :name => "#{t(:full_time)}:", :time => 1, :invert => 1});
		fields.push({:left => 310,:field => "green", :width => 'width:40px/', :name => '<img height="19" width="19" style="position:relative;top:3px" src="/assets/normal.png" alt="">'})
		fields.push({:left => 45,:field => "opers", :width => 'width:150px/width:120px', :name => "#{t(:unique_opers)}:"});
		fields.push({:left => 215,:field => "sys_time", :width => 'width:170px/width:250px', :name => "#{t(:sys_time)}:", :time => 1, :invert => 1});
		fields.push({:left => 330,:field => "yellow", :width => 'width:40px/', :name => '<img height="20" width="20" style="" src="/assets/warning.png" alt="">', :invert => 1})
		fields.push({:left => 120,:field => "users", :width => 'width:150px/width:120px', :name => "#{t(:login_s)}:"});
		fields.push({:left => 235,:field => "user_time", :width => 'width:170px/width:250px', :name => "#{t(:user_time)}:", :time => 1, :invert => 1});
		fields.push({:left => 350,:field => "red", :width => 'width:40px/', :name => '<img height="20" width="20" style="" src="/assets/error.png" alt="">', :invert => 1})
		
%>	
function stat_compare_get_user_data(d,m,y){
	show_loading(1)
	m=m+1
	if (m<10){m='0'+m}
	if (d<10){d='0'+d}
	selected_date=y+'-'+m+'-'+d
	
	
	$.getJSON('get_day_stat_activity_by_login' ,{date:selected_date, loginid:stat_compare_selected_loginid}, function(data) {
	if (data.length > 0){
		var element ={}
			login_name=''
			element['id']=stat_selected_lines_id
			element['view']=1
			for (i=0;i<user_list.length;i++){
				if (user_list[i].id == stat_compare_selected_loginid){
					element['name']=user_list[i].name
					
					login_name=user_list[i].name
				}	
			}
			if (stat_compare_selected_loginid == 0){
				login_name=''
			}
			element['login_id']=stat_compare_selected_loginid
			<%  fields.each do |item| %>
				element['<%= item[:field] %>']=data[0].<%= item[:field] %>
			<% end %>
		stat_selected_lines.push(element)
		star_display=''
		stat_compare_abs_count++
		if (stat_compare_abs_count > 1){star_display='display:none;';;block_size="39px"}else{block_size="145px";stat_compare_lines_id=stat_selected_lines_id}
		$('<div id="stat_compare_block_id_'+stat_selected_lines_id+'"   style="transition:0.6s;width:100%;position:relative;height:'+block_size+'">'+
			'<div  style="width:calc(100% - 10px);height:calc(100% - 10px);top:5px;left:5px;position:absolute;border:1px solid <%= $gray %>;overflow:hidden">'+
				'<div onclick="stat_compare_show_hide_day('+stat_selected_lines_id+')" style="position:absolute;left:30px;top:0px;width:calc(100% -  60px);height:30px;cursor:pointer;z-index:10"></div>'+
				
				'<div style="width:100%;height:29px;top:0px;left:0px;position:absolute;border-bottom:1px solid <%= $gray %>;background:<%= $l_gray %>">'+
					'<div style="width:30px;height:30px;position:absolute;right:0px;top:0px;">'+
							'<img height="20" width="20" onclick="stat_compare_days_delete_row('+stat_selected_lines_id+')" style="cursor:pointer;position:absolute;top:5px;right:5px" src="/assets/delete2.png" alt="">'+
					'</div>'+
					'<div class="text_m" align="center" style="position:absolute;right:30px;top:0px;width:150px;height:30px;line-height:30px;border-left:1px solid <%= $gray %>"><b>'+data[0].date+'</b></div>'+
					'<div class="text_m" align="center" style="position:absolute;right:180px;top:0px;width:250px;height:30px;line-height:30px"><b>'+login_name+'</b></div>'+
					'<div align="center" onclick="stat_compare_select_day_to_compare('+stat_selected_lines_id+')" style="position:absolute;left:0px;top:0px;width:30px;height:30px;cursor:pointer;border-right:1px solid <%= $gray %>"></div>'+
					'<img height="20" width="20" id="stat_line_star_'+stat_selected_lines_id+'" style="'+star_display+'position:absolute;top:5px;left:5px;cursor:pointer" src="/assets/star.png" alt="">'+
					'<div style="position:absolute;left:30px;top:0px;width:480px;height:30px;cursor:pointer;">'+
						<%  	
							count_tr_row=0
							fields.each do |item|
								count_tr_row+=1
						 %>
						 	<% if count_tr_row == 1 %>
								'<div style="width:25px;height:30px;position:absolute;top:0px;left:0px">'+
									'<img height="20" width="20"  style="position:absolute;top:5px;right:0px" src="/assets/operations.png" alt="">'+
						 		'</div>'+
						 	<% end %>
						 	<% if count_tr_row == 3 %>
								'<div style="width:55px;height:30px;position:absolute;top:0px;left:65px">'+
									'<img height="20" width="20"  style="position:absolute;top:5px;right:0px" src="/assets/user.png" alt="">'+
						 		'</div>'+
						 	<% end %>
						 	<% if count_tr_row == 4 %>
								'<div style="width:55px;height;position:absolute;top:0px;left:140px">'+
									'<img height="20" width="20"  style="position:absolute;top:5px;right:0px" src="/assets/time.png" alt="">'+
						 		'</div>'+
						 	<% end %>
						 	<% if count_tr_row == 7 %>
								'<div style="width:55px;height:30px;position:absolute;top:0px;left:255px">'+
									'<img height="20" width="20"  style="position:absolute;top:5px;right:0px" src="/assets/perfomance.png" alt="">'+
						 		'</div>'+
						 	<% end %>
							'<div style="width:20px;height:30px;position:absolute;top:0px;left:<%= item[:left] %>px">'+
								'<img height="16" width="16" class="stat_сompare_arrow_class_'+stat_selected_lines_id+'" id="stat_сompare_arrow_<%= item[:field] %>_'+stat_selected_lines_id+'" style="position:absolute;top:7px;left:4px" src="/assets/idle.png" alt="">'+
							'</div>'+
						<% end %>
					'</div>'+
				'</div>'+
				'<table class="table_body" style="width:900px;position:absolute;top:30px;left:0px;line-height:20px">'+
					'<tr>'+
						<% 
						count_tr_row=0
						fields.each do |item|
							count_tr_row+=1
						 %>
							'<td style="<%= item[:width].split('/')[0] %>" align="right" ><%= raw item[:name] %></td>'+
							'<td style=";border-right:0px solid <%= $gray %>;<%= item[:width].split('/')[1] %>"><b>'+<% if item[:time] == 1 %>second_to_hour_converter(<% end %>data[0].<%= item[:field] %><% if item[:time] == 1 %>)<% end %>+'</b><div class="stat_line_all_diff_'+stat_selected_lines_id+'" id="stat_line_<%= item[:field] %>_diff_'+stat_selected_lines_id+'" style="float:right"></div></td>'+
						 <% if count_tr_row % 3 == 0%>
							'</tr>'+
							'<tr>'+
						 <% end %>

						<% end %>
					'</tr>'+
				'</table>'+
			'</div>'+
		'</div>').appendTo($('#stat_compare_lines_container'));
		
		if (stat_selected_lines_id > 0 && stat_compare_abs_count > 1){stat_compare_between_days(stat_selected_lines_id)}
		stat_selected_lines_id++
		show_loading(0)
	}else{
		show_loading(0)
	}
	}).fail(function(error) {
		show_loading(0)
		error_show(error)
 })
}

function stat_compare_between_days(i){
	<% fields.each do |item| %>
		document.getElementById('stat_line_<%= item[:field] %>_diff_'+i).innerHTML='('+<% if item[:time] == 1 %>second_to_hour_converter(<% end %>Math.abs(stat_selected_lines[i].<%= item[:field] %>-stat_selected_lines[stat_compare_lines_id].<%= item[:field] %>)<% if item[:time] == 1 %>)<% end %>+')'
		document.getElementById('stat_line_<%= item[:field] %>_diff_'+i).style.color=stat_selected_lines[i].<%= item[:field] %>-stat_selected_lines[stat_compare_lines_id].<%= item[:field] %> <% if item[:invert] == 1 %> < <% else %> > <% end %> 0 ? '<%= $green %>' : '<%= $red %>' 
		console.log(stat_selected_lines[i].<%= item[:field] %>-stat_selected_lines[stat_compare_lines_id].<%= item[:field] %> <% if item[:invert] == 1 %> < <% else %> > <% end %> 0 ? '<%= $green %>' : '<%= $red %>')
		document.getElementById('stat_сompare_arrow_<%= item[:field] %>_'+i).src=stat_selected_lines[i].<%= item[:field] %>-stat_selected_lines[stat_compare_lines_id].<%= item[:field] %> <% if item[:invert] == 1 %> < <% else %> > <% end %> 0 ? '/assets/green_arrow.png' : '/assets/red_arrow.png' 
		console.log(stat_selected_lines[i].<%= item[:field] %>-stat_selected_lines[stat_compare_lines_id].<%= item[:field] %> <% if item[:invert] == 1 %> < <% else %> > <% end %> 0 ? '/assets/green_arrow.png' : '/assets/red_arrow.png')
	<%
	end
	%>

}

function stat_compare_select_day_to_compare(i){
	if (i !== stat_compare_lines_id){
		document.getElementById('stat_line_star_'+stat_compare_lines_id).style.display='none'
		stat_compare_lines_id=i
		document.getElementById('stat_line_star_'+stat_compare_lines_id).style.display=''
		for (n=0;n<stat_selected_lines.length;n++){
			if (stat_compare_lines_id !== stat_selected_lines[n].id  &&  stat_selected_lines[n].view == 1 ){
				stat_compare_between_days(stat_selected_lines[n].id)
			}
		}
		class_line=document.getElementsByClassName('stat_line_all_diff_'+i)
		for (n=0;n<class_line.length;n++){
			class_line[n].innerHTML=''
		}
		class_line=document.getElementsByClassName('stat_сompare_arrow_class_'+i)
		for (n=0;n<class_line.length;n++){
			class_line[n].src='/assets/idle.png'
		}
		
		
	}
	
}

function stat_compare_show_hide_day(i){
	
	element=document.getElementById('stat_compare_block_id_'+i)
	if (i == 0 && element.value !== 0 && element.value !== 1){element.value = 1}else if (element.value !== 0 && element.value !== 1){element.value = 0}
	if (element.value == 0 ){
		element.style.height = '145px'
		element.value = 1
	}else{
		element.style.height = '39px'
		element.value = 0
	}
}
function page_calendar_action_with_day(d,m,y){
	if (document.getElementById('login_select_field').value == ''){
		stat_compare_selected_loginid=0
	}
	element=document.getElementById('day_selected_'+d+'_'+m+'_'+y)
	if (element.value == 1){
		stat_compare_get_user_data(d,m,y)
		stat_compare_select_day(d,m,y)
	}
	/*	element.style.background='<%= $blue %>'
		element.style.color='<%= $white %>'
		
	}else if (stat_compare_selected_loginid > 0){
		document.getElementById('day_selected_'+d+'_'+m+'_'+y).innerHTML=d+'<div style="width:15px;height:2px;bottom:3px;left:5px;position:absolute;background:<%= $blue %>"></div>'
		stat_compare_get_user_data(d,m,y)
		if (element.value == 1){
			element.style.background=''
			element.style.color=''
		}
	} */
}

function stat_compare_days_delete_row(abs){
	for (i=0;i<stat_selected_lines.length;i++){
		if (stat_selected_lines[i].id == abs && stat_compare_lines_id == abs && stat_compare_abs_count == 1){
			stat_compare_lines_id=0
			stat_selected_lines[i].view=0
		}else if (stat_selected_lines[i].id == abs && stat_compare_lines_id == abs){
			for(x=0;x<stat_selected_lines.length;x++){
				if (stat_compare_lines_id !== stat_selected_lines[x].id && stat_selected_lines[x].view == 1 ){
					stat_compare_select_day_to_compare(stat_selected_lines[x].id)
					stat_compare_lines_id=stat_selected_lines[x].id
					x=stat_selected_lines.length
				}
			}
			stat_selected_lines[i].view=0
		}else if(stat_selected_lines[i].id == abs){
			stat_selected_lines[i].view=0
		}
	}
	$('#stat_compare_block_id_'+abs).remove()
	stat_compare_abs_count--

	
}
$(document).ready(
	function() { 
		$("#stat_compare_lines_container").niceScroll(scroll_settings);
	}
);
var user_list= <%= raw Login.select(:id, :name).to_json  %>
</script>