{% extends "cranefly/layout.html" %}
{% import "cranefly/macros.html" as macros with context %}

{% block title %}{{ macros.page_title(title=_('Popular Threads')) }}{% endblock %}

{% block container %}
<div class="page-header header-primary">
  <div class="container">
    {{ messages_list(messages) }}
    <h1>{% trans %}Popular Threads{% endtrans %}</h1>
  </div>
</div>

<div class="container container-primary">
  {% if threads %}
  <div class="forum-threads-list">
    <table class="table">
      <thead>
        <tr>
          <th>{% trans %}Thread{% endtrans %}</th>
          <th class="span1">{% trans %}Rating{% endtrans %}</th>
          <th class="span5">{% trans %}Activity{% endtrans %}</th>
          {% if user.is_authenticated() and list_form %}
          <th class="check-cell"><label class="checkbox"><input type="checkbox" class="checkbox-master"></label></th>
          {% endif %}
        </tr>
      </thead>
      <tbody>
        {% for thread in threads %}
        <tr>
          <td>
            <a href="{% url 'thread_new' thread=thread.pk, slug=thread.slug %}" class="thread-icon{% if not thread.is_read %} thread-new{% endif %} tooltip-top" title="{% if not thread.is_read -%}
            {% trans %}Click to see first unread post{% endtrans %}
            {%- else -%}
            {% trans %}Click to see last post{% endtrans %}
            {%- endif %}"><i class="icon-comment"></i></a>
            <a href="{% url 'thread' thread=thread.pk, slug=thread.slug %}" class="thread-name">{{ thread.name }}</a>
            <span class="thread-details">
              {% trans user=thread_starter(thread), forum=thread_forum(thread), start=thread.start|reltimesince|low %}by {{ user }} in {{ forum }} {{ start }}{% endtrans %}
            </span>
            <ul class="unstyled thread-flags">
              {% if thread.weight == 2 %}
              <li><i class="icon-star tooltip-top" title="{% trans %}This thread is an annoucement{% endtrans %}"></i></li>
              {% endif %}
              {% if thread.weight == 1 %}
              <li><i class="icon-star-empty tooltip-top" title="{% trans %}This thread is sticky{% endtrans %}"></i></li>
              {% endif %}
              {% if thread.closed %}
              <li><i class="icon-lock tooltip-top" title="{% trans %}This thread is closed{% endtrans %}"></i></li>
              {% endif %}
            </ul>
          </td>
          <td>
            <div class="thread-rating{% if (thread.upvotes-thread.downvotes) > 0 %} thread-rating-positive{% elif (thread.upvotes-thread.downvotes) < 0 %} thread-rating-negative{% endif %}">
              {% if (thread.upvotes-thread.downvotes) > 0 %}+{% elif (thread.upvotes-thread.downvotes) < 0 %}-{% endif %}{{ (thread.upvotes-thread.downvotes)|abs|intcomma }}
            </div>
          </td>
          <td>
            <div class="thread-last-reply">
              {{ replies(thread.replies) }} - {% trans user=thread_reply(thread), last=thread.last|reltimesince|low %}last by {{ user }} {{ last }}{% endtrans %}
            </div>
          </td>
        </tr>
        {% endfor %}
      </tbody>
    </table>
  </div>
  {% else %}
  <p class="lead">{% trans %}Looks like there are no popular threads... yet!{% endtrans %}</p>
  {% endif %}
</div>
{% endblock %}


{% macro replies(thread_replies) -%}
{% trans count=thread_replies, replies=('<strong>' ~ (thread_replies|intcomma) ~ '</strong>')|safe -%}
{{ replies }} reply
{%- pluralize -%}
{{ replies }} replies
{%- endtrans %}
{%- endmacro %}

{% macro thread_starter(thread) -%}
{% if thread.start_poster_id %}<a href="{% url 'user' user=thread.start_poster_id, username=thread.start_poster_slug %}" class="user-link">{{ thread.start_poster_name }}</a>{% else %}{{ thread.start_poster_name }}{% endif %}
{%- endmacro %}

{% macro thread_forum(thread) -%}
<a href="{% url 'forum' forum=thread.forum_id, slug=thread.forum.slug %}" class="forum-link">{{ thread.forum.name }}</a>
{%- endmacro %}

{% macro thread_reply(thread) -%}
{% if thread.last_poster_id %}<a href="{% url 'user' user=thread.last_poster_id, username=thread.last_poster_slug %}" class="user-link">{{ thread.last_poster_name }}</a>{% else %}{{ thread.last_poster_name }}{% endif %}
{%- endmacro %}

{% macro pager() -%}
{% if items_total > 0 and pagination['total'] > 1 %}
<div class="pagination">
  <ul>
    <li class="count">{{ macros.pager_label(pagination) }}</li>
    {%- if pagination['prev'] > 0 %}<li><a href="{%- if pagination['prev'] > 1 %}{% url 'popular_threads' page=pagination['prev'] %}{% else %}{% url 'popular_threads' %}{% endif %}" class="tooltip-top" title="{% trans %}Previous Page{% endtrans %}"><i class="icon-chevron-left"></i></a></li>{% endif -%}
    {%- if pagination['next'] > 0 %}<li><a href="{% url 'popular_threads' page=pagination['next'] %}" class="tooltip-top" title="{% trans %}Next Page{% endtrans %}"><i class="icon-chevron-right"></i></a></li>{% endif -%}
  </ul>
</div>
{% endif %}
{%- endmacro %}