include mixins

//- var evp = JSON.parse(exclude_from_view);
- var evps = [];
- for(var i in exclude_from_view) { if(exclude_from_view.hasOwnProperty(i) && !isNaN(+i)) { evps[+i] = exclude_from_view[i]; } }
- var evps_string = evps.toString();

mixin punit(pu)
	li(class="list-group-item")
		- set_checked = true;
		- evps.some(function(e, i, a) { if(e === pu.dn) set_checked=false;});
		input(type="checkbox", id=pu.dn, checked=set_checked)
		label(for=pu.dn) &nbsp;#{pu.name}
	if pu.subunits.length > 0
		ul(class="list-group" style="margin-left: 12px; margin-bottom: 0px;")
			each sub in pu.subunits
				+punit(sub)
	
doctype html
html
	include header
	body(style="overflow: hidden;")
		
		.root(style="overflow: hidden;")

			.new_flex_zero
				+nav("PfarrInfoSystem", "dropdown_menu")
						
				.container-fluid(style="margin-bottom: 18px;")
					p.
						Wählen Sie aus, welche pastoralen Kontexte in der Ansicht angezeigt werden sollen.
						
					form(class="form-horizontal" method="post", action="/select_punits_for_view")
						input(type="hidden", name="u", value="#{u}")
						input(type="hidden", name="d", value="#{d}")
						input(type="hidden", name="exclude_from_view", id="exclude_from_view", value="#{evps_string}")
						button(class="btn btn-success btn-xs", type="submit") 
							span(class="glyphicon glyphicon-ok", aria-hidden="true")
							span 
							span Übernehmen
		
			.new_flex_one(style="overflow: auto; padding-left: 15px; padding-right: 15px;")
				ul(class="list-group")
					for pu in pastoralunits
						+punit(pu)
					
		script.
			
			// console.log($( "#ekmd\\/kk_sonneberg\\/p_judenbach-heinersdorf" ).prop("checked"));
		
			$( "form" ).submit(function( event ) {
				var cbs = $("[type=checkbox]");
				var excludes = [];
				for(i=0; i<cbs.length; i++) {
					if($(cbs[i]).prop('checked') === false)
					{
						console.log("NOT CHECKED: " + $(cbs[i]).attr('id'));
						excludes.push($(cbs[i]).attr('id'));
					}
				}
				
				$("#exclude_from_view").val(excludes.toString());
				
				/* console.log($("#exclude_from_view").val()); */
			});
