{% macro render_column_header(header_class, header_style, center_text=False, sort_key=None, header_title=None) %}
{% set class_suffix = (search.sort == sort_key) and ("_" + search.order) or "" %}
{% set th_classes = filter_truthy([header_class, sort_key and "sorting" + class_suffix, center_text and "text-center"]) %}
<th {% if th_classes %} class="{{ ' '.join(th_classes) }}"{% endif %} {% if header_title %}title="{{ header_title }}"{% endif %} style="{{ header_style }}">
	{% if sort_key %}
	<a href="{% if class_suffix == '_desc' %}{{ modify_query(s=sort_key, o="asc") }}{% else %}{{ modify_query(s=sort_key, o="desc") }}{% endif %}"></a>
	{% endif %}
	{{ caller() }}
</th>
{% endmacro %}
{% if (use_elastic and torrent_query.hits.total > 0) or (torrent_query.items) %}
<div class="table-responsive">
	<table class="table table-bordered table-hover table-striped torrent-list">
		<thead>
			<tr>
				{% call render_column_header("hdr-category", "width:80px;", center_text=True) %}
					<div>Category</div>
				{% endcall %}
				{% call render_column_header("hdr-name", "width:auto;") %}
					<div>Name</div>
				{% endcall %}
				{% call render_column_header("hdr-link", "width:70px;", center_text=True) %}
					<div>Link</div>
				{% endcall %}
				{% call render_column_header("hdr-size", "width:100px;", center_text=True, sort_key="size") %}
					<div>Size</div>
				{% endcall %}
				{% call render_column_header("hdr-date", "width:140px;", center_text=True, sort_key="id", header_title="In UTC") %}
					<div>Date</div>
				{% endcall %}

				{% if config.ENABLE_SHOW_STATS %}
				{% call render_column_header("hdr-seeders", "width:50px;", center_text=True, sort_key="seeders", header_title="Seeds") %}
					<i class="fa fa-arrow-up" aria-hidden="true"></i>
				{% endcall %}
				{% call render_column_header("hdr-leechers", "width:50px;", center_text=True, sort_key="leechers", header_title="Leeches") %}
					<i class="fa fa-arrow-down" aria-hidden="true"></i>

				{% endcall %}
				{% call render_column_header("hdr-downloads", "width:50px;", center_text=True, sort_key="downloads", header_title="Completed downloads") %}
					<i class="fa fa-check" aria-hidden="true"></i>
				{% endcall %}

				{% endif %}
			</tr>
		</thead>
		<tbody>
			{% set torrents = torrent_query if use_elastic else torrent_query.items %}
			{% for torrent in torrents %}
			<tr class="{% if torrent.deleted %}deleted{% elif torrent.hidden %}warning{% elif torrent.remake %}danger{% elif torrent.trusted %}success{% else %}default{% endif %}">
				{% set cat_id = use_elastic and ((torrent.main_category_id|string) + '_' + (torrent.sub_category_id|string)) or torrent.sub_category.id_as_string %}
				{% set icon_dir = config.SITE_FLAVOR %}
				<td style="padding:0 4px;">
				{% if use_elastic %}
				<a href="/?c={{ cat_id }}" title="{{ category_name(cat_id) }}">
				{% else %}
				<a href="/?c={{ cat_id }}" title="{{ torrent.main_category.name }} - {{ torrent.sub_category.name }}">
				{% endif %}
					<img src="/static/img/icons/{{ icon_dir }}/{{ cat_id }}.png" alt="{{ category_name(cat_id) }}">
				</a>
				</td>
				{% if use_elastic %}
				<td><a href="{{ url_for('view_torrent', torrent_id=torrent.meta.id) }}" title="{{ torrent.display_name | escape }}">{%if "highlight" in torrent.meta %}{{ torrent.meta.highlight.display_name[0] | safe }}{% else %}{{torrent.display_name}}{%endif%}</a></td>
				{% else %}
				<td><a href="{{ url_for('view_torrent', torrent_id=torrent.id) }}" title="{{ torrent.display_name | escape }}">{{ torrent.display_name | escape }}</a></td>
				{% endif %}
				<td style="white-space: nowrap;text-align: center;">
					{% if torrent.has_torrent %}<a href="{{ url_for('download_torrent', torrent_id=torrent.id) }}"><i class="fa fa-fw fa-download"></i></a>{% endif %}
					{% if use_elastic %}
					<a href="{{ create_magnet_from_info(torrent.display_name, torrent.info_hash) }}"><i class="fa fa-fw fa-magnet"></i></a>
					{% else %}
					<a href="{{ torrent.magnet_uri }}"><i class="fa fa-fw fa-magnet"></i></a>
					{% endif %}
				</td>
				<td class="text-center">{{ torrent.filesize | filesizeformat(True) }}</td>
				{% if use_elastic %}
				<td class="text-center" data-timestamp="{{ torrent.created_time | utc_time }}">{{ torrent.created_time | display_time }}</td>
				{% else %}
				<td class="text-center" data-timestamp="{{ torrent.created_utc_timestamp|int }}">{{ torrent.created_time.strftime('%Y-%m-%d %H:%M') }}</td>
				{% endif %}
				
				{% if config.ENABLE_SHOW_STATS %}
				{% if use_elastic %}
				<td class="text-center" style="color: green;">{{ torrent.seed_count }}</td>
				<td class="text-center" style="color: red;">{{ torrent.leech_count }}</td>
				<td class="text-center">{{ torrent.download_count }}</td>
				{% else %}
				<td class="text-center" style="color: green;">{{ torrent.stats.seed_count }}</td>
				<td class="text-center" style="color: red;">{{ torrent.stats.leech_count }}</td>
				<td class="text-center">{{ torrent.stats.download_count }}</td>
				{% endif %}
				{% endif %}
			</tr>
			{% endfor %}
		</tbody>
	</table>
</div>
{% else %}
<h3>No results found</h3>
{% endif %}

<div class="center">
	{% if use_elastic %}
	{{ pagination.info }}
	{{ pagination.links }}
	{% else %}
	{% from "bootstrap/pagination.html" import render_pagination %}
	{{ render_pagination(torrent_query) }}
	{% endif %}
</div>