include mixins

- var parent_event_uuid = (pentrytype === "tevent" ? parent_event.uuid : "");
- var pentryfunction = (pentrytype === "tevent" ? "event" : pentrytype);
- var generateUID = function() { return "a"+("000000" + (Math.random()*Math.pow(36,6) << 0).toString(36)).slice(-6) };

doctype html
html
	include header
	script(src='/javascripts/socket.io.js' )
	script(src="/javascripts/jquery.loading.min.js")
	link(href="/stylesheets/jquery.loading.min.css", rel="stylesheet")
	script(src="/javascripts/typeahead.jquery.min.js")
	link(href="/stylesheets/mytypeahead.css", rel="stylesheet")
	script(src="/javascripts/validator.min.js")
	link(href="/stylesheets/style_day.css", rel="stylesheet")
	style.
		.panel { margin-bottom: 5px; }
		.panel-title { cursor: pointer; }
		//- .panel_group_and_unit { border-width: 3px; }
	body
	
		.modal.fade(id="confirmDelete", role="dialog", aria-labelledby="confirmDeleteLabel", aria-hidden="true")
			.modal-dialog(role="document")
				.modal-content
					.modal-header
						button(type="button", class="close", data-dismiss="modal", aria-hidden="true") &times;
						h4(class="modal-title", id="confirmDeleteLabel") Löschen bestätigen
					.modal-body
						p Sind Sie sicher, dass Sie das Ereignis löschen wollen?
					.modal-footer
						button(type="button", class="btn btn-default", data-dismiss="modal") Abbrechen
						button(type="button", class="btn btn-danger", id="confirm") Löschen
						
		
		.root

			.new_flex_zero
				+nav_with_form("PfarrInfoSystem", "PfIS", "entryeditnav")
				.container-fluid
					h1 #{pentrytype_pretty} #{edit_type}
						.small #{date.weekday_short}, #{date.day}. #{date.month} #{date.year}
	
			.subroot(style="height: 100%;")
				.container-fluid
					p Lade Kalendereintrag ...
							
						
		script.
			
			var scroll_top = 0;
			var scroll_top_body = 0;
			
			$('.noEnterSubmit').keypress(function(e){
				if ( e.which == 13 ) e.preventDefault();
			});
			
			$("#entryeditnav-form").on('submit', function(e){
				console.log('FILTER SUBMIT');
				e.preventDefault();
			});
			
			function get_compiled_message(type, headline, message) {
				return '<div class="alert alert-' + type + '" role="alert"><strong>'+ headline +'</strong> ' + message + '</div>';
			};
			
			var sockets = io.connect();
			
			function compile_data_for_socketio(custom) {
				var data = { 
						pentrytype : "#{pentrytype}",
						day: "#{date.date}", 
						pentry: "#{pentry.uuid}",
						pcalview : "#{pcalview}",
						data : custom
					};
					
				if("#{pentrytype}" === "tevent") {
					data.parent_event = "#{parent_event_uuid}";
				}
				
				return data;
			}
			
			function goto_mainpage() {
				$('body').loading();
				console.log("#{pentryfunction}");
				sockets.emit("get_rendered_#{pentryfunction}", compile_data_for_socketio({}));
			}
			
			$(document).ready(function(){
				goto_mainpage();
				console.log('NOW EMITTING');
			});
			
			function set_pastoralunit(data) {
				$('body').loading();
				sockets.emit('pentry_set_pastoralunit', compile_data_for_socketio({punit: data}));
			}
			
			function add_location(data) {
				$('body').loading();
				sockets.emit('pentry_add_location', compile_data_for_socketio({location: data}));
			}
			
			function edit_location(data) {
				$('body').loading();
				sockets.emit('pentry_edit_location', compile_data_for_socketio({location: data}));
			}
			
			function add_involved(data) {
				$('body').loading();
				sockets.emit('pentry_add_involved', compile_data_for_socketio({involved: data}));
			}
			
			function edit_involved(data) {
				$('body').loading();
				sockets.emit('pentry_edit_involved', compile_data_for_socketio({involved: data}));
			}
			
			function add_role(data) {
				$('body').loading();
				sockets.emit('event_add_role', compile_data_for_socketio({role: data}));
			};
			
			function edit_role(data) {
				$('body').loading();
				sockets.emit('event_edit_role', compile_data_for_socketio({role: data}));
			};
			
			sockets.on('get_rendered_pentry_result', function(data) {
					console.log('get_rendered_pentry_result');
					
					history.replaceState({direction: 'back'}, document.title, location.pathname);
					history.pushState({direction: 'mainpage'}, document.title, location.pathname);
					
					$("#entryeditnav-form").empty();
					
					$(".root").children(".subroot").remove();
					$(".root").append(data.html);
					$(".subroot > .new_flex_one").scrollTop(scroll_top);
					$("html, body").scrollTop(scroll_top_body); 
					
					scroll_top = 0;	
					scroll_top_body = 0;
					
					$('body').loading('stop');
				});
			
			$('#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 + "\" als " + $etype + " löschen wollen?");
				
				console.log($etype);
				
				if($etype === "Dienst") {
					var fieldset = $(e.relatedTarget).closest('fieldset');
					$(this).find('.modal-footer #confirm').data('fieldset', fieldset);
				}
				else {
					// 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);
				if($etype === "Dienst") {
					var fieldset = $(this).data('fieldset');
					var uid = fieldset.data('uid');
					sockets.emit('event_remove_role', compile_data_for_socketio({role: uid}));
				}
				else {
					var tr = $(this).data('tr');
					var uid = tr.data('uid');
					
					if($etype === "Ort") {
						sockets.emit('pentry_remove_location', compile_data_for_socketio({location: uid}));
					}
					else {
						var role = tr.data('role');
						sockets.emit('pentry_remove_involved', compile_data_for_socketio({role: role, involved: uid}));
					}
				}
			});
			
			sockets.on('pentry_remove_involved_result', function(data) {
				if(data.result === "OK") {
					var tr = $('#confirmDelete').find('.modal-footer #confirm').data('tr');
					if(tr.is(":last-child") && tr.is(":first-child")) {
						var table = tr.closest("table");
						var t_parent = table.parent();
						table.remove();
						t_parent.append("<p>Keine Beteiligten ausgewählt.</p>");
					}
					else
						tr.remove();
				}
				
				$('#confirmDelete').modal('hide');
				
				$('#messageview_involved_' + data.role).append(data.html);
			});
			
			sockets.on('pentry_remove_location_result', function(data) {
				if(data.result === "OK") {
					var tr = $('#confirmDelete').find('.modal-footer #confirm').data('tr');
					if(tr.is(":last-child") && tr.is(":first-child"))
					{
						var table = tr.closest("table");
						var t_parent = table.parent();
						table.remove();
						t_parent.append("<p>Keine Orte ausgewählt.</p>");
					}
					else
						tr.remove();
				}
				$('#confirmDelete').modal('hide');
				
				$('#messageview_locations').append(data.html);
			});
			
			sockets.on('event_remove_role_result',function(data){
				var fieldset = $('#confirmDelete').find('.modal-footer #confirm').data('fieldset');
				
				var is_last_role = (fieldset.is(":last-child") && fieldset.is(":first-child"));
				var f_parent = fieldset.parent();
				
				fieldset.remove();
				if(is_last_role)
					f_parent.append("<div class='col-sm-12'><p>Keine Dienste angegeben.</p></div>")

				$('#confirmDelete').modal('hide');
			});
			
			function save_tevent() {
				var event = {
					uuid : "#{pentry.uuid}",
					name : $("#edit_name").val(),
					name_secondary: $("#edit_name_secondary").val(),
					visibility: $("#combo_type").val(),
					end : $("#edit_end").val(),
					end_is_vague : $("#check_end_is_vague").prop("checked"),
					description : $("#area_description").val(),
					annotations : $("#area_annotations").val()
				};
				return event;
			}
			
			function save_event() {
				event = save_tevent();
				event.start = $("#edit_begin").val();
				
				return event;
			}
			
			function save_away() {
				var away = {
					uuid : "#{pentry.uuid}",
					reason: $("#edit_reason").val()
				};
				
				return away;
			}
			
			function save_keep_in_mind() {
				var keep_in_mind = {
					uuid : "#{pentry.uuid}",
					name : $("#edit_name").val(),
					description:  $("#area_description").val()
				};
				
				return keep_in_mind;
			}
			
			function save_todo() {
				var todo = {
					uuid : "#{pentry.uuid}",
					name : $("#edit_name").val(),
					description:  $("#area_description").val()
				};
				
				return todo;
			}
			
			function save_vehicle_use() {
				var vehicle_use = {
					uuid : "#{pentry.uuid}",
					name : $("#edit_name").val(),
					description:  $("#area_description").val(),
					vehicle_id: $("#combo_vehicle").val()
				};
				
				return vehicle_use;
			}
			
			function save_and_goto(subpage, subpage_data) {
				$('body').loading();

				var data = {};
				data["#{pentryfunction}"] = save_#{pentrytype}();
				sockets.emit('#{pentryfunction}_save', compile_data_for_socketio(data));
				
				console.log('subpage: ', subpage);
				console.log('subbpage_data: ', subpage_data);
				
				scroll_top = $(".subroot > .new_flex_one").scrollTop();
				scroll_top_body = $("html, body").scrollTop();
				switch(subpage) {
					case "select_pastoralunit":
						sockets.emit('render_pastoralunitselector', compile_data_for_socketio({}));
						break;
					case "add_predefined_location":
						sockets.emit('render_locationselector', compile_data_for_socketio({}));
						break;
					case "add_location":
						sockets.emit('render_locationeditor', compile_data_for_socketio({ type: "add" }));
						break;
					case "edit_location":
						sockets.emit('render_locationeditor', compile_data_for_socketio({ type: "edit", location: subpage_data.location }));
						break;
					case "add_actor":
						sockets.emit('render_actorselector', compile_data_for_socketio(subpage_data));
						break;
					case "add_role":
						sockets.emit('render_roleeditor', compile_data_for_socketio({type: "add"}));
						break;
					case "edit_role":
						sockets.emit('render_roleeditor', compile_data_for_socketio({type: "edit", role: subpage_data.role}));
						break;
					case "add_involved":
						sockets.emit('render_involvededitor', compile_data_for_socketio({type: "add", role: subpage_data.role}));
						break;
					case "edit_involved":
						sockets.emit('render_involvededitor', compile_data_for_socketio({type: "edit", role: subpage_data.role, involved: subpage_data.involved}));
						break;
					case "add_parishdbpersongroup":
						sockets.emit('render_parishdbpersongroupselector', compile_data_for_socketio({role: subpage_data.role}));
						break;
				}
			}
			
			sockets.on('render_locationselector_result', function(data) {
					console.log('got locationselector');
					history.pushState({direction: 'locationselector'}, document.title, location.pathname);
					$(".root").children(".subroot").remove();
					$(".root").append(data.html);
					$("html, body").scrollTop(0);
					$(".root").scrollTop(0);
					$('body').loading('stop');
			});
				
			sockets.on('render_locationeditor_result', function(data) {
					console.log('got locationeditor');
					history.pushState({direction: 'locationeditor'}, document.title, location.pathname);
					$(".root").children(".subroot").remove();
					$(".root").append(data.html);
					$("html, body").scrollTop(0);
					$(".root").scrollTop(0);
					$('body').loading('stop');
			});
				
			sockets.on('render_pastoralunitselector_result', function(data) {
					console.log('got pastoralunitsecelcotr');
					history.pushState({direction: 'pastoralunitselector'}, document.title, location.pathname);
					$(".root").children(".subroot").remove();
					$(".root").append(data.html);
					$("html, body").scrollTop(0);
					$(".root").scrollTop(0);
					$('body').loading('stop');
				});
				
			sockets.on('render_actorselector_result', function(data) {
					console.log('got actorselector');
					history.pushState({direction: 'actorselector'}, document.title, location.pathname);
					$(".root").children(".subroot").remove();
					$(".root").append(data.html);
					$("html, body").scrollTop(0);
					$(".root").scrollTop(0);
					$('body').loading('stop');
				});
				
			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>");
			}
			
			/*function show_submitbutton(id, functioncall, extrahtml) {
				console.log(functioncall);
				$("#entryeditnav-form").append('<button style="display: inline-block;" class="btn btn-success disabled" type="button" id="'+id+'" ' + extrahtml + ' onclick="' + functioncall + '"><span class="glyphicon glyphicon-ok" aria-hidden="true"></span></button>');
			}*/

			sockets.on('render_parishdbpersongroupselector_result', function(data) {
					console.log('got parishdbpersongroupselector');
					history.pushState({direction: 'parishdbpersongroupselector'}, document.title, location.pathname);
					$(".root").children(".subroot").remove();
					$(".root").append(data.html);
					$("html, body").scrollTop(0);
					$(".root").scrollTop(0);
					$('body').loading('stop');
				});
				
			sockets.on('render_involvededitor_result', function(data) {
					console.log('got involvededitor');
					history.pushState({direction: 'involvededitor'}, document.title, location.pathname);
					$(".root").children(".subroot").remove();
					$(".root").append(data.html);
					$("html, body").scrollTop(0);
					$(".root").scrollTop(0);
					$('body').loading('stop');
				});
				
			sockets.on('render_roleeditor_result', function(data) {
					console.log('got roleeditor');
					history.pushState({direction: 'roleeditor'}, document.title, location.pathname);
					$(".root").children(".subroot").remove();
					$(".root").append(data.html);
					$("html, body").scrollTop(0);
					$(".root").scrollTop(0);
					$('body').loading('stop');
				});
			
			// HANDLE SIMPLE BACK
			// history.replaceState({direction: 'back'}, document.title, location.pathname);
			// history.pushState({direction: 'mainpage'}, document.title, location.pathname);
			window.addEventListener("popstate", function(e) {
				console.log(e.state);
				if(e.state !== null) {
					if(e.state.direction === "back") {
						history.replaceState(null, document.title, location.pathname);
						setTimeout(function(){
							location.replace("#{pentry_href}/cancel?pcalview=#{pcalview}");
							},0);
					}
					if(e.state.direction === "mainpage")
						goto_mainpage();
				}
			}, false);
			
