<% implement('chimera/editor/index_wrapper') %>
<%
is_sortable = false;
show_sortable_handles = false;

fields.reset();

while (fields.hasNext()) {
	field = fields.next().value;

	if (field.options.make_sortable) {
		is_sortable = true;
		show_sortable_handles = true;
		break;
	}
}

pagination_config = Pagination.getConfig();

if (pagination_config.sorting || pagination_config.filtering) {
	show_sortable_handles = false;
}
%>

<% start('index_title_row') %>
	<% if (is_sortable && show_sortable_handles) { %>
		<th></th>
	<% } %>

	<%
	fields.reset();
	while (fields.hasNext()) {
		field = fields.next().value;

		if (field.options.hide) {
			continue;
		}
	%>
		<th>
			<% print(Pagination.sort({field: field.path, content: field.fieldType.title})); %>
		</th>
	<%
	}
	%>
	<th></th>
<% end('index_title_row') %>

<% start('index_filter_row') %>
	<%

	if (is_sortable && show_sortable_handles) { %>
		<th></th>
	<% }

	fields.reset();
	while (fields.hasNext()) {
		field = fields.next().value;

		if (field.options.hide) {
			continue;
		}

		%> <th> <%
		print(Pagination.filter({
			field       : field.path,
			content     : field.fieldType.title,
			className   : 'form-control',
			attributes  : {
				placeholder : 'Filter…'
			}
		}));
		%> </th> <%
	}
	%>
	<th></th>
<% end('index_filter_row') %>

<% start('index_tbody') %>
	<%
		iterator = new Iterator(records);
		while (iterator.hasNext()) {
			item = iterator.next();
			record = item.value;
			sortable_class = '';

			if (is_sortable && show_sortable_handles) {
				sortable_class = 'sortable-row';
			}

			%>

			<tr
				class="model-list-record js-he-link <%= sortable_class %>"
				data-href="<%= Router.routeUrl('RecordAction', {controller: 'editor', subject: modelName, action: 'edit', id: record.id}) %>"
				data-id="<%= record.id %>"
				data-modelname="<%= modelName %>"
			>

			<%

			if (is_sortable && show_sortable_handles) { %>
				<td class="drag-handle js-he-unlink">
					<i class="fas fa-grip-vertical"></i>
				</td>
			<% }

			for (i = 0; i < record.fields.length; i++) {

				if (record.fields[i].field.options.hide) {
					continue;
				}

				%> <td> <%
				Chimera.printField(record.fields[i]);
				%> </td> <%
			}

			%> <td class="js-he-unlink"> <%
			Chimera.printActions('record', {}, record);
			%> </td> <%

			%>
			</tr>
			<%
		}
	%>
<% end('index_tbody') %>