<xf:if is="$apiKey.isInsert()">
	<xf:title>{{ phrase('add_api_key') }}</xf:title>
<xf:else />
	<xf:title>{{ phrase('edit_api_key') }}</xf:title>
</xf:if>

<xf:pageaction if="$apiKey.isUpdate()">
	<xf:button href="{{ link('api-keys/delete', $apiKey) }}" icon="delete" overlay="true" />
</xf:pageaction>

<xf:form action="{{ link('api-keys/save', $apiKey) }}" ajax="true" data-force-flash-message="on" class="block">
	<div class="block-container">
		<div class="block-body">
			<xf:if is="$apiKey.isUpdate()">
				<xf:formrow label="{{ phrase('api_key') }}" rowtype="button">
					<code>{$apiKey.api_key_snippet}</code>
					<xf:button href="{{ link('api-keys/view-key', $apiKey) }}"
						data-xf-click="overlay"
						class="button--link">{{ phrase('view_full_key') }}
					</xf:button>

					<xf:button href="{{ link('api-keys/regenerate', $apiKey) }}"
						data-xf-click="overlay"
						class="button--link">{{ phrase('regenerate_key') }}
					</xf:button>
				</xf:formrow>
			</xf:if>

			<xf:textboxrow name="title" value="{$apiKey}"
				label="{{ phrase('title') }}">
				<xf:explain>{{ phrase('provide_title_for_key_seen_list_api_keys') }}</xf:explain>
			</xf:textboxrow>

			<xf:if is="$apiKey.isUpdate()">
				<xf:macro id="api_key_macros::key_type_row" arg-apiKey="{$apiKey}" />

				<xf:formrow label="{{ phrase('created') }}">
					<xf:date time="{$apiKey.creation_date}" />
					&middot;
					{{ phrase('by_user_x', {'name': $apiKey.Creator.username ?: phrase('n_a') }) }}
				</xf:formrow>

				<xf:formrow label="{{ phrase('last_used') }}">
					<xf:if is="$apiKey.last_use_date">
						<xf:date time="{$apiKey.last_use_date}" />
					<xf:else />
						{{ phrase('n_a') }}
					</xf:if>
				</xf:formrow>
			<xf:else />
				<xf:radiorow name="key_type" value="guest" label="{{ phrase('key_type') }}">
					<xf:option value="guest">{{ phrase('guest_key') }}</xf:option>
					<xf:option label="{{ phrase('user_key') }}" value="user"
						data-xf-init="disabler">
						<xf:textbox name="username" ac="single"
							value="{{ $apiKey.key_type == 'user' ? $apiKey.User.username : '' }}"
							maxlength="{{ max_length($xf.visitor, 'username') }}" />
						<xf:afterhint>{{ phrase('enter_username_key_authenticate_as') }}</xf:afterhint>
					</xf:option>
					<xf:option value="super">{{ phrase('super_user_key') }}</xf:option>
					<xf:explain>{{ phrase('cannot_be_changed_after_creation_requires_new_api_key') }}</xf:explain>
				</xf:radiorow>
			</xf:if>

			<xf:radiorow label="{{ phrase('allowed_scopes') }}" name="allow_all_scopes"
				value="{$apiKey.allow_all_scopes}">
				<xf:option value="0" label="{{ phrase('selected_scopes_only') }}:">
					<xf:checkbox>
						<xf:foreach loop="$scopes" value="$scope">
							<xf:option name="scopes[]"
								value="{$scope.api_scope_id}"
								checked="{$apiKey.scopes.{$scope.api_scope_id}}"
								label="{$scope.api_scope_id}"
								hint="{$scope.description}" />
						</xf:foreach>
					</xf:checkbox>
				</xf:option>
				<xf:option value="1" label="{{ phrase('all_scopes') }}"/>
				<xf:explain>{{ phrase('api_scopes_explain') }}</xf:explain>
			</xf:radiorow>

			<hr class="formRowSep" />

			<xf:checkboxrow>
				<xf:option name="active" selected="$apiKey.active"
					label="{{ phrase('api_key_is_active') }}"
					hint="{{ phrase('use_this_to_disable_api_key') }}"/>
			</xf:checkboxrow>
		</div>
		<xf:submitrow icon="save" sticky="true" />
	</div>
</xf:form>