include mixins
include mixins_email

mixin do_group(elem)
	+email_do_group(elem)
		
mixin do_person(elem)
	+email_do_person(elem)

.subroot(style="height: 100%;")
	.new_flex_zero(style="padding-left: 15px; padding-right: 15px;")
		p.pentryedit_subheadline Geben Sie die eMail-Adressen an. Sie können dazu aus der Pfarrdatenbank eine Person oder eine Gruppe wählen!

		form(class="form-horizontal", id="form-parishdbpersongroupselector")
			
			fieldset(id="filedset_to")
				legend(style="font-size: 120%; margin-bottom: 0px;") Empfänger
				#destination_to_parent
					if dest_to.length > 0
						table(width='100%', class='table table-condensed', style='margin-bottom: 10px;', id="destination_to")
							each empf in dest_to
								tr(data-addresses="#{empf.addresses}", data-recipient-name="#{empf.recipient_name}")
									td(class="the_email_recipient", width="90%") !{empf.recipient}
									td 
										span(	class="glyphicon glyphicon-trash", 
												aria-hidden="true", 
												style="color: red; padding-right: 10px; padding-left: 10px; cursor: pointer;" 
												data-toggle="modal", 
												data-target="#confirmDelete", 
												data-delete-type="to", data-delete-name="#{empf.recipient_name}")

					else
						p <i>Keine Empfänger in dieser Kategorie angegeben.</i>
			
			- var display_fieldset_cc = (dest_cc.length > 0 ? "" : "display: none;");
			fieldset(style=display_fieldset_cc, id="fieldset_cc")
				legend(style="font-size: 120%; margin-bottom: 0px;") Kopie
				#destination_cc_parent
					if dest_cc.length > 0
						table(width='100%', class='table table-condensed', style='margin-bottom: 10px;', id="destination_cc")
							each empf in dest_cc
								tr(data-addresses="#{empf.addresses}", data-recipient-name="#{empf.recipient_name}")
									td(class="the_email_recipient", width="90%") !{empf.recipient}
									td 
										span(	class="glyphicon glyphicon-trash", 
												aria-hidden="true", 
												style="color: red; padding-right: 10px; padding-left: 10px; cursor: pointer;" 
												data-toggle="modal", 
												data-target="#confirmDelete", 
												data-delete-type="cc", data-delete-name="#{empf.recipient_name}")
					else
						p <i>Keine Empfänger in dieser Kategorie angegeben.</i>
						
			- var display_fieldset_bcc = (dest_bcc.length > 0 ? "" : "display: none;");
			fieldset(style="margin-bottom: 10px;" + display_fieldset_bcc, id="fieldset_bcc")
				legend(style="font-size: 120%; margin-bottom: 0px;") Blindkopie
				#destination_bcc_parent
					if dest_bcc.length > 0
						table(width='100%', class='table table-condensed', style='margin-bottom: 10px;', id="destination_bcc")
							each empf in dest_bcc
								tr(data-addresses="#{empf.addresses}", data-recipient-name="#{empf.recipient_name}")
									td(class="the_email_recipient", width="90%") !{empf.recipient}
									td 
										span(	class="glyphicon glyphicon-trash", 
												aria-hidden="true", 
												style="color: red; padding-right: 10px; padding-left: 10px; cursor: pointer;" 
												data-toggle="modal", 
												data-target="#confirmDelete", 
												data-delete-type="bcc", data-delete-name="#{empf.recipient-name}")

					else
						p <i>Keine Empfänger in dieser Kategorie angegeben.</i>
						
		form(class="form-inline", id="form-emaileditor")
			.form-group
				label(for="emailinput") eMail - direkte Eingabe: &nbsp;
				input(type="email", class="form-control noEnterSubmit", size="65", id="emailinput", placeholder="Gültige eMail-Adresse eingeben, dann Empfangstyp wählen und auf 'Hinzufügen' klicken", required)
			select(class="form-control", id="emailinput_type")
				option(value="to") An
				option(value="cc") Kopie
				option(value="bcc") Blindkopie
			button(type="submit", class="btn btn-default") Hinzufügen
			
		.row
			.col-sm-12 &nbsp;
						
		button(class="btn btn-warning btn-xs", type="button", onclick="history.back();") 
			span(class="glyphicon glyphicon-remove-circle", aria-hidden="true")
			span 
			span Zurück

		button(class="btn btn-success btn-xs", type="button", id="button_next", onclick="go_forward_to(pcalpub_steps.SUMMARY,submit_emailaddresses);") 
			span(class="glyphicon glyphicon-ok", aria-hidden="true")
			span 
			span Weiter
			
		
	.new_flex_one(style="overflow: auto; padding-left: 15px; padding-right: 15px; margin-top: 10px;")
		.parishdbelemspane
			for elem in parishdb
				if elem.type === "person"
					+do_person(elem)
				if elem.type === "group"
					+do_group(elem)
	
	script.
		show_searchbar("adjust_visible_elems(this);");
		
		if(#{dest_to.length} === 0)
			$("#button_next").prop('disabled', true);
			
		$('.noEnterSubmit').keydown(function(e){
			if ( e.which == 13 ) e.preventDefault();
		});
			
		function add_email_address(data) {
			console.log(data);
			var table_selector = "destination_" + data.dest;
			var the_table = $("#"+table_selector);
			console.log(the_table.length);
			
			$("#fieldset_" + data.dest).show();
			
			if(the_table.length === 0) {
				$("#"+table_selector+"_parent").empty();
				$("#"+table_selector+"_parent").append("<table width='100%' class='table table-condensed' style='margin-bottom: 10px;' id='" + table_selector + "'></table>");
				the_table = $("#"+table_selector);
				
				if(data.dest === "to")
					$("#button_next").prop('disabled', false);
			}
			
			if(data.description === "")
				data.description = "[als Grppe]";

			var the_delete = '<span class="glyphicon glyphicon-trash" aria-hidden="true" style="color: red; padding-right: 10px; padding-left: 10px; cursor: pointer;" data-toggle="modal", data-target="#confirmDelete", data-delete-type="' + data.dest + '", data-delete-name="' + data.name + '"></span>';
			the_table.append("<tr data-addresses='" + data.address + "' data-recipient-name='" + data.name + "'><td class='the_email_recipient' width='90%;'><p style='margin-bottom: 0px;'>" + data.name + " (" + data.description + ")</p><p class='parishdbelemspane_elem_kontext', style='font-size: 80%;'>(" + data.kontext + ")</p></td><td>" + the_delete + "</td></tr>");
		}
		
		function add_email_address_group(data, elem_uid) {
			var group_elem = $("#"+elem_uid);
			var selects = group_elem.find('select');
			console.log(selects);
			selects.each(function(index) {
				var selected_option = $(this).find("option:selected");
				var val = selected_option.val();
				console.log(val);
				data.address += ";" + val;
			})
			console.log(data);
			add_email_address(data);
		}
		
		function set_visibility_according_to_filter(substrRegex, elems) {
			var actual_dn = $("#actual_elem_dn").val();
			var actual_group = $("#actual_elem_group").val();
			
			elems.each(function(index) {
				var txt = $(this).data('elemname');
				
				var show = substrRegex.test(txt);

				if(show) {
					$(this).show();
				}
				else {	
					$(this).hide();
				}
			});
		}
		
		function adjust_visible_elems(my_input) {
			console.log(my_input.value);
			
			// regex used to determine if a string contains the substring `q`
			substrRegex = new RegExp(my_input.value, 'i');
			
			set_visibility_according_to_filter(substrRegex, $("[data-parishdb]"));
		}
		
		$('#confirmDelete').on('show.bs.modal', function (e) {
			$etype = $(e.relatedTarget).attr('data-delete-type');
			$ename = $(e.relatedTarget).attr('data-delete-name');
			$(this).find('.modal-body p').text("Sind Sie sicher, dass Sie \"" + $ename + "\" aus der Empfängerliste (" + $etype + ")  löschen wollen?");
			
			// Pass form reference to modal for submission on yes/ok
			var tr = $(e.relatedTarget).closest('tr');
			
			$(this).find('.modal-footer #confirm').data('tr', tr);
			
			$(this).find('.modal-footer #confirm').data('delete-type', $etype);
		});

		$('#confirmDelete').find('.modal-footer #confirm').on('click', function() {
			$etype = $(this).data('delete-type');
			console.log($etype);
			
			var tr = $(this).data('tr');
			
			if(tr.is(":last-child") && tr.is(":first-child")) {
				var table = tr.closest("table");
				var table_id = table.prop('id');
				var t_parent = table.parent();
				table.remove();
				t_parent.append("<p><i>Keine Empfänger in dieser Kategorie angegeben.</i></p>");
				if(table_id === "destination_to")
					$("#button_next").prop('disabled', true);
			}
			else
				tr.remove();
				
			$('#confirmDelete').modal('hide');
		});
		
		$('#form-emaileditor').validator().on('submit', function(e) {
			if( ! e.isDefaultPrevented() )
			{
				console.log(e);
				var dest;
				switch($("#emailinput_type").prop("selectedIndex")) {
					case 1: dest="cc"; break;
					case 2: dest="bcc"; break;
					default: dest="to";
				}
				add_email_address({'dest': dest, 'name': $("#emailinput").val(), 'kontext': '-', 'description' : 'direkt', 'address': $("#emailinput").val()});
				e.preventDefault();
			}
		});
