include mixins
include mixins_email

doctype html
html
	include header
	link(href="/stylesheets/bootstrap-datepicker.min.css", rel="stylesheet")
	script(src='/javascripts/bootstrap-datepicker.min.js' )
	script(src='/javascripts/bootstrap-datepicker.de.min.js' )
	script(src='/javascripts/tinymce/tinymce.min.js' )
	script(src="/javascripts/validator.min.js")
	script(src='/socket.io/socket.io.js')
	script(src='/siofu/client.js')
	body
		
		+standard_delete_dialog("confirmDelete")
						
		.modal.fade(id="sendEmail", role="dialog", aria-labelledby="sendEmailLabel", aria-hidden="true")
			.modal-dialog(role="document")
				.modal-content
					.modal-header
						h4(class="modal-title", id="sendEmailLabel") eMail versenden
					.modal-body
						p(style="font-weight: bold; font-style: italic;") Bitte warten Sie, während die eMail versendet wird ...
						#sendEmail_message
						#sendEmail_explanation
					.modal-footer
						button#sendEmail_closebutton(type="button", class="btn btn-default", data-dismiss="modal", style="display: none;") Schließen
						
		#emailaddresschooser.sidewideoverlay(style="z-index: 5;")
			.new_flex_zero
				+nav_with_form("PfarrInfoSystem", "PfIS", "entryeditnav")
				p.pentryedit_headline eMail-Empfänger auswählen
				
			.new_flex_one(style="margin-bottom: 15px; padding-left: 15px; padding-right: 15px; overflow: auto;")
				+email_directinput
				.parishdbelemspane(style="margin-top: 5px;")
					for elem in parishdb
						if elem.type === "person"
							+email_do_person(elem)
						if elem.type === "group"
							+email_do_group(elem)
		
		.root

			.new_flex_zero
				+nav("PfarrInfoSystem", "dropdown_menu")
				p.pentryedit_headline eMail versenden 
					button.btn.btn-success.btn-xs#sendbutton(style="margin-left: 15px; margin-right: 5px;", data-toggle="modal" data-target="#sendEmail") Senden
					span.glyphicon.glyphicon-info-sign#sendbutton_info(aria-hidden="true",  data-toggle="tooltip" data-placement="bottom" title="Sie müssen mindestens einen Empfänger sowie den Betreff angeben!")
					
				
			.new_flex_one(style="margin-bottom: 15px; padding-left: 15px; padding-right: 15px; overflow: auto;")
				+emaildestinationview([], [], [], true, "$('#emailinput').val('peter@halieus.de'); $('#emailaddresschooser').show();")
				fieldset(id="fieldset_subject")
					legend(style="font-size: 120%; margin-bottom: 0px;") Betreff
					//- form#form-subject
					//-	.form-group
					input(type="text", id="subject", class="control-label col-xs-12", oninput="update_sendbutton();")
				fieldset(id="fieldset_body")
					legend(style="font-size: 120%; margin-bottom: 0px;") Nachricht
					textarea#bodyeditor
			
			
		script.
			/*$('#form-subject').validator().on('submit', function(e) {
				if( ! e.isDefaultPrevented() )
				{
					console.log(e);
					e.preventDefault();
				}
			});*/
			
			$("#sendbutton").prop('disabled', 'true');
			
			$(function () {
				$('[data-toggle="tooltip"]').tooltip();
			})
			
			tinymce.editors=[];
			tinymce.init({
				selector: '#bodyeditor',
				resize: false,
				language: 'de',
				plugins: 'advlist,autolink,autoresize,lists,link,image,charmap,print,preview,anchor,searchreplace,visualblocks,code,insertdatetime,media,table,contextmenu,paste',
				toolbar: 'undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image',
				autoresize_bottom_margin: 5,
				autoresize_min_height: 150
				});
			
			
			$("#entryeditnav-form").on('submit', function(e){
				console.log('FILTER SUBMIT');
				e.preventDefault();
			});
			
			function show_searchbar(functiontocall) {
				$("#entryeditnav-form").append("<div class='input-group'><div class='input-group-addon'><span class='glyphicon glyphicon-search'></span></div><input class='form-control' type='text' placeholder='Suche'  style='display: inline;' oninput='" + functiontocall + "'></input></div>");
			}
			show_searchbar("adjust_visible_elems(this);"); // can always be on!!!
			
			var __cache_to_is_filled = false;
			function update_sendbutton(to_is_filled) {
				if(typeof to_is_filled !== 'undefined')
					__cache_to_is_filled = to_is_filled;
				console.log('update_sendbutton: ', to_is_filled, __cache_to_is_filled);
				if(__cache_to_is_filled && $('#subject').val() != '') {
					$('#sendbutton').prop('disabled', false);
					$('#sendbutton_info').hide();
				}
				else {
					$('#sendbutton').prop('disabled', true);
					$('#sendbutton_info').show();
				}
			}
			
			var full = location.protocol+'//'+location.hostname+(location.port ? ':'+location.port: '');
			console.log(full);

			var sockets = io.connect(full+'/email');
			var siofu = new SocketIOFileUpload(sockets);
	
			var is_sending = false;
	
			$('#sendEmail').on('show.bs.modal', function (e) {
				is_sending = true;
			
				$("#sendEmail_message").html("");
				$("#sendEmail_explanation").html("");
				$("#sendEmail_closebutton").hide();
				
				var data = {
						dest_to : get_emailaddresses_from_table("#destination_to"),
						dest_cc : get_emailaddresses_from_table("#destination_cc"),
						dest_bcc : get_emailaddresses_from_table("#destination_bcc"),
						subject : $("#subject").val(),
						body : tinymce.get('bodyeditor').getContent(),
						body_raw: tinymce.get('bodyeditor').getContent({format : 'text'})
					};
					
				sockets.emit('email_send', data);
			});
			
			$("#sendEmail").on('hidden.bs.modal',  function(e) {
				is_sending = false;
				$("#button_summary_back").show();
				$("#button_summary_send").show();
			});
			
			sockets.on('email_send_result', function(result) {
				console.log('RESULT', result);
				$("#sendEmail_message").html(result.message);
				$("#sendEmail_explanation").html(result.explanation);
				$("#sendEmail_closebutton").show();
			});
			
			/*history.replaceState({step : pcalpub_steps.BACK}, document.title, location.pathname);
			history.pushState({step : pcalpub_steps.SETTINGS}, 'PfIS: Terminveröffentlichung (Einstellungen)', location.pathname);*/
			
			window.addEventListener("popstate", function(e) {
			
				$("#entryeditnav-form").empty();
			
				if(e.state !== null) {
					console.log(e.state);
					if(e.state.step === pcalpub_steps.BACK) {
						console.log(location.pathname);
						window.location.href = location.pathname;
					}
					if(e.state.step === pcalpub_steps.SETTINGS)
						sockets.emit('pcalpub_show_settings', {});
					else if(e.state.step === pcalpub_steps.EDITOR) {
						if(current_step < pcalpub_steps.EDITOR)
							pcalpub_send_html_per_email();
						else
							sockets.emit('pcalpub_show_html_for_email', {});
					}
					else if(e.state.step === pcalpub_steps.ADDRESSCHOOSER) {
						if(current_step < pcalpub_steps.ADDRESSCHOOSER)
							pcalpub_choose_addresses();
						else if(is_sending === true)
							history.forward(); // we are about to go back from SUMMARY - do not allow that if sending!!!
						else
							sockets.emit('pcalpub_show_addresschooser_from_saved', {});
					}
					else if(e.state.step === pcalpub_steps.SUMMARY && ! is_sending) {
						if(current_step < pcalpub_steps.SUMMARY) {
							console.log("AABB");
							submit_emailaddresses();
						}
						else {
							console.log("WRONG");
							sockets.emit('pcalpub_show_summary_from_saved', {});
							}
					}
				}
			}, false);
			
		+email_scripts("$('#emailaddresschooser').hide();", "update_sendbutton", "confirmDelete")
