{% extends "community/_base.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 -%}

{%- set is_manager = g.community.has_permission(current_user, 'manage') -%}

{%- 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=55) }}
        {% endcall %}
      </div>

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

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

    <ul class="posts">
    {%- for post in thread.posts[1:] %}
      <li>
        {{ m_post(post) }}
      </li>
    {%- endfor %}
    </ul>

   {%- if not is_closed %}
    <hr />

    <div class="media">
      <div class="media-left">
        {% call m_user_link(current_user) %}
          {{ m_user_photo(current_user, size=55) }}
        {% endcall %}
      </div>

      <div class="media-body">
        <h3 style="margin-top: 0;">{{ _("Post a comment") }}</h3>
        <form action="." method="POST" enctype="multipart/form-data">
          {{ form.csrf_token }}

          {{ m_field(form.message, class_="resizeable-vertical", rows=10) }}
          {{ m_field(form.attachments) }}

          {%- if g.community.type == 'participative' or is_manager %}
            {{ m_field(form.send_by_email) }}
          {%- endif %}

          <div class="form-actions">
            <button type="submit" class="btn btn-primary" name="__action" value="create">
              {{ _("Post this comment") }}
            </button>
            <button type="submit" class="btn cancel" name="__action" value="cancel">
              {{ _("Cancel") }}
            </button>
          </div>
        </form>
      </div>
    </div>
    {%- 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 %}

{%- block sidebar %}
  {%- call m_box_menu() %}
    <ul class="nav nav-list">
      {%- set content_actions = actions.for_category('forum:thread') %}
      {%- set global_actions = actions.for_category('forum:global') %}

      {%- for action in content_actions %}
        <li>{{ action.render() }}</li>
      {%- endfor %}

      {%- if content_actions and global_actions %}
        <li class="divider"></li>
      {%- endif %}

      {%- for action in global_actions %}
        <li>{{ action.render() }}</li>
      {%- endfor %}
    </ul>
  {%- endcall %}
{%- endblock %}


{%- block modals %}
  <div class="modal fade" id="modal-delete" role="dialog"
      data-keyboard="true">

    <div class="modal-dialog">
      <div class="modal-content">
        <form
          action="{{ url_for('.thread_delete', thread_id=thread.id, community_id=g.community.slug) }}"
          method="POST" style="margin-bottom: 0;">

          {{ csrf.field() }}

          <div class="modal-header">
            <button class="close" data-dismiss="modal">&times;</button>
            <h3>{{ _("Delete Thread") }}</h3>
          </div>

          <div class="modal-body">
            {{ _('Are you sure you want to delete this thread?') }}
          </div>

          <div class="modal-footer">
            <button data-dismiss="modal" class="btn">{{ _("Cancel") }}</button>
            <button type="submit" name="__action" value="delete" class="btn btn-danger">{{ _("Really Delete") }}</button>
          </div>
        </form>
      </div>
    </div>
  </div>
{%- endblock %}
