{# vim: set ts=2 et sts=2 sw=2: #}
{% extends "forums/base.html" %}
{% from "layout/errorlist.html" import errorlist %}
{% from "includes/common_macros.html" import content_editor %}
{# L10n: {t} is the title of the thread. {f} is the name of the forum. #}
{% set title = _('{t} | {f} | Forums')|f(t=thread.title, f=forum.name) %}
{% set crumbs = [(url('forums.forums'), _('Forums')),
                 (url('forums.threads', forum.slug), forum.name),
                 (None, thread.title)] %}
{% set canonical_url = thread.get_absolute_url() %}
{% if posts.number > 1 %}
  {% set canonical_url = canonical_url|urlparams(page=posts.number) %}
{% endif %}

{% block content %}
<article id="posts" class="main">
  <h1>{{ thread.title }}</h1>
  <div class="badges">
    {% if thread.is_sticky %}<span class="sticky">{{ _('Sticky') }}</span>{% endif %}
    {% if thread.is_locked %}<span class="locked">{{ _('Locked') }}</span>{% endif %}
    {% if is_watching_thread %}<span class="watching">{{ _('Watching') }}</span>{% endif %}
  </div>
  <div class="thread-actions">
    {% if user.is_authenticated() %}
      <form action="{{ url('forums.watch_thread', forum_slug=forum.slug, thread_id=thread.id) }}" method="post">
        {{ csrf() }}
        {% if is_watching_thread %}
          {% set watch = _('Stop watching') %}
          <input type="hidden" name="watch" value="no" />
        {% else %}
          {% set watch = _('Watch this thread') %}
          <input type="hidden" name="watch" value="yes" />
        {% endif %}
        <input type="image" alt="{{ watch }}" title="{{ watch }}" src="{{ MEDIA_URL }}img/forums/watch.png"/>
      </form>
    {% endif %}
    {% if not thread.is_locked and
           has_perm_or_owns('forums_forum.thread_edit_forum', thread, forum) %}
      <a href="{{ url('forums.edit_thread', forum.slug, thread.id) }}"><img src="{{ MEDIA_URL }}img/forums/edit.png" alt="{{ _('Edit') }}" title="{{ _('Edit') }}"/></a>
    {% endif %}
    {% if has_perm('forums_forum.thread_delete_forum', forum) %}
      <a href="{{ url('forums.delete_thread', forum.slug, thread.id) }}">
        <img src="{{ MEDIA_URL }}img/forums/delete.png" alt="{{ _('Delete') }}" title="{{ _('Delete') }}">
      </a>
    {% endif %}
    {% if has_perm('forums_forum.thread_locked_forum', forum) %}
      <form action="{{ url('forums.lock_thread', forum.slug, thread.id) }}" method="post">
        {{ csrf() }}
        <input type="image" src="{{ MEDIA_URL }}img/forums/type/locked.png" alt="{{ _('Change locked status') }}" title="{{ _('Change locked status') }}"/>
      </form>
    {% endif %}
    {% if has_perm('forums_forum.thread_sticky_forum', forum) %}
      <form action="{{ url('forums.sticky_thread', forum.slug, thread.id) }}" method="post">
        {{ csrf() }}
        <input type="image" src="{{ MEDIA_URL }}img/forums/type/sticky.png" alt="{{ _('Change sticky status') }}" title="{{ _('Change sticky status') }}" />
      </form>
    {% endif %}
    {% if has_perm('forums_forum.thread_move_forum', forum) %}
      <form class="move" action="{{ url('forums.move_thread', forum.slug, thread.id) }}" method="post">
        {{ csrf() }}
        <select name="forum">
          {% for f in forums %}
            {% if has_perm('forums_forum.thread_move_forum', f) %}
              <option value="{{ f.id }}" {% if f == forum %}selected="selected"{% endif %}>{{ f.name }}</option>
            {% endif %}
          {% endfor %}
        </select>
        <input type="submit" value="Move Thread" />
      </form>
    {% endif %}
  </div>

  <div class="thread-watch">
    {% if user.is_authenticated() %}
    {% endif %}
  </div>

  <ol class="posts-columns">
    <li class="author">{{ _('Author') }}</li>
    <li class="message">{{ _('Message') }}</li>
  </ol>

  {% if posts %}
    <ol class="posts">
      {% for post in posts.object_list %}
        <li id="post-{{ post.id }}">
          {% include "forums/includes/post.html" %}
        </li>
      {% endfor %}
    </ol>

    {{ posts|paginator }}

  {% else %}
    <p>{{ _('Oh, no! Looks like there are no posts!') }}</p>
  {% endif %}

  {% if user.is_authenticated() and not thread.is_locked %}
    <div id="thread-reply">
      <h3>{{ _('Post a reply') }}</h3>

      <form action="{{ url('forums.reply', forum.slug, thread.id)|urlparams(hash='thread-reply') }}" method="post">
        {{ csrf() }}
        {{ errorlist(form) }}

        {{ content_editor(form.content) }}

        <div class="editor-actions">
          <input type="submit" name="preview" value="{{ _('Preview') }}" />
          <input type="submit" value="{{ _('Reply') }}" />
        </div>
      </form>
    </div>
  {% elif user.is_authenticated() and thread.is_locked %}
    <div id="thread-reply">
      <h3>{{ _('Post a reply') }}</h3>

      <p id="thread-locked">{{ _('This thread has been locked. It is no longer possible to post new replies.') }}</p>
    </div>
  {% endif %}
  {% if reply_preview %}
    <div id="post-preview">
      <h3>{{ _('Reply Preview:') }}</h3>
      <ol class="posts">
        <li>
          {% set post = reply_preview %}
          {% include "forums/includes/post.html" %}
        </li>
      </ol>
    </div>
  {% endif %}
</article>
{% endblock %}
