<%  @calendar_view_disable=1    %>
<div style="width:300px;height:30px;left:10px;top:10px;position:absolute;z-index:2">
	<%=
	render partial: "shared/search_list", locals: {
				select_id:'oper',
				select_name:t(:oper_search),
				search_command:'operation_sort',
				drop_direction:'down',
				delete_function:0 }
	%>
</div>
<div id="kpi_global_kpi_container" style="width:315px;position:absolute;height:50px;right:0px;top:0px">
	<div id="kpi_global_kpi_field" style="width:200px;height:30px;top:10px;left:0px;position:absolute"></div>
	<div class="text_b" style="width:200px;height:30px;top:10px;left:-210px;position:absolute;line-height:30px;font-size:18px" align="right"><%= t('.common_kpi') %></div>
	<div class="button" onclick="kpi_update_in_db()" style="width:150px;right:10px;top:10px"><%= t(:save) %></div>
</div>
<div style="width:100%;height:calc(100% - 50px);position:absolute;top:50px;left:0px" >
	<%
		#	gt - Глобальная таблица
			update_kpi="kpi_local_update('+id+','+unique+')"
			@container=""
			@gt_structure=[]
						@gt_structure.push({:onclick => "", :row_name => '', :value => "'+oper_system_source(data.data[i])+'", :width => "width:60px", :filter => 'none' })
						@gt_structure.push({:onclick => "", :row_name => t(:opers), :value => "'+kpi_print_oper_name(data.data[i].name,data.data[i].id)+'", :width => "", :filter => 'name' })
						@gt_structure.push({:onclick => "", :row_name => t(:avg_time), :value => "'+kpi_set_times_field(data.data[i].time_avg,data.data[i].id,1)+'", :width => "width:180px", :filter => 'time_avg' })
						@gt_structure.push({:onclick => "", :row_name => t(:avg_user_time), :value => "'+kpi_set_times_field(data.data[i].user_time_avg,data.data[i].id,2)+'", :width => "width:180px", :filter => 'user_time_avg' })
						@gt_structure.push({:onclick => "", :row_name => t(:avg_sys_time), :value => "'+kpi_set_times_field(data.data[i].sys_time_avg,data.data[i].id,3)+'", :width => "width:200px", :filter => 'sys_time_avg' })
						@gt_structure.push({:onclick => "", :row_name => t(:count), :value => "'+kpi_set_count_field(data.data[i].count_avg,data.data[i].id,4,3)+'", :width => "width:120px", :filter => 'avg_time' })
						@gt_structure.push({:onclick => "", :row_name => t(:kpi), :value => "'+kpi_set_kpi_field(data.data[i].kpi,data.data[i].id,5,data.data[i].time_avg)+'", :width => "width:230px", :filter => 'kpi' })
						@gt_structure.push({:onclick => "", :row_name => '', :value =>"<div onclick=\"kpi_set_global_kpi('+data.data[i].id+')\" id=\"kpi_row_global_'+data.data[i].id+'\" style=\"cursor:pointer;font-size:12px;color:#{$blue};display:'+(data.data[i].kpi_global == 0 ? 'block' : 'none')+'\" align=\"center\">#{t('.select_common')}</div>", :width => "width:70px", :filter => 'none' })
	%>
	<%= render partial: "shared/table", locals: {
		name:"kpi_table",
		header:@gt_structure,
		query:"settings_get_kpi_list",
		action_after:1,
		time:1,
		add_params:',operation_id:kpi_selected_foid',
		offset:50,
		first_sort:'name;ASC',
		additional_field:@container
	} %>
</div>
<script>
	var kpi_active_data=[]
	var kpi_absolute = 0
	var kpi_selected_foid = 0
	var calendar_from_date=new Date()
	var calendar_to_date=new Date()
	
window.onload = function(){
	
	gt_build_table_kpi_table(gt_last_sort_kpi_table)
	document.getElementById('kpi_global_kpi_field').innerHTML=kpi_set_count_field(0,0,6,4)
}
function gt_action_after_build_kpi_table(data){
	kpi_absolute = data.global_kpi
	document.getElementById('count_0_6').value=Math.round((kpi_absolute-1)*100)
	if (kpi_active_data.length == 0){kpi_active_data = data.data;return true}
	for (i=0;i<data.data.length;i++){
		check=0
		for (n=0;n<kpi_active_data.length;n++){
			if (data.data[i].id == kpi_active_data[n].id){
				check=1
			}
		}
		if (check==0){kpi_active_data.push(data.data[i])}
	}
	
}

function oper_select_search(foid){
		kpi_selected_foid=foid
		gt_last_page_kpi_table=1
		gt_build_table_kpi_table(gt_last_sort_kpi_table)
}


function kpi_set_times_field(seconds,id,unique){
	<%
	fields = [
		{:name => t(:js_hour), :value => 'hours', :type => 1},
		{:name => t(:js_minute), :value => 'minutes', :type => 2},
		{:name => t(:js_second), :value => 'seconds', :type => 2}
	]
	
	%>
	hours=Math.floor(seconds/3600)
	minutes=Math.floor((seconds - hours*3600)/60)
	seconds=seconds - hours*3600 - minutes*60
	result='<div style="width:165px;height:30px;position:relative" onmouseover="kpi_show_field_arrows('+id+','+unique+')" onmouseleave="kpi_hide_field_arrows('+id+','+unique+')">'+
			<% fields.each do |field| %>
			'<input id="<%= field[:value] %>_'+id+'_'+unique+'" class="text_field" onchange="kpi_check_value_in_fields(<%= field[:type] %>,this);<%= raw update_kpi %>" style="width:20px;float:left;position:relative;cursor:pointer" value="'+<%= field[:value] %>.add_zero()+'" >'+
			'<div class="kpi_field_arrows_'+id+'_'+unique+'" style="width:12px;height:30px;float:left;position:relative;line-height:30px;display:none">'+
				'<%= image_tag( "up_small.png", onclick: "kpi_check_add_remove_value_from_field(document.getElementById('+\"'\"+\"#{field[:value]}_\"+id+'_'+unique+\"'\"+'),1,#{field[:type]});#{update_kpi}".html_safe, style:"position:absolute;top:6px;width:8px;height:8px;left:0px;cursor:pointer") %>'+
				'<%= image_tag( "down_small.png", onclick: "kpi_check_add_remove_value_from_field(document.getElementById('+\"'\"+\"#{field[:value]}_\"+id+'_'+unique+\"'\"+'),-1,#{field[:type]});#{update_kpi}".html_safe, style:"position:absolute;bottom:6px;width:8px;height:8px;left:0px;cursor:pointer") %>'+
			'</div>'+
			'<div style="width:15px;height:30px;float:left;position:relative;line-height:30px">'+
				'<%= field[:name] %>'+
			'</div>'+
			
			<% end %>
		'</div>'
	
	
	return result
}

function kpi_set_count_field(count,id,unique,type){
	result='<div style="width:165px;height:30px;position:relative" onmouseover="kpi_show_field_arrows('+id+','+unique+')" onmouseleave="kpi_hide_field_arrows('+id+','+unique+')">'+
			'<input id="count_'+id+'_'+unique+'" class="text_field" onchange="kpi_check_value_in_fields('+type+',this);<%= raw update_kpi %>" style="width:'+(type == 3 ? 60 : 30)+'px;float:left;position:relative;cursor:pointer" value="'+count+'" >'+
			'<div class="kpi_field_arrows_'+id+'_'+unique+'" style="width:12px;height:30px;float:left;position:relative;line-height:30px;display:none">'+
				'<%= image_tag( "up_small.png", onclick: "kpi_check_add_remove_value_from_field(document.getElementById('+\"'\"+'count_'+id+'_'+unique+\"'\"+'),1,'+type+');#{update_kpi}".html_safe, style:"position:absolute;top:6px;width:8px;height:8px;left:0px;cursor:pointer") %>'+
				'<%= image_tag( "down_small.png", onclick: "kpi_check_add_remove_value_from_field(document.getElementById('+\"'\"+'count_'+id+'_'+unique+\"'\"+'),-1,'+type+');#{update_kpi}".html_safe, style:"position:absolute;bottom:6px;width:8px;height:8px;left:0px;cursor:pointer") %>'+
			'</div>'+
			'<div style="width:15px;height:30px;float:left;position:relative;line-height:30px;color:<%= $d_blue %>">'+
				(type == 4 ? '%' : '')+''+
			'</div>'+
		'</div>'
	return result
}



function kpi_set_kpi_field(kpi,id,unique,avg_duration){
	percent=Math.round((kpi - 1) * 100)
	seconds=avg_duration*kpi
	hours=Math.floor(seconds/3600)
	minutes=Math.floor((seconds - hours*3600)/60)
	seconds=seconds - hours*3600 - minutes*60
	kpi_opacity=1
	kpi_hide='none'
	<% update_kpi_time = ";kpi_update_kpi_format_field(1,'+id+','+unique+','+avg_duration+')" %>
	<% update_kpi_percent = ";kpi_update_kpi_format_field(2,'+id+','+unique+','+avg_duration+')" %>
	result='<div style="width:230px;height:30px;position:relative"">'+
			'<div id="kpi_field_'+id+'" style="width:100%;height:100%;position:absolute;opacity:'+kpi_opacity+'" onmouseover="kpi_show_field_arrows('+id+','+unique+')" onmouseleave="kpi_hide_field_arrows('+id+','+unique+')">'+
			'<input id="percent_'+id+'_'+unique+'" class="text_field" onchange="kpi_check_value_in_fields(3,this)<%=  raw update_kpi_time %>;" style="width:30px;float:left;position:relative;cursor:pointer" value="'+percent+'" >'+
			'<div class="kpi_field_arrows_'+id+'_'+unique+'" style="width:12px;height:30px;float:left;position:relative;line-height:30px;display:none">'+
				'<%= image_tag( "up_small.png", onclick: "kpi_check_add_remove_value_from_field(document.getElementById('+\"'\"+'percent_'+id+'_'+unique+\"'\"+'),1,4)#{update_kpi_time}".html_safe, style:"position:absolute;top:6px;width:8px;height:8px;left:0px;cursor:pointer") %>'+
				'<%= image_tag( "down_small.png", onclick: "kpi_check_add_remove_value_from_field(document.getElementById('+\"'\"+'percent_'+id+'_'+unique+\"'\"+'),-1,4)#{update_kpi_time}".html_safe, style:"position:absolute;bottom:6px;width:8px;height:8px;left:0px;cursor:pointer") %>'+
			'</div>'+
			'<div style="width:30px;height:30px;float:left;position:relative;line-height:30px">'+
				'% <b>|</b>'+
			'</div>'+
			<% fields.each do |field| %>
			'<input id="<%= field[:value] %>_'+id+'_'+unique+'" class="text_field" onchange="kpi_check_value_in_fields(<%= raw field[:type] %>,this)<%= raw  update_kpi_percent %>" style="width:20px;float:left;position:relative;cursor:pointer" value="'+<%= field[:value] %>.add_zero()+'" >'+
			'<div class="kpi_field_arrows_'+id+'_'+unique+'" style="width:12px;height:30px;float:left;position:relative;line-height:30px;display:none">'+
				'<%= image_tag( "up_small.png", onclick: "kpi_check_add_remove_value_from_field(document.getElementById('+\"'\"+'#{field[:value]}_'+id+'_'+unique+\"'\"+'),1,#{field[:type]})#{update_kpi_percent}".html_safe, style:"position:absolute;top:6px;width:8px;height:8px;left:0px;cursor:pointer") %>'+
				'<%= image_tag( "down_small.png", onclick: "kpi_check_add_remove_value_from_field(document.getElementById('+\"'\"+'#{field[:value]}_'+id+'_'+unique+\"'\"+'),-1,#{field[:type]})#{update_kpi_percent}".html_safe, style:"position:absolute;bottom:6px;width:8px;height:8px;left:0px;cursor:pointer") %>'+
			'</div>'+
			'<div style="width:15px;height:30px;float:left;position:relative;line-height:30px">'+
				'<%= field[:name] %>'+
			'</div>'+

			<% end %>
			'</div>'+
			'<div id="kpi_field_'+id+'_hider" style="position:absolute;width:100%;height:100%;display:'+kpi_hide+'"></div>'+
		'</div>'
	return result
}


function kpi_check_add_remove_value_from_field(field,value,type){
	value=parseInt(field.value)+value
	if (value < 0){value = 0}
	field.value=value
	
	kpi_check_value_in_fields(type,field)
}

function kpi_check_value_in_fields(type,field){
	value=parseInt(field.value.replace(/[^0-9.]/g, ""))
	if (isNaN(value) || value == undefined){value = 0}
	if (type == 1){	
		if (value < 0 ){value = 0}
		if (value > 99 ){value = 99}	
	}else if(type == 2){
		if (value < 0 ){value = 0}
		if (value > 59 ){value = 59}
	}else if(type == 3){
		if (value < 0 ){value = 0}
		field.value = value
		return true
	}if(type == 4){
		if (value < 0 ){value = 0}
		if (value > 999 ){value = 999}
		field.value = value
		return true
	}
	field.value = value.add_zero()
}
function kpi_show_field_arrows(id,unique){
	elements = document.getElementsByClassName("kpi_field_arrows_"+id+"_"+unique)
	for(i=0;i<elements.length;i++){
		elements[i].style.display=''
	}
}
function kpi_hide_field_arrows(id,unique){
	elements = document.getElementsByClassName("kpi_field_arrows_"+id+"_"+unique)
	for(i=0;i<elements.length;i++){
		elements[i].style.display='none'
	}
}


function kpi_update_kpi_format_field(direction,id,unique,avg_time){
	for (i=0;i<kpi_active_data.length;i++){
		if (id ==  kpi_active_data[i].id){
			avg_time=kpi_active_data[i].time_avg
		}
	}
	percent_f=document.getElementById('percent_'+id+'_'+unique)
	hours_f=document.getElementById('hours_'+id+'_'+unique)
	minutes_f=document.getElementById('minutes_'+id+'_'+unique)
	seconds_f=document.getElementById('seconds_'+id+'_'+unique)
	if (direction == 1){
		new_avg=Math.round((percent_f.value / 100 + 1) * avg_time)
		hours=Math.floor(new_avg/3600)
		minutes=Math.floor((new_avg - hours*3600)/60)
		seconds=new_avg- hours*3600 - minutes*60
	
		hours_f.value=hours.add_zero()
		minutes_f.value=minutes.add_zero()
		seconds_f.value=seconds.add_zero()
	}else{
		new_percent=(((parseInt(hours_f.value) * 3600 + parseInt(minutes_f.value) * 60 + parseInt(seconds_f.value)) / avg_time) - 1) * 100
		if (new_percent > 999){
			new_percent = 999
			percent_f.value=Math.round(new_percent)
			kpi_update_kpi_format_field(1,id,unique,avg_time)	
		}else{
			percent_f.value=Math.round(new_percent)
		}
	}
	kpi_local_update(id,unique)
}


function kpi_local_update(id,unique){
	if (unique < 4){
		value = parseInt(document.getElementById('hours_'+id+'_'+unique).value) * 3600 + parseInt(document.getElementById('minutes_'+id+'_'+unique).value) * 60 + parseInt(document.getElementById('seconds_'+id+'_'+unique).value)
	}else if (unique == 4){
		value = document.getElementById('count_'+id+'_'+unique).value
	}else if (unique == 5){
		value = document.getElementById('percent_'+id+'_'+unique).value / 100 + 1
	}else if (unique == 6){
		kpi_absolute=(document.getElementById('count_0_6').value/100)+1
		kpi_update_all_default_kpi()
	}
	for (i=0;i<kpi_active_data.length;i++){
		if (id == kpi_active_data[i].id){
			row=kpi_active_data[i]
			console.log(unique)
			if (unique == 1){
				row.time_avg=value
				kpi_update_kpi_format_field(1,id,5,value)
			}else if(unique == 2){
				row.user_time_avg=value
			}else if(unique == 3){
				row.sys_time_avg=value
			}else if(unique == 4){
				row.count_avg=value
			}else if(unique == 5){
				console.log(value == kpi_absolute)
				if ( value == kpi_absolute ){
					row.kpi_global = 1
					document.getElementById('kpi_row_global_'+id).style.display='none'
				}else{
					row.kpi_global = 0
					document.getElementById('kpi_row_global_'+id).style.display=''
				}
				row.kpi=value
			}
		}
	}
}

function kpi_set_global_kpi(id){
	document.getElementById('percent_'+id+'_5').value =Math.round((kpi_absolute - 1) * 100)
	for (i=0;i<kpi_active_data.length;i++){
		if (id ==  kpi_active_data[i].id){
			row=kpi_active_data[i]
			row.kpi_global = 1
		}
	}
	kpi_local_update(id,5)
	kpi_update_kpi_format_field(1,id,5)
}

function kpi_update_all_default_kpi(){
	for (x=0;x<kpi_active_data.length;x++){
		if (1 ==  kpi_active_data[x].kpi_global){
			id=kpi_active_data[x].id
			document.getElementById('percent_'+id+'_5').value =Math.round((kpi_absolute - 1) * 100)
			kpi_update_kpi_format_field(1,id,5)
		}
	}
}

function kpi_update_in_db(){
	show_loading(1)
	update_array=[]
	for (x=0;x<kpi_active_data.length;x++){
		element={
			'id':kpi_active_data[x].id,
			'time_avg':kpi_active_data[x].time_avg,
			'user_time_avg':kpi_active_data[x].user_time_avg,
			'sys_time_avg':kpi_active_data[x].sys_time_avg,
			'count_avg':kpi_active_data[x].count_avg,
			'kpi':kpi_active_data[x].kpi
		}
		update_array.push(element)
		
	}
	update_array=JSON.stringify(update_array)
	$.post('settings_update_kpi',{values: update_array},function(){
		show_loading(0)
	})
}

function kpi_change_oper_name(id){
	window.open('operations_info/'+id+'/edit','_self')
}

function kpi_print_oper_name(oper_name,id){
	return '<%= image_tag( "edit_db.png", style: "float:left;width:20px;height:20px;padding-right:10px;cursor:pointer", onclick: "kpi_change_oper_name('+id+')".html_safe) %><div style="float:left;max-width:calc(100% - 30px)">'+oper_name+'</div>'
}


</script>