<xf:if is="$memberStat.isInsert()">
	<xf:title>{{ phrase('add_member_stat') }}</xf:title>
<xf:else />
	<xf:title>{{ phrase('edit_member_stat:') }} {$memberStat.title}</xf:title>
</xf:if>

<xf:pageaction if="$memberStat.isUpdate() AND $memberStat.canEdit()">
	<xf:button href="{{ link('member-stats/delete', $memberStat) }}" icon="delete" overlay="true" />
</xf:pageaction>

<xf:if is="{{ !$memberStat.canEdit() }}">
	<div class="blockMessage blockMessage--important blockMessage--iconic">
		{{ phrase('only_limited_fields_in_item_may_be_edited') }}
	</div>
</xf:if>

<xf:form action="{{ link('member-stats/save', $memberStat) }}" ajax="true" class="block">
	<div class="block-container">
		<div class="block-body">
			<xf:textboxrow name="title" value="{$memberStat}"
				readonly="{{ !$memberStat.canEdit() }}"
				label="{{ phrase('title') }}" />

			<xf:textboxrow name="member_stat_key" value="{$memberStat}"
				readonly="{{ !$memberStat.canEdit() }}"
				label="{{ phrase('member_stat_key') }}" />

			<hr class="formRowSep" />

			<xf:radiorow name="user_criteria" readonly="{{ !$memberStat.canEdit() }}"
				label="{{ phrase('user_criteria') }}">
				<xf:option value="all" selected="!$memberStat.criteria"
					label="{{ phrase('all_valid_users') }}" />
				<xf:option value="searcher" selected="$memberStat.criteria"
					label="{{ phrase('searcher_criteria') }}"
					data-xf-init="disabler"
					data-container=".js-hiderContainer"
					data-hide="yes" />
			</xf:radiorow>

			<div class="js-hiderContainer">
				<hr class="formRowSep" />

				<xf:include template="helper_user_search_criteria">
					<xf:set var="$noSpecificUser" value="{{ true }}" />
					<xf:set var="$readOnly" value="{{ !$memberStat.canEdit() }}" />
				</xf:include>

				<hr class="formRowSep" />
			</div>

			<xf:if is="$memberStat.canEdit()">
				<xf:formrow label="{{ phrase('sort') }}" rowtype="input">
					<div class="inputPair">
						<xf:select name="sort_order" value="{$memberStat.sort_order}">
						<xf:options source="{$sortOrders}" />
					</xf:select>

					<xf:select name="sort_direction" value="{$memberStat.sort_direction}">
						<xf:option value="desc">{{ phrase('descending') }}</xf:option>
						<xf:option value="asc">{{ phrase('ascending') }}</xf:option>
					</xf:select>
					</div>
				</xf:formrow>
			<xf:else />
				<xf:formrow label="{{ phrase('sort') }}">
					{$sortOrders.{$memberStat.sort_order}}
					&middot;
					<xf:if is="$memberStat.sort_direction == 'desc'">
						{{ phrase('descending') }}
					<xf:else />
						{{ phrase('ascending') }}
					</xf:if>

					<xf:hiddenval name="sort_order">{$memberStat.sort_order}</xf:hiddenval>
					<xf:hiddenval name="sort_direction">{$memberStat.sort_direction}</xf:hiddenval>
				</xf:formrow>
			</xf:if>

			<xf:formrow rowtype="input"
				label="{{ phrase('php_callback') }}"
				explain="{{ phrase('member_stat_callback_explain') }}">

				<xf:macro id="helper_callback_fields::callback_fields"
					arg-data="{$memberStat}"
					arg-readOnly="{{ !$memberStat.canEdit() }}" />
			</xf:formrow>

			<hr class="formRowSep" />

			<xf:selectrow name="permission_limit" value="{$memberStat}" column="permission_limit_"
				disabled="{{ !$memberStat.canEdit() }}"
				readonly="{{ !$memberStat.canEdit() }}"
				label="{{ phrase('permission_limit') }}">
				<xf:option value="" label="{{ phrase('(none)') }}" />
				<xf:foreach loop="$permissionsData.interfaceGroups" key="$interfaceGroupId" value="$interfaceGroup">
					<xf:optgroup label="{$interfaceGroup.title}">
						<xf:foreach loop="{$permissionsData.permissionsGrouped.{$interfaceGroupId}}" value="{$permission}">
							<xf:option value="{$permission.permission_group_id}-{$permission.permission_id}">
								{$permission.title}
							</xf:option>
						</xf:foreach>
					</xf:optgroup>
				</xf:foreach>

				<xf:explain>{{ phrase('permission_limit_explain') }}</xf:explain>
			</xf:selectrow>

			<xf:formrow rowtype="input"
				label="{{ phrase('visibility_callback') }}"
				explain="{{ phrase('member_stat_visibility_callback_explain') }}">

				<xf:macro id="helper_callback_fields::callback_fields"
					arg-data="{$memberStat}"
					arg-className="visibility_class"
					arg-methodName="visibility_method"
					arg-readOnly="{{ !$memberStat.canEdit() }}" />
			</xf:formrow>

			<xf:checkboxrow label="{{ phrase('options') }}">
				<xf:option name="show_value" selected="$memberStat.show_value"
					readonly="{{ !$memberStat.canEdit() }}"
					label="{{ phrase('show_value') }}"
					hint="{{ phrase('if_enabled_and_provided_value_will_be_displayed_for_each_user') }}" />
				<xf:option name="overview_display" selected="$memberStat.overview_display"
					label="{{ phrase('show_on_overview_tab') }}"
					hint="{{
						phrase('if_enabled_top_5_members_of_this_stat_will_be_displayed_on_overview') . ' '
						. (($xf.development AND $memberStat.addon_id) ? phrase('field_value_maintained_addon_upgrade_explain') : '')
					}}" />
			</xf:checkboxrow>

			<xf:numberboxrow name="user_limit" value="{$memberStat}"
				label="{{ phrase('user_limit') }}"
				explain="{{ ($xf.development AND $memberStat.addon_id) ? phrase('field_value_maintained_addon_upgrade_explain') : '' }}" />

			<xf:macro id="display_order_macros::row"
				arg-value="{$memberStat}"
				arg-explain="{{ ($xf.development AND $memberStat.addon_id) ? phrase('field_value_maintained_addon_upgrade_explain') : '' }}" />

			<xf:checkboxrow readonly="{{ !$memberStat.canEdit() }}">
				<xf:option selected="{$memberStat.cache_lifetime}"
					label="{{ phrase('enable_cache_and_cache_for_x_minutes:') }}">

					<xf:numberbox name="cache_lifetime" value="{{ $memberStat.cache_lifetime ?: 60 }}"
						min="0" readonly="{{ !$memberStat.canEdit() }}" />
				</xf:option>
			</xf:checkboxrow>

			<xf:checkboxrow>
				<xf:option name="active" selected="$memberStat.active"
					label="{{ phrase('member_stat_is_active') }}"
					hint="{{
						phrase('use_this_to_disable_member_stat_from_showing_on_members_page') . ' '
						. (($xf.development AND $memberStat.addon_id) ? phrase('field_value_maintained_addon_upgrade_explain') : '')
					}}" />
			</xf:checkboxrow>

			<xf:macro id="addon_macros::addon_edit" arg-addOnId="{$memberStat.addon_id}" />
		</div>

		<xf:submitrow icon="save" sticky="true" />
	</div>
</xf:form>