{% extends "forum/thread.html" %}

{%- from "macros/user.html" import m_user_link, m_user_photo %}
{%- from "macros/box.html" import m_box_content, m_box_menu -%}
{%- from "macros/form.html" import m_field -%}
{%- from "community/macros.html" import show_all_viewers -%}


{%- block content %}
  {% call m_box_content() %}
    {#  TODO #}
    <p>
      <a href="{{ url_for('.index', community_id=g.community.slug) }}">
        <i class="fa fa-arrow-left"></i> {{ _("Back to conversation list") }}</a>
    </p>

    <article class="thread">
      {%- set post = thread.posts[0] %}
      <h2>{{ thread.title }}</h2>

      <div>
        {{ _("Posted by %(creator)s", creator=m_user_link(thread.creator)) }}
        –
        {{ thread.created_at | age(date_threshold='day') }}
        {%- if is_closed %} <i class="fa fa-lock fa-big"></i>{%- endif %}
        {{ m_post_edit_link(post) }}
      </div>
      
      

      <div class="media post">
        <div class="media-left">
          {% call m_user_link(thread.creator) %}
            {{ m_user_photo(thread.creator, size=40) }}
          {% endcall %}
        </div>

        <div class="media-body">
          {{ m_post_content(thread.posts[0]) }}
        </div>
        <div class="media-post-tail"></div>
      </div>
      <hr>
      
      {{show_all_viewers(viewers)}}

      {%- if thread.posts|length > 1 %}
        <hr class="clearfix" style="margin-top: 5px;">
      {%- endif %}

      
    </article>
  {% endcall %}
{%- endblock %}

{# macros #}
{%- macro m_post_edit_link(post) %}
  {%- if not is_closed and (g.user == post.owner or g.is_manager) %}
    <a href="{{ url_for('.post_edit', community_id=post.thread.community.slug, thread_id=post.thread.id, object_id=post.id) }}">
      <i class="fa fa-edit"></i>
      {{ _('Edit') }}
    </a>
  {%- endif %}
{%- endmacro %}

{%- macro m_post_content(post) %}
  <div class="body">
    {{ post.body_html|safe }}
  </div>

  {%- if post.attachments %}
    <div class="attachments">
      <ul>
        {%- for attachment in post.attachments %}
          <li>
            <span class="attachment-item">
              <img src="{{ attachment.icon }}"/>
              <a href="{{ url_for(attachment) }}">{{ attachment.name }}</a>
              ({{ attachment.content_length | filesize }})
            </span>
          </li>
        {%- endfor %}
      </ul>
    </div>
  {%- endif %}

  {%- for entry in post.history %}
    <div class="history">
      {%- set date = entry.date|datetimeparse|age(date_threshold='day') %}
      {{ _('edited by %(user)s - %(date)s', user=entry.user, date=date) }}
      {%- if entry.reason %}<q class="reason">{{ entry.reason }}</q>{%- endif %}
    </div>
  {%- endfor %}
{%- endmacro %}

{%- macro m_post(post) %}
  <div class="post media">
    <div class="media-left">
      {% call m_user_link(post.creator) %}
        {{ m_user_photo(post.creator, size=55) }}
      {% endcall %}
    </div>

    <div class="media-body">
      <a id="post_{{ post.id }}"></a>

      <div>
        {% call m_user_link(post.creator) %}
          <b>{{ post.creator }}</b>
        {% endcall %}
        <span class="date">{{ post.created_at | age(date_threshold='day') }}</span>
        {{ m_post_edit_link(post) }}
      </div>

      {{ m_post_content(post) }}
    </div>
  </div>
{%- endmacro %}
