<%  @calendar_view_disable=1    %>
<div style="width:100%;position:absolute;top:0px;left:0px;height:100%;background:<%= $gray %>;opacity:0.5"></div>

<div style="width:100%;top:0px;left:0px;position:absolute;height:50px;background:<%= $white %>;border-bottom:1px solid <%= $gray %>">
	<div class="text_b" style="width:350px;height:50px;position:absolute;top:0px;left:10px;line-height:50px;" align="left"><%= t('.mail_status')%>: <b id="mail_server_status"></b></div>
	<div class="button" onclick="mail_start_server()" id="mail_start_button" style="background:<%= $gray %>;color:<%= $d_blue  %>;position:absolute;width:200px;top:10px;right:10px"> <%= t('.start_service')%></div>
	<div class="button" onclick="mail_stop_server()" id="mail_stop_button" style="background:<%= $gray %>;color:<%= $d_blue  %>;position:absolute;width:200px;top:10px;right:220px"><%= t('.stop_service')%></div>
</div>

<div style="width:100%;position:absolute;height:calc(100% - 280px);top:279px;left:0px;background:<%= $white %>;border-top:1px solid <%= $gray %>">
	<div class="text_b" style="width:250px;height:30px;position:absolute;top:10px;left:10px;line-height:30px;border-bottom:2px solid <%= $red %>" align="left"><b><%= t('.rules')%></b></div>
	<div id="mail_active_rule_list" class="settings_mail_scroll" style="width:100%;top:50px;left:0px;height:calc(100% - 50px);position:absolute;overflow:scroll;overflow-x:hidden">
	</div>	
</div>
<div style="width:100%;position:relative;height:220px;position:absolute;top:60px;left:0px">	
	<div style="width:calc(100% - 20px);top:9px;height:200px;left:9px;position:absolute;background:<%= $white %>;border:1px solid <%= $gray %>">
		<!-------------------------------------------------------------------->
		<div style="width:298px;height:100%;position:absolute;top:0px;left:0px;border-right:2px solid <%= $gray %>">
			<div class="text_b" style="width:250px;height:30px;position:absolute;top:10px;left:10px;line-height:30px;border-bottom:2px solid <%= $red %>" align="left"><b><%= t('.new_rule')%></b></div>
			<div class="text_m" style="width:80px;height:30px;position:absolute;top:55px;left:10px;line-height:30px" align="right"><%= t('.subscriber')%>:</div>
			<input type="email" id="email_address_to_add" class="text_field" style="width:150px;height:20px;position:absolute;top:60px;left:100px" placeholder="example@company.ru">
			<%= image_tag("add.png", size: "30", style: "position:absolute;top:55px;left:260px;cursor:pointer", onclick: "mail_add_adress()") %>
			<div class="settings_mail_scroll" style="width:240px;top:100px;left:29px;height:99px;position:absolute;border:1px solid <%= $gray %>;border-bottom:0px solid black;overflow:scroll;overflow-x:hidden">
				<table class="table_body" id="settings_mail_email_container" style="width:100%"></table>
			</div>
		</div>
		<!-------------------------------------------------------------------->
		<div style="width:calc((100% - 400px) / 3 - 2px);height:100%;position:absolute;top:0px;left:300px;border-right:2px solid <%= $gray %>">
				<div style="width:100%;height:50px;position:absolute;top:0px;left:0px;z-index:20">
					<%= 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 class="settings_mail_scroll" style="background:<%= $white %>;overflow:scroll;overflow-x:hidden;width:100%;height:calc(100% - 82px);position:absolute;top:50px;left:0px;border-top:1px solid <%= $gray %>;border-bottom:1px solid <%= $gray %>">
					<table id="mail_table_with_user" class="table_body"></table>
				</div>
				<div style="width:calc(100% - 20px);bottom:0px;left:10px;height:30px;position:absolute">
					<%= render partial: "shared/check_box_v2", locals: {
						id_name:'user_all',
						class_name:'',
						style_custom:'',
						top:'0px',
						left:'5px',
						width:'100%',
						click_action:'',
						hover_action:'',
						text:t('.all_login_l')
					}%>
				</div>
		</div>
		<!-------------------------------------------------------------------->
		<div style="width:calc((100% - 400px) / 3 - 2px);height:100%;position:absolute;top:0px;left:calc(300px + (100% - 400px) / 3);border-right:2px solid <%= $gray %>">
				<div style="width:100%;height:50px;position:absolute;top:0px;left:0px;z-index:20">
					<%= 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 class="settings_mail_scroll" style="background:<%= $white %>;overflow:scroll;overflow-x:hidden;width:100%;height:calc(100% - 82px);position:absolute;top:50px;left:0px;border-top:1px solid <%= $gray %>;border-bottom:1px solid <%= $gray %>">
					<table id="mail_table_with_oper" class="table_body"></table>
				</div>
				<div style="width:calc(100% - 20px);bottom:0px;left:10px;height:30px;position:absolute">
					<%= render partial: "shared/check_box_v2", locals: {
						id_name:'oper_all',
						class_name:'',
						style_custom:'',
						top:'0px',
						left:'5px',
						width:'100%',
						click_action:'',
						hover_action:'',
						text:t('.all_oper_l')
					}%>
				</div>
		</div>
		<!-------------------------------------------------------------------->
		<div style="width:calc((100% - 400px) / 3 - 2px);height:100%;position:absolute;top:0px;left:calc(300px + ((100% - 400px) / 3)*2);border-right:2px solid <%= $gray %>">
				<div style="width:100%;height:50px;position:absolute;top:0px;left:0px;z-index:20">
					<%= render partial: "shared/search_list", locals: {
						select_id:'office',
						select_name:t(:office_search),
						search_command:'office_sort',
						drop_direction:'down',
						delete_function:0
					} %>
				</div>
				<div class="settings_mail_scroll" style="background:<%= $white %>;overflow:scroll;overflow-x:hidden;width:100%;height:calc(100% - 82px);position:absolute;top:50px;left:0px;border-top:1px solid <%= $gray %>;border-bottom:1px solid <%= $gray %>">
					<table id="mail_table_with_office" class="table_body"></table>
				</div>
				<div style="width:calc(100% - 20px);bottom:0px;left:10px;height:30px;position:absolute">

					<%= render partial: "shared/check_box_v2", locals: {
						id_name:'office_all',
						class_name:'',
						style_custom:'',
						top:'0px',
						left:'5px',
						width:'100%',
						click_action:'',
						hover_action:'',
						text:t('.all_office_l')
					}%>
				</div>
		</div>
		<!-------------------------------------------------------------------->
		
		<div onclick="mail_add_new_rule()" style="width:80px;position:absolute;right:10px;top:calc(50% - 40px);height:80px;background:<%= $blue %>">
			<%= image_tag("save.png", size: "50", style: "position:absolute;top:calc(50% - 25px);left:15px;cursor:pointer") %>
		</div>
	</div>
</div>

<style>
.table_rows{
	text-align:center;
	width:100%;
}
</style>
<script>
window.onload = function(){
	setInterval( function() { 
		mail_check_status()
	} , 5000)
	mail_update_rule_list()
}
var adress_count=0
var email_lists=[]
function mail_add_adress(){
	adress_count++
	address=document.getElementById('email_address_to_add').value
	if (address.length > 5 && address.indexOf('@') > -1 && address.indexOf('.') > -1){
		document.getElementById('email_address_to_add').style.color=""
		$('<tr class="mail_class_list" id="mail_selected_id_'+adress_count+'">'+
			'<td align="center">'+address+'</td>'+
			'<td style="width:30px" ><img src="/assets/delete2.png" onclick="mail_delete_adress('+adress_count+')" style="width:20px;height:20px;cursor:pointer;position:relative;left:10px"></td>'+
		'</tr>').prependTo('#settings_mail_email_container')
		document.getElementById('email_address_to_add').value=''
		var element={}
			element['id']=adress_count
			element['mail']=address
		email_lists.push(element)
	}else{
		document.getElementById('email_address_to_add').style.color="<%= $red %>"
	}
}
function mail_delete_adress(id){
	$('#mail_selected_id_'+id).remove()
	for (i=0;i<email_lists.length;i++){
		if (email_lists[i].id==id){
			position_id=i
		}
	}
	email_lists.splice(position_id,1)
	console.log(email_lists.length)
}

function office_select_search(foid){
		if (foid>0){
			document.getElementById("office_select_field").value=''
			mail_add_element(foid,'office')
			
		}
}
function oper_select_search(foid){
		if (foid>0){
			document.getElementById("oper_select_field").value=''
			mail_add_element(foid,'oper')
		}
}
function login_select_search(foid){
		if (foid>0){
			document.getElementById("login_select_field").value=''
			mail_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 mail_foid_list=[]
var mail_loginid_list=[]
var mail_officeid_list=[]
function mail_add_element(id,type){
	if (type == 'oper'){
		console.log(oper_list.length)
		for(i=0;i<oper_list.length;i++){
			if (id == oper_list[i].id){
				names=oper_list[i].oper_name+' ('+oper_list[i].type_name+')'
				mail_foid_list.push(id)
			}
		}
	}else if (type == 'user'){
		for(i=0;i<user_list.length;i++){
			if (id == user_list[i].id){
				names=user_list[i].name
				mail_loginid_list.push(id)
			}
		}
	}else{
		for(i=0;i<office_list.length;i++){
			if (id == office_list[i].id){
				names=office_list[i].name
				mail_officeid_list.push(id)
			}
		}
	}
		$('<tr onclick="mail_remove_element('+id+','+"'"+type+"'"+')" id="mail_element_in_table_'+type+'_'+id+'"><td class="statistic_report_element_in_table_'+type+'" style="cursor:pointer;border-bottom:1px solid <%= $gray %>">'+names+'</td></tr>').appendTo($('#mail_table_with_'+type));
}
function mail_remove_element(id,type){
		$('#mail_element_in_table_'+type+'_'+id).remove()
		position_id=0
		if (type == 'oper'){
			for (i=0;i<mail_foid_list.length;i++){if (mail_foid_list[i]==id){position_id=i}}
			mail_foid_list.splice(position_id,1)
		}else if (type == 'user'){
			for (i=0;i<mail_loginid_list.length;i++){if (mail_loginid_list[i]==id){position_id=i}}
			mail_loginid_list.splice(position_id,1)
		}else{
			for (i=0;i<mail_officeid_list.length;i++){if (mail_officeid_list[i]==id){position_id=i}}
			mail_officeid_list.splice(position_id,1)
		}
		console.log(mail_foid_list.length+' '+mail_loginid_list.length+' '+mail_officeid_list.length)
}

function mail_add_new_rule(){
	if (email_lists.length > 0){
		if (cb_oper_all_value == 1){
			operations="&operations=1"
		}else{
			operations="&operations="+JSON.stringify(mail_foid_list)
		}
		if (cb_user_all_value == 1){
			users="&users=1"
		}else{
			users="&users="+JSON.stringify(mail_loginid_list)
		}
		if (cb_office_all_value == 1){
			offices="&offices=1"
		}else{
			offices="&offices="+JSON.stringify(mail_officeid_list)
		}
		$.post("mail_add_new_rule?mails="+JSON.stringify(email_lists)+operations+users+offices, function(data) {
			console.log('Правило добавлено')
			$('.mail_class_list').remove()
			email_lists.length = 0
			mail_update_rule_list()
		})
	}
}
function mail_update_rule_list(){
	$('.mail_rules_list_class').remove()
	$.getJSON('mail_get_rule_list', function(data) {
		for (i=0;i<data.length;i++){
			mails_list=''
			for (mail=0;mail<data[i].mail.length;mail++){
				mails_list+='<tr><td class="table_rows">'+data[i].mail[mail]+'</td></tr>'
			}
			//////////////
			opers_list=''
			if (data[i].operation_id.length == 1 && data[i].operation_id[0] == 0){
				opers_list='<tr><td class="table_rows"><b><%= t('.all_oper') %></b></td></tr>'
			}else{
				for (foid=0;foid<data[i].operation_id.length;foid++){
					opers_list+='<tr><td class="table_rows">'+support_get_name(data[i].operation_id[foid],'oper')+'</td></tr>'
				}
			}
			/////////////
			users_list=''
			if (data[i].login_id.length == 1 && data[i].login_id[0] == 0){
				users_list='<tr><td class="table_rows"><b><%= t('.all_login') %></b></td></tr>'
			}else{
				for (loginid=0;loginid<data[i].login_id.length;loginid++){
					users_list+='<tr><td class="table_rows">'+support_get_name(data[i].login_id[loginid],'user')+'</td></tr>'
				}
			}
			/////////////
			offices_list=''
			if (data[i].office_id.length == 1 && data[i].office_id[0] == 0){
				offices_list='<tr><td class="table_rows"><b><%= t('.all_office') %></b></td></tr>'
			}else{
				for (officeid=0;officeid<data[i].office_id.length;officeid++){
					offices_list+='<tr><td class="table_rows">'+support_get_name(data[i].office_id[officeid],'office')+'</td></tr>'
				}
			}
			/////////////
			<%
			@tables=[]
			@tables.push({:name => t('.subscriber'), :field => "mail"})
			@tables.push({:name => t(:login_s), :field => "user"})
			@tables.push({:name => t(:opers), :field => "oper"})
			@tables.push({:name => t(:office_s), :field => "office"})
			
			%>
			$('<div class="mail_rules_list_class" style="width:50%;height:300px;position:relative;float:left">'+
				'<div style="width:calc(100% - 20px);position:absolute;top:9px;left:9px;height:300px;border:1px solid <%= $gray %>">'+
					'<div class="text_b" style="width:100%;height:40px;position:absolute;top:0px;left:0px;background:<%= $green %>;line-height:40px;color:<%= $white %>;" align="center">Активно'+
						'<img style="position:absolute;top:8px;right:13px;cursor:pointer" onclick="mail_delete_rule('+data[i].id+')" src="/assets/cross_white.png" width="24" height="24">'+
					'</div>'+
					<% @tables.each do |fields|%>
						'<div style="width:25%;height:calc(100% - 40px);position:relative;float:left;top:40px">'+
							'<div class="text_m" style="width:100%;height:39px;position:absolute;top:0px;left:0px;line-height:40px;border-bottom:1px solid <%= $gray %>" align="center"><%= fields[:name] %></div>'+
							'<div class="settings_mail_scroll" style="width:100%;top:40px;position:absolute;left:0px;height:calc(100% - 40px);overflow:scroll;overflow-x:hidden">'+
								'<table class="table_body" style="width:100%">'+<%= fields[:field] %>s_list+'</table>'+
							'</div>'+
						'</div>'+
					<% end %>
				'</div>'+
			'</div>').appendTo('#mail_active_rule_list')
			$(".settings_mail_scroll").niceScroll(scroll_settings);
		}
	}).fail(function(error) {
		show_loading(0)
		error_show(error)
 })
}
function mail_delete_rule(id){
	$.post("mail_delete_rule?id="+id, function(data) {
		console.log('Правило удалено')
		mail_update_rule_list()
	})
	console.log(id)
}
function support_get_name(id,type){
	names=''
	if (type == 'oper'){
		console.log(oper_list.length)
		for(n=0;n<oper_list.length;n++){
			if (id == oper_list[n].id){
				names=oper_list[n].oper_name+' ('+oper_list[n].type_name+')'
				
			}
		}
	}else if (type == 'user'){
		for(n=0;n<user_list.length;n++){
			if (id == user_list[n].id){
				names=user_list[n].name
			}
		}
	}else{
		for(n=0;n<office_list.length;n++){
			if (id == office_list[n].id){
				names=office_list[n].name
			}
		}
	}
	return names
}
var mailer_ip="http://"+location.host+"/"
var mail_status=0
function mail_check_status(){
	/*$.ajax({
	  url: mailer_ip+'mail_check',
	  type: "GET",
	  crossDomain: true,
	}).done(function(){
		console.log(1)
		document.getElementById('mail_server_status').innerHTML='Работает'	
	}).fail(function(){
		console.log(2)
		document.getElementById('mail_server_status').innerHTML='Не работает'	
	})*/
	
	$.getJSON('mail_check_server', function(data) {
		mail_status=1
		document.getElementById('mail_start_button').style.color='<%= $d_blue %>'
		document.getElementById('mail_start_button').style.background='<%= $gray %>'
		document.getElementById('mail_stop_button').style.color=''
		document.getElementById('mail_stop_button').style.background=''
		
		document.getElementById('mail_server_status').innerHTML='<%= t('.work') %>'
		document.getElementById('mail_server_status').style.color='<%= $green %>'
	}).error(function() {
		mail_status=2
		document.getElementById('mail_start_button').style.color=''
		document.getElementById('mail_start_button').style.background=''
		document.getElementById('mail_stop_button').style.color='<%= $d_blue %>'
		document.getElementById('mail_stop_button').style.background='<%= $gray %>'
		
		document.getElementById('mail_server_status').innerHTML='<%= t('.not_work') %>'	
		document.getElementById('mail_server_status').style.color='<%= $red %>'
	}).fail(function(error) {
		show_loading(0)
		//error_show(error)
 })
}
function mail_start_server(){
	if (mail_status == 2){
		mail_status == 0
		$.post("mail_start_server?port=3010", function(data) {
			console.log('Сервер запущен')
		})
	}
}
function mail_stop_server(){
	if (mail_status == 1){
		mail_status == 0
		$.post("mail_stop_server", function(data) {
			console.log('Сервер остановлен')
		})
	}
}

$(document).ready(
	function() { 
		$(".settings_mail_scroll").niceScroll(scroll_settings);
	}
);
</script>