<% include ../../shared/header %>
<% include ../../shared/nav %>
<% 
	var  getsortlink= function(field,label){
		var order = (request.query.sort && request.query.sort.search("-") === 0) ? '' : '-' ;
		var orderlink = (request.query.sort) ? 
			'?'+locals.viewhelper.getQueryString(request.query).replace('sort='+request.query.sort,'sort='+order+field) 
			: '?'+locals.viewhelper.getQueryString(request.query)+'&sort='+order+field;
		%>
		<a href="  <%= orderlink %>"><%= label %></a>
		<%
	}
%>
<input type="hidden" name="_csrf" value=<%= locals.token %> >
<form name="items-form" class="_pea-form" method="get" >
	<div class="_pea-container">
		<div class="_pea-row _pea-form ">
			<div class="_pea-col-span6  _pea-text-big-em">
				<a href="/p-admin/item/new" class="_pea-button _pea-color-info">New Item</a>
			</div>
			<div class="_pea-col-span6 _pea-text-right">
					<input type="text" placeholder="search" class="_pea-text-big _pea-col-span10" name="search"/>
					<button type="submit" class="_pea-col-span2 _pea-text-big" >search</button>
			</div>
		</div>
		<% include ../../shared/html_inject_precontent %>
		<div class="_pea-row">
			<div class="_pea-col-span12">
				<div class="_pea-document">
					<table class="_pea-table">
						<thead style="">
							<% editlistingprops = true %>
							<% include _pagination_item %>
						</thead>
						<thead>
							<tr class="_pea-text-left">
								<th><% getsortlink('title','Title') %></th>
								<th>Author</th>
								<th>Categories</th>
								<th>Tags</th>
								<th>Type</th>
								<th style="width:6em"><% getsortlink('createdat','Date') %></th>
								<th style="width:6em"></th>
							</tr>
						</thead>
						<tbody>
							<% if(items.length>0){
									items.forEach(function(item){ %>
										<tr>
											<td> 
												<% if(item.primaryasset && item.primaryasset.fileurl  && item.primaryasset.assettype.match("image")){%>
													<div class="_pea-pull-right _pea-col-span9" style="height:100%;">
														<a href="/p-admin/item/edit/<%= item.name %>" style="position:relative; vertical-align:center;"> <%= item.title %> </a>
													</div>
													<div class="_pea-pull-left _pea-col-span3" style=" overflow:hidden; max-height:5em; padding-right:1em;">
													
														<img src="<%- item.primaryasset.fileurl %>" style="width:100%; height:auto;" />
													</div>
												<%} 
												else{%>

												<div class="_pea-col-span12">
													<a href="/p-admin/item/edit/<%= item.name %>"> <%= item.title %> </a>
												</div>
												<%}%>

											</td>
											<td>
													<%if(item.authors && item.authors.length>0){
														item.authors.forEach(function(author){%>
														<a href="/p-admin/items?filter_authors=<%= author.username %>"><%= author.username %></a>,
														<%});
													}
													else if(item.primaryauthor){%>
													<a href="/p-admin/items?filter_authors=<%= item.primaryauthor.username %>"><%= item.primaryauthor.username %></a>
													<%}%>
											</td>
											<td>
												<%if(item.categories && item.categories.length>0){
													item.categories.forEach(function(category){%>
														<a href="/p-admin/items?filter_categories=<%= category.name %>"><%= category.title %></a>,
													<%});
												}%>
											</td>
											<td>
												<%if(item.tags && item.tags.length>0){
													item.tags.forEach(function(tag){%>
														<a href="/p-admin/items?filter_tags=<%= tag.name %>"><%= tag.title %></a>,
													<%});
												}%>
											</td>
											<td>
												<%if(item.contenttypes && item.contenttypes.length>0){
													item.contenttypes.forEach(function(contenttype){%>
														<a href="/p-admin/items?filter_contenttypes=<%= contenttype.name %>"><%= contenttype.title %></a>,
													<%});
												}%>
											</td>
											<td>
												<small>
													<% var disDate = new Date(item.createdat) %>
													<%- disDate %>
												</small>
											</td>
											<td>
												<a href="/p-admin/item/edit/<%= item.name %>" class="_pea-button _pea-color-inverse"> edit </a>
												<a href="/item/<%= item.name %>" class="_pea-button " target="_blank"> i </a>
												<a data-href="/item/<%= item.name %>/delete" class="_pea-button _pea-color-error _pea-dialog-delete" data-successfunction="backToItemLanding" >x</a>

											</td>
										</tr>
										<%
									});
								}
								else{ %>
									<tr><td colspan="7" class="_pea-text-center">No Items!</td></tr>
								<%} %>
						</tbody>
						<tfoot style="">
							<% editlistingprops = false %>
							<% include _pagination_item %>
						</tfoot>
						<!-- <tfoot>
							<tr class="_pea-text-left">
								<th>Title</th>
								<th>Author</th>
								<th>Categories</th>
								<th>Tags</th>
								<th>Type</th>
								<th>Date</th>
								<th></th>
							</tr>
						</tfoot> -->
					</table>
				</div>
			</div>
		</div>
		<% include ../../shared/html_inject_postcontent %>
	</div>
</form>
<script type="text/javascript">
	window.backToItemLanding = function () {
		window.location = '/p-admin/items';
	};
</script>
<% include ../../shared/footer %>
