include mixins
include mixins_day

doctype html
html
	include header
	script(src='/javascripts/pfiswebsocket.js')
	script(src='/javascripts/runtime.js')
	script(src='/templates/daycontent.js')
	script(src='/templates/errorview_inlay.js')
	link(href="/stylesheets/bootstrap-datepicker.min.css", rel="stylesheet")
	script(src='/javascripts/bootstrap-datepicker.min.js' )
	script(src='/javascripts/bootstrap-datepicker.de.min.js' )
	body
			
		if can_go_back === false
			script.
				history.pushState(null, null, location.href);
				window.onpopstate = function(event) {
					history.go(1);
				};
				
		.modal.fade(id="daypickerDialog", role="dialog", aria-labelledby="daypickerDialogLabel", 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="daypickerDialogLabel") Datum anspringen
					.modal-body
						#datepicker()
					.modal-footer
						button(type="button", class="btn btn-default", data-dismiss="modal") Abbrechen
						button(type="button", class="btn btn-success", id="confirm") Anspringen
						
		+standard_delete_dialog("confirmDelete")

		.root

			.new_flex_zero
				+nav("PfarrInfoSystem", "dropdown_menu")
					+nav_item( "#", "active" ) Tag
					+nav_item( "/timeline/" + date.date) TagZeitleiste
					+nav_item( "/week/" + date.date) Woche
					+nav_item( "/day/"+date.date+"/select_punits_for_view" ) Angezeigte Orte konfigurieren
					+nav_item( "/info" ) Info
					
			#dataparent.subroot.full_height(style="height: 100%; margin-top: 5px;")
				+daycontent(date, litinfo, last_week, yesterday, tomorrow, next_week, parishcal, messages)
				
	script.
		var ws;
		
		$(document).ready(function() {
			console.log('NOW CONNECTING');
			
			ws = new PfISWebSocket("#{sessionuuid}");
			ws.bind_standard_close_handler();
		 			
			ws.bind('get_parishday_result', function(data){
				console.log('day_goto_result');
				
				history.replaceState(null, document.title, "/day/" + data.date.date + "?back=false");
				
				var html = daycontent_template(data);
				$('#dataparent').html(html);
					
				var timeline_elem = $('.navbar-collapse > ul > li:nth(1) > a');
				timeline_elem.prop('href', '/timeline/' + data.date.date);
				var week_elem = $('.navbar-collapse > ul > li:nth(2) > a');
				week_elem.prop('href', '/week/' + data.date.date);
				console.log(week_elem);
			});
			
			ws.bind('pentry_delete_result', function(data) {
				console.log("pentry_delete_result", data);
				
				if(data.result === 'OK') {
					var button = $('[data-pentry-uuid="'+ data.uuid + '"]');
					var type = data.pentrytype;
					var box = button.closest('.' + type);
					box.remove();
				}
				
				var messages = errorview_inlay_template({ messages: data.messages, is_error: (data.result === 'OK' ? false : true), session_ended: false});
					
				$('#daycontrol_parent').append(messages);
					
				$('#confirmDelete').modal('hide');
			});
		});
		
		function day_goto(new_date) {
			console.log(new_date);
			ws.send('get_parishday', {format : "day", date : new_date});
		}
		
		$('#datepicker').datepicker({format: "yyyy-mm-dd", language: "de", calendarWeeks: true, todayHighlight: true});
			
		$('#daypickerDialog').on('show.bs.modal', function (e) {
			date = $(e.relatedTarget).attr('data-date');
			$('#datepicker').datepicker("update", date);
		});
		
		$('#daypickerDialog').find('.modal-footer #confirm').on('click', function() {
				$('#daypickerDialog').modal('hide');
				console.log('CLICKED');
				day_goto($("#datepicker").datepicker('getFormattedDate'));
		});
		
		function toggle_pentrydetails_visibility(pentry) {
			$(pentry).find(".pentry_details").toggle();
		}
		
		$('#confirmDelete').on('show.bs.modal', function (e) {
			$etitle = $(e.relatedTarget).attr('data-pentry-title');
			$(this).find('.modal-body p').text("Sind Sie sicher, dass Sie " + $etitle + " löschen wollen?");
		
			$(this).find('.modal-footer #confirm').data('button', $(e.relatedTarget));
		});
		
		$('#confirmDelete').find('.modal-footer #confirm').on('click', function() {
				var delete_button = $(this).data('button');
				
				var date = delete_button.data('date');
				var type = delete_button.data('pentry-type');
				var uuid = delete_button.data('pentry-uuid');
				
				console.log(date, type, uuid);
				
				ws.send('pentry_delete', { date: date, pentrytype: type, uuid: uuid });
		});

		
