{! admin/util/minimal-grid !}
{! admin/util/search !}
{! admin/util/dates !}
{! admin/util/twemoji?body=true !}

<form method="get" id="search-form">

<div class="e-row-variable" style="margin-bottom: 20px">
	<div class="e-col-50 e-no-padding">
	{% if User::require_acl ('admin/add') %}
	<a href="/blog/add">{"New Post"}</a>
	&nbsp;|&nbsp;
	<a href="/blog/import">{"Import Posts"}</a>
	{% end %}
	{% if User::require_acl ('settings') %}
	&nbsp;|&nbsp;
	<a href="/blog/settings">{"Settings"}</a>
	&nbsp;|&nbsp;
	<a href="/admin/extended?extends=blog\Post&name=Blog+Posts">{"Custom Fields"}</a>
	{% end %}
	</div>
	<div class="e-col-50 e-no-padding" style="text-align: right">
		<input type="text" name="q" id="search-query" size="40" value="{{q}}" />
		<button>{"Search"}</button>
		{% if q != '' %}
		<button onclick="window.location.href = '/blog/admin'; return false">{"Clear"}</button>
		{% end %}
	</div>
</div>

<div class="e-row-variable infobar">
	<div class="e-col-50 e-no-padding">
		{! navigation/pager?style=results&url=[url]&total=[total]&count=[count]&limit=[limit] !}
	</div>
	<div class="e-col-50 e-no-padding" style="text-align: right">
		{% if strpos ($data->q, 'published:yes') !== false %}
			<strong>{"Published"}</strong>
			&nbsp;|&nbsp;
			<a href="#" class="search-for" data-search="published:no">{"Draft"}</a>
			&nbsp;|&nbsp;
			<a href="#" class="search-for" data-search="published:que">{"Scheduled"}</a>
		{% elseif strpos ($data->q, 'published:no') !== false %}
			<a href="#" class="search-for" data-search="published:yes">{"Published"}</a>
			&nbsp;|&nbsp;
			<strong>{"Draft"}</strong>
			&nbsp;|&nbsp;
			<a href="#" class="search-for" data-search="published:que">{"Scheduled"}</a>
		{% elseif strpos ($data->q, 'published:que') !== false %}
			<a href="#" class="search-for" data-search="published:yes">{"Published"}</a>
			&nbsp;|&nbsp;
			<a href="#" class="search-for" data-search="published:no">{"Draft"}</a>
			&nbsp;|&nbsp;
			<strong>{"Scheduled"}</strong>
		{% else %}
			<a href="#" class="search-for" data-search="published:yes">{"Published"}</a>
			&nbsp;|&nbsp;
			<a href="#" class="search-for" data-search="published:no">{"Draft"}</a>
			&nbsp;|&nbsp;
			<a href="#" class="search-for" data-search="published:que">{"Scheduled"}</a>
		{% end %}
	</div>
</div>

<table width="100%">
	<tr>
		<th width="38%">{"Title"}</th>
		<th width="18%">
			<select name="m" id="search-month">
				<option value="">{"All Dates"}</option>
				{% foreach archives as archive %}
				<option value="{{archive->date}}"
					{% if m === $data->archive->date %}selected{% end %}
				>{{months[$data->archive->month - 1]}} {{archive->year}} ({{archive->total}})</option>
				{% end %}
			</select>
		</th>
		<th width="14%">{"Author"}</th>
		<th width="17%">{"Tags"}</td>
		<th width="13%">&nbsp;</th>
	</tr>
{% foreach posts %}
	<tr>
		<td class="title">
			<a href="/blog/post/{{ loop_value->id }}/{{ loop_value->slug }}">{{ loop_value->title }}</a><br />
		</td>
		<td>
			{{ loop_value->ts|I18n::short_date }} -
			<a href="#" class="search-for" data-search="published:{{ loop_value->published}}">{{ loop_value->published|blog_filter_published }}</a>
		</td>
		<td><a href="#" class="search-for" data-search="author:&quot;{{ loop_value->author}}&quot;">{{ loop_value->author }}</a></td>
		<td class="tags">
			{% foreach loop_value->tags as _tag %}
				<a href="#" class="search-for" data-search="{{_tag}}">{{_tag|ucfirst}}</a>
			{% end %}
		</td>
		<td class="edit">
			{% if loop_value->locked %}
			{"Editing Locked"}
			{% else %}
				<a href="/blog/edit?id={{ loop_value->id }}">{"Edit"}</a>
				{% if User::require_acl ('admin/delete') %}
				|
				<a href="/blog/delete" data-id="{{ loop_value->id }}" onclick="return $.confirm_and_post (this, '{"Are you sure you want to delete this post?"}');">{"Delete"}</a>
				{% end %}
			{% end %}
		</td>
	</tr>
{% end %}
</table>

</form>

{% if total > $data->limit %}
{! navigation/pager?style=numbers&url=[url]&total=[total]&count=[count]&limit=[limit] !}
{% end %}

<style>
.tags a {
	display: inline-block;
	padding-right: 8px;
}
.tags a:last-child {
	padding-right: 0px;
}
</style>

<script>
$(function () {
	$.search_init ({
		form: '#search-form',
		query: '#search-query',
		links: '.search-for'
	});

	$('#search-month').change (function (e) {
		e.preventDefault ();
		
		$('#search-form').submit ();
	});
});
</script>
