<% var e, error, n, notice; %>

<% if (!_.isEmpty(m.errors)) { %>
	<div class="zfab_error_wrap zfab_row"><div class="zfab_row_inner"><div class="messageDisplay zfab_error">
		<% foreach (m.errors as e => error) { %>
			<div class="zfab_error_inner">
				{{zenario.htmlspecialchars(error.message, true)}}
			</div>
		<% } %>
	</div></div></div>
<% } %>	
<% foreach (m.notices as n => notice) { %>
	<div class="{{notice.type|escape}}DisplayRow zfab_row">
		<div class="zfab_row_inner">
			<div class="messageDisplay">
				<div>
					<% if (notice.html) { %>
						{{notice.message}}
					<% } else { %>
						{{zenario.htmlspecialchars(notice.message, true)}}
					<% } %>
				</div>
			</div>
		</div>
	</div>
<% } %>

<% if (m.fields) { %>
	<%
		var f,
			field,
			val,
			ff = m.fields[0],
			fl = m.fields.length,
			lf = m.fields[fl-1],
			pgf = ff._lastVisibleGroupingField,
			lgf = lf._lastVisibleGroupingField,
			rowHasError = false,
			hadKeySymbol = false,
			hadProtectedSymbol = false;
	%>
	
	<% foreach (m.fields as f => field) { %>
		<% if (field.error) { %>
			<% rowHasError = true; %>
			<% break; %>
		<% } %>	
	<% } %>
	
	<% if (ff._lastVisibleGrouping !== ff.grouping) { %>
		<% if (pgf) { %>
			</fieldset>
			<% if (pgf.post_grouping_html) { %>
				{{pgf.post_grouping_html}}
			<% } %>
			<% if (!ff.grouping || m.grouping.start_new_grouping_wrapper) { %>
				</x-zenario-grouping-wrapper>
			<% } %>
		<% } %>
		<% if (ff.grouping) { %>
			<% if (!pgf || m.grouping.start_new_grouping_wrapper) { %>
				<x-zenario-grouping-wrapper
					<% if (m.grouping.grouping_wrapper_is_main_scroll) { %>
						id="{{m.lib.idPrefix()|e}}|||main_scroll|||"
					<% } %>
					class="
						zfab_grouping_wrapper
						{{m.grouping.grouping_wrapper_css_class|escape}}
					"
					style="{{m.grouping.grouping_wrapper_style|escape}}"
				>
					<% if (m.grouping.grouping_wrapper_icon_class) { %>
						<div class="zfab_grouping_wrapper_icon {{m.grouping.grouping_wrapper_icon_class|escape}}">
							<% if (zenario.defined(m.grouping.grouping_wrapper_record_count)) { %>
								<span class="zfab_grouping_wrapper_record_count">{{m.grouping.grouping_wrapper_record_count|escape}}</span>
							<% } %>
						</div>
					<% } %>
			<% } %>
			<% if (m.grouping.pre_grouping_html) { %>
				{{m.grouping.pre_grouping_html}}
			<% } %>
			<fieldset
				class="
					zenario_grouping zenario_grouping__{{ff.grouping|escape}}
					{{m.grouping.grouping_css_class|escape}}
					
					<% if (ff.type == 'radio') { %>
						zenario_grouping_with_input zenario_grouping_with_radio
						
						<% if (!!m.lib.value(ff.id)) { %>
							zenario_grouping_with_input_selected zenario_grouping_with_radio_selected
						<% } %>
					<% } %>
					<% if (ff.type == 'checkbox') { %>
						zenario_grouping_with_input zenario_grouping_with_checkbox
						
						<% if (!_.isEmpty(m.lib.value(ff.id))) { %>
							zenario_grouping_with_input_selected zenario_grouping_with_checkbox_selected
						<% } %>
					<% } %>
				"
				style="{{m.grouping.grouping_style|escape}}"
				onclick="{{m.grouping.grouping_onclick|escape}}"
			>
		<% } %>
	<% } %>
	<% if (ff.legend) { %>
		<legend class="zenario_legend zenario_legend_for__{{ff._id|escape}}"><span>{{ff.legend}}</span></legend>
	<% } %>
	
	<div id="{{m.lib.idPrefix()|escape}}row__{{ff._id|escape}}" style="{{ff.row_style|escape}}" class="
		zfab_row
		zenario_ab_row__{{ff._id|escape}}
		{{rowHasError? 'zenario_row_with_error' : ''}}
		{{ff.error? 'zenario_row_with_error_on_first_field' : ''}}
		{{ff.row_class|escape}}
		{{ff.pressed? 'zenario_row_pressed' : ''}}
		{{ff.type=='checkbox'? 'zenario_single_line' : ''}}
		{{ff.onoff? 'zenario_onoffswitch zenario_onoffswitch_admin' : ''}}
		{{ff._hideOnOpen? 'zenario_hide_row_on_open' : ''}}
		{{ff.indent? 'zenario_indent_level zenario_indent_level_' +  (1*ff.indent) : ''}}
		zenario_row_for_{{ff.snippet? 'snippet' : ff.upload? 'upload' : ff.pick_items? 'pick_items' : ff.type |escape}}
		
		<% if (fl > 1) { %>
			zfab_row_with_multiple_fields zfab_row_with_{{fl}}_fields
		<% } else { %>
			zfab_row_with_1_field
		<% } %>
		
		<% if (ff.pre_field_html || ff.post_field_html) { %>
			zfab_row_with_pre_or_post_field_html
		<% } %>
		
		{{m.lib.classesForRowContents('zfab_row_with_', m.fields)}}
	">
		
		<div class="zfab_row_inner">
			
			<% if (!zenario.engToBoolean(ff.full_width)) { %>
				<% foreach (m.fields as f => field) { %>
					<% if (field.side_note) { %>
						<div class="zenario_side_note_wrap zenario_side_note_wrap_for_field">
							<div class="zenario_side_note">
								<div class="zenario_note_icon"></div>
								<div class="zenario_note_content">{{zenario.unfun(field.side_note)}}</div>
							</div>
						</div>
					<% } %>
				<% } %>
				<% if (ff.label && !zenario.IN(ff.type, 'checkbox', 'radio')) { %>
					<div class="title {{ff._hideOnOpen? 'zenario_hide_on_open' : ''}} {{ff._showOnOpen? 'zenario_show_on_open' : ''}}">
						<label class="{{ff.label_class|escape}}" for="{{ff._id|escape}}">
							{{ff.label|escape}}
						</label>
						
						<% if (ff.pre_field_post_label_html) { %>
							{{ff.pre_field_post_label_html}}
						<% } %>
						
						<% if (ff.encrypted && zenario.IN(ff.type, 'checkboxes', 'radios')) { %>
							<span class="zfab_encrypted_field">🔑 </span>
							<% hadKeySymbol = true; %>
						<% } %>
						
						<% if (ff.site_setting && ff.site_setting.protect_from_database_restore && zenario.IN(ff.type, 'checkboxes', 'radios')) { %>
							<span class="zfab_protected_from_database_restore" title="{{zenarioA.phrase.siteSettingProtected|e}}"> </span>
							<% hadProtectedSymbol = true; %>
						<% } %>
					</div>
				<% } %>
			<% } %>
			
			<div class="
				zfab_row_fields
				{{ff._hideOnOpen? 'zenario_hide_on_open' : ''}}
				{{ff._showOnOpen? 'zenario_show_on_open' : ''}}
			">
				<% foreach (m.fields as f => field) { %>
					
					<% var showEncryptedIcon = !hadKeySymbol && field.encrypted; %>
					<% var showProtectedIcon = !hadProtectedSymbol && field.site_setting && field.site_setting.protect_from_database_restore; %>
					<% var showPhraseIcon = field.show_phrase_icon; %>
					
					{{field._html}}
					
					<% if (showEncryptedIcon) { %>
						<span class="zfab_encrypted_field">🔑 </span>
					<% } %>
			
					<% if (showProtectedIcon) { %>
						<span class="zfab_protected_from_database_restore" title="{{zenarioA.phrase.siteSettingProtected|e}}"> </span>
					<% } %>
			
					<% if (showPhraseIcon) { %>
						<a class="zenario_fab_phrase_icon" title="{{zenarioA.phrase.translatedField}}"></a>
					<% } %>
				<% } %>
			</div>
			<% var nb; %>
			<% foreach (m.fields as f => field) { %>
				<% if (field.note_below && $.trim(nb = zenario.unfun(field.note_below))) { %>
					<% if (field.show_plain_note_below) { %>
						<% var note_below_class = "zenario_note_below_plain"; %>
					<% } else { %>
						<% var note_below_class = "zenario_note_below"; %>
					<% } %>

					<div class="{{note_below_class}}">
						<div class="zenario_note_icon"></div>
						<div class="zenario_note_content">{{nb}}</div>
					</div>
				<% } %>
			<% } %>
			<% foreach (m.fields as f => field) { %>
				<% if (field.error && field.error != true) { %>
					<div class="zfab_inline_error">
						{{zenario.htmlspecialchars(field.error, true)}}
					</div>
				<% } %>	
			<% } %>	
		</div>
	</div>
	
	<% if (ff._isLastRow) { %>
		<% if (ff.grouping) { %>
			</fieldset>
			<% if (lgf && lgf.post_grouping_html) { %>
				{{lgf.post_grouping_html}}
			<% } %>
			</x-zenario-grouping-wrapper>
		<% } %>
	<% } %>
<% } %>