<xf:macro name="page_action" arg-linkPrefix="!">
	<xf:pageaction>
		<div class="buttonGroup">
			<xf:button href="{{ link($linkPrefix . '/add') }}" icon="add">{{ phrase('add_category') }}</xf:button>
			<xf:button href="{{ link($linkPrefix . '/sort') }}" icon="sort" overlay="true" />
		</div>
	</xf:pageaction>
</xf:macro>

<xf:comment><!--
	Note that this is a global macro, so scope is inherited into it. The expectation is that this macro will generally
	be close to the only thing in a template so this makes the macro work similarly to an include.
--></xf:comment>
<xf:macro name="category_list" global="true"
	arg-categoryTree="!"
	arg-filterKey="!"
	arg-linkPrefix="!"
	arg-idKey="category_id"
	arg-parentIdKey="parent_category_id">

	<xf:if is="$categoryTree.countChildren()">
		<div class="block">
			<div class="block-outer">
				<xf:macro template="filter_macros" name="quick_filter" arg-key="{$filterKey}" arg-class="block-outer-opposite" />
			</div>
			<div class="block-container">
				<div class="block-body">
					<xf:datalist>
						<xf:foreach loop="$categoryTree.getFlattened(0)" value="$treeEntry">
							<xf:set var="$category" value="{$treeEntry.record}" />
							<xf:datarow>
								<xf:cell class="dataList-cell--link dataList-cell--main" hash="{$category.{$idKey}}">
									<a href="{{ link($linkPrefix . '/edit', $category) }}">
										<div class="u-depth{$treeEntry.depth}">
											<div class="dataList-mainRow">{$category.title}</div>
										</div>
									</a>
								</xf:cell>
								<xf:if is="$permissionContentType">
									<xf:action
										class="{{ {$customPermissions.{$category.{$idKey}}} ? 'dataList-cell--highlighted' : '' }}"
										href="{{ link($linkPrefix . '/permissions', $category) }}">
										{{ phrase('permissions') }}
									</xf:action>
								</xf:if>
								<xf:popup class="dataList-cell--action u-hideMedium"
									label="{{ phrase('add...') }}">

									<div class="menu" data-menu="menu" aria-hidden="true">
										<div class="menu-content">
											<h3 class="menu-header">{{ phrase('add...') }}</h3>
											<a href="{{ link($linkPrefix . '/add', null, { {$parentIdKey}: $category.{$parentIdKey} }) }}" class="menu-linkRow">{{ phrase('sibling') }}</a>
											<a href="{{ link($linkPrefix . '/add', null, { {$parentIdKey}: $category.{$idKey} }) }}" class="menu-linkRow">{{ phrase('child') }}</a>
										</div>
									</div>
								</xf:popup>
								<xf:delete href="{{ link($linkPrefix . '/delete', $category) }}" />
							</xf:datarow>
						</xf:foreach>
					</xf:datalist>
				</div>
			</div>
		</div>
	<xf:else />
		<div class="blockMessage">{{ phrase('no_items_have_been_created_yet') }}</div>
	</xf:if>
</xf:macro>

<xf:macro name="sortable_form" arg-categoryTree="!" arg-linkPrefix="!">
	<xf:macro template="public:nestable_macros" name="setup" />

	<xf:form action="{{ link($linkPrefix . '/sort') }}" class="block" ajax="true">
		<div class="block-container">
			<div class="block-body">
				<div class="nestable-container" data-xf-init="nestable">
					<xf:macro name="sortable_list" arg-children="{$categoryTree}" />
					<xf:hiddenval name="categories" value="" />
				</div>
			</div>
			<xf:submitrow icon="save" rowtype="simple" />
		</div>
	</xf:form>
</xf:macro>

<xf:macro name="sortable_list" arg-children="!">
	<ol class="nestable-list">
		<xf:foreach loop="$children" key="$id" value="$child">
			<xf:macro name="sortable_list_entry" arg-child="{$child}" arg-children="{$child.children}" />
		</xf:foreach>
	</ol>
</xf:macro>

<xf:macro name="sortable_list_entry" arg-child="!" arg-children="!">
	<li class="nestable-item" data-id="{$child.id}">
		<div class="nestable-handle" aria-label="{{ phrase('drag_handle')|for_attr }}"><xf:fa icon="fa-bars" /></div>
		<div class="nestable-content">{$child.record.title}</div>
		<xf:if is="$child.children is not empty">
			<xf:macro name="sortable_list" arg-children="{$child.children}" />
		</xf:if>
	</li>
</xf:macro>

<xf:macro name="parent_category_select_row"
	arg-category="!"
	arg-categoryTree="!"
	arg-idKey="category_id"
	arg-parentIdKey="parent_category_id">

	<xf:selectrow name="{$parentIdKey}" value="{$category.{$parentIdKey}}"
		label="{{ phrase('parent_category') }}">

		<xf:option value="0">{{ phrase('(none)') }}</xf:option>
		<xf:foreach loop="$categoryTree.getFlattened(0)" value="$treeEntry">
			<xf:option value="{$treeEntry.record.{$idKey}}">{{ repeat('--', $treeEntry.depth) }} {$treeEntry.record.title}</xf:option>
		</xf:foreach>
	</xf:selectrow>
</xf:macro>

<xf:macro name="category_delete_form" arg-linkPrefix="!" arg-category="!">
	<xf:form action="{{ link($linkPrefix . '/delete', $category) }}" class="block" ajax="true">

		<div class="block-container">
			<div class="block-body">
				<xf:inforow rowtype="confirm">
					{{ phrase('please_confirm_that_you_want_to_delete_following:') }}
					<strong><a href="{{ link($linkPrefix . '/edit', $category) }}">{$category.title}</a></strong>
				</xf:inforow>

				<xf:if is="$category.hasChildren()">
					<xf:radiorow name="child_nodes_action">
						<xf:option value="move" selected="true">{{ phrase('move_this_categorys_children_to_its_parent') }}</xf:option>
						<xf:option value="delete">{{ phrase('delete_this_categorys_children') }} </xf:option>
					</xf:radiorow>
				</xf:if>
			</div>
			<xf:submitrow rowtype="{{ !$category.hasChildren() ? 'simple' : '' }}" icon="delete" />
		</div>
		<xf:redirect />

	</xf:form>
</xf:macro>