- var parishes_string = JSON.stringify(data.parishes);
- var _date = new Date(); var _dd = _date.getDate(); if(_dd < 10) _dd = '0' + _dd; var _mm = _date.getMonth()+1; if(_mm < 10) _mm= '0' + _mm;
- var date = _date.getFullYear() + "-" + _mm + "-" + _dd;

include mixins
include mixins_pcalpub
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/validator.min.js")
	script(src='/javascripts/pfiswebsocket.js')
	script(src="/javascripts/validator.min.js")
	script(src="/javascripts/typeahead.jquery.min.js")
	link(href="/stylesheets/mytypeahead.css", rel="stylesheet")
	body
		.root
			.new_flex_zero
				+nav("PfarrInfoSystem", "mainmenu")
				.container-fluid
					h1 Eine Patenurkunde erstellen
			.new_flex_one(style="margin-bottom: 10px;")
				.container-fluid
					form(class="form-horizontal", role="form", id="form-patenurkunde", style="margin-top: 20px;")
						.form-group
							label(for='combo_parish', class="col-sm-2 control-label") Taufpfarrei
							.col-sm-10
								select(id='combo_parish', class="form-control", onchange="update_churches();")
									- var id=0;
									- each parish in data.parishes
										option(value="#{id}") #{parish.name}
										- id++;	
										
						.form-group
							label(for='combo_church', class="col-sm-2 control-label") Taufkirche
							.col-sm-10
								select(id='combo_church', class="form-control")
									- var id=0;
									- each church in data.parishes[0].churches
										option(value="#{id}") #{church}
										- id++;

						.form-group
							label(for='dpicker', class="col-sm-2 control-label") Taufdatum
							.col-sm-10
								//- http://eternicode.github.io/bootstrap-datepicker/
								#datepicker(data-date=date)
									
						.form-group
							label(for='baptistname', class="col-sm-2 control-label") Taufspender
							.col-sm-10
								input(type="text", id="baptistname", class="form-control", required)
								.help-block.with-errors
									
						.form-group
							label(for='childname', class="col-sm-2 control-label") Name des Taufkindes
							.col-sm-10
								input(type="text", id="childname", class="form-control", required)
								.help-block.with-errors
									
						.form-group
							label(for='patename', class="col-sm-2 control-label") Name des Paten
							.col-sm-10
								input(type="text", id="patename", class="form-control", required)
								.help-block.with-errors
									
						button(class="btn btn-success btn-sm", type="submit", id="submit_button") 
							span(class="glyphicon glyphicon-ok", aria-hidden="true")
							span 
							span Urkunde erstellen
							
		script.
			
			function activate_datepicker() {
				$('#datepicker').datepicker({format: "yyyy-mm-dd", language: "de", calendarWeeks: true, todayHighlight: true});
				//$("#datepicker").datepicker('setDate', new Date());
				//$("#datepicker").datepicker('update');
				console.log($("#datepicker").datepicker('getFormattedDate'));
			}
			activate_datepicker();
			
			var parishes = !{parishes_string};
			
			function update_churches() {
				var optionSelected = $("#combo_parish").prop('selectedIndex');
				
				$("#combo_church").empty();
				
				for(idx = 0; idx < parishes[optionSelected].churches.length; ++idx) {
					var church = parishes[optionSelected].churches[idx];
					$("#combo_church").append($("<option></option>").text(church));
				}
			}
			
			var substringMatcher = function(strs) {
				return function findMatches(q, cb) {
					var matches, substringRegex;

					// an array that will be populated with substring matches
					matches = [];

					// regex used to determine if a string contains the substring `q`
					substrRegex = new RegExp(q, 'i');

					// iterate through the pool of strings and for any string that
					// contains the substring `q`, add it to the `matches` array
					$.each(strs, function(i, str) {
						if (substrRegex.test(str)) {
							matches.push(str);
						}
					});

					cb(matches);
				};
			};

			var baptists = ["Pfarrer Peter Fischer", "Kaplan Andreas Stahl", "Kaplan Markus Grasser", "Pfarradministrator P. Dr. Peter Uzor"];
									
			$("#baptistname").typeahead({
				hint: true,
				highlight: true,
				minLength: 0
			},
			{
				name: 'baptists',
				source: substringMatcher(baptists)
			});
			
			var ws = new PfISWebSocket("#{sessionuuid}", "#{taskuuid}");
			ws.bind_standard_close_handler();
			
			$('#form-patenurkunde').validator().on('submit', function(e) {
				if( ! e.isDefaultPrevented() )
				{
					e.preventDefault();
					
					console.log('GENERATE URKUNDE');
					
					var data = {
						parishid : $("#combo_parish").prop('selectedIndex'),
						churchid : $("#combo_church").prop('selectedIndex'),
						date     : $("#datepicker").datepicker('getFormattedDate'),
						baptistname : $("#baptistname").val(),
						childname : $("#childname").val(),
						patename :  $("#patename").val()
					};
					
					console.log(data);
					
					ws.send('generate_urkunde', data);
				}
			});
			
			ws.bind('generate_urkunde_result', function(data) {
				console.log(data.result);
				if(data.result === 'OK')
				{
					console.log(data.file);
					//window.open("data:application/pdf;base64," + data.data , '_blank');
					var the_new_win = window.open(data.file, '_blank');
					the_new_win.title = "Patenurkunde";
				}
				else
					console.log('ERROR IN generate_urkunde(_result)');
			});
			
