{% set page_title = topic.title ~ " - Topic" %}
{% set active_forum_nav=True %}

{% extends theme("layout.html") %}
{% block content %}
{% from theme('macros.html') import render_pagination, form_field %}

<ol class="breadcrumb">
    <li><a href="{{ url_for('forum.index') }}">Forum</a></li>
    <li><a href="{{ topic.forum.category.url }}">{{ topic.forum.category.title }}</a></li>
    <li><a href="{{ topic.forum.url }}">{{ topic.forum.title }}</a></li>
    <li class="active">{{ topic.title }}</li>
</ol>

<div class="pull-left" style="padding-bottom: 10px">
    {{ render_pagination(posts, topic.url) }}
</div> <!-- end span pagination -->

<div class="pull-right" style="padding-bottom: 10px">
    <div class="btn btn-group">
    {% if current_user|delete_topic(topic.first_post.user_id, topic.forum) %}
        <a href="{{ url_for('forum.delete_topic', topic_id=topic.id, slug=topic.slug) }}" class="btn btn-danger">
            <span class="fa fa-trash-o"></span> Delete Topic
        </a>
    {% endif %}
    {% if current_user|lock_topic(topic.forum) %}
        {% if not topic.locked %}
            <a href="{{ url_for('forum.lock_topic', topic_id=topic.id, slug=topic.slug) }}" class="btn btn-warning">
                <span class="fa fa-lock"></span> Lock Topic
            </a>
        {% else %}
            <a href="{{ url_for('forum.unlock_topic', topic_id=topic.id, slug=topic.slug) }}" class="btn btn-warning">
                <span class="fa fa-unlock"></span> Unlock Topic
            </a>
        {% endif %}
    {% endif %}
    </div>

    {% if current_user.is_authenticated() %}
    <div class="btn btn-group">
        {% if current_user.is_tracking_topic(topic) %}
        <a href="{{ url_for('forum.untrack_topic', topic_id=topic.id, slug=topic.slug) }}" class="btn btn-default"><span class="fa fa-star">
            </span> Untrack Topic
        </a>
        {% else %}
        <a href="{{ url_for('forum.track_topic', topic_id=topic.id, slug=topic.slug) }}" class="btn btn-default">
            <span class="fa fa-star"></span> Track Topic
        </a>
        {% endif %}

        {% if current_user|post_reply(topic.forum) and not (topic.locked or topic.forum.locked) %}
        <a href="{{ url_for('forum.new_post', topic_id=topic.id, slug=topic.slug) }}" class="btn btn-primary">
            <span class="fa fa-pencil"></span> Reply
        </a>
        {% endif %}
    </div>
    {% endif %}
</div>

<table class="table table-bordered">
    <tbody>
        {% for post in posts.items %}
        <tr>
            <td >
                <span class="pull-right">
                    <strong>#{%- if posts.page == 1 -%} {{ loop.index }} {%- else -%} {{ loop.index + (posts.page - 1) * config["POSTS_PER_PAGE"] }} {%- endif -%}</strong>
                </span>
                <span class="pull-left">
                    <a href="
                    {%- if posts.page > 1 -%}
                        {{ topic.url }}?page={{ posts.page }}#pid{{ post.id }}
                    {%- else -%}
                        {{ topic.url }}#pid{{ post.id }}
                    {%- endif -%}
                        ">{{ post.date_created|format_date('%d %B %Y') }}</a>
                    {% if post.user_id and post.date_modified %}
                    <small>
                        (Last modified: {{ post.date_modified|format_date }} by
                        <a href="{{ url_for('user.profile', username=post.modified_by) }}">
                            {{ post.modified_by }}
                        </a>.)
                    </small>
                    {% endif %}
                </span>
            </td>
        </tr>

        <tr>
        <td>
            <table class="table table-borderless">
                <tr>
                {% if post.user_id %}
                    {% if post.user.avatar %}
                    <td width="1">
                        <img src="{{ post.user.avatar }}" alt="Avatar" height="100" width="100">
                    </td>
                    {% endif %}
                    <td>
                        <a href="{{ post.user.url }}">
                            <span style="font-weight:bold">{{ post.user.username }}</span> <!-- TODO: Implement userstyles -->
                        </a>
                            {%- if post.user|is_online %}
                            <span class="label label-success">Online</span>
                            {%- else %}
                            <span class="label label-default">Offline</span>
                            {%- endif %}
                            <div class="profile primary-group">
                            {{ post.user.primary_group.name }}
                            </div>
                    </td>

                    <td class="pull-right">
                        Posts: {{ post.user.post_count }}<br />
                        Registered since: {{ post.user.date_joined|format_date('%b %d %Y') }}<br />
                    </td>
                {% else %}
                    <td>
                        <strong>{{ post.username }}</strong>
                        <br />
                        Guest
                    </td>
                {% endif %}
                </tr>
            </table>
        </td>
        </tr>

        <tr>
            <td>
                <div class="post_body" id="pid{{ post.id }}">
                {% autoescape false %}
                    {{ post.content|markup }}
                    <!-- Signaure Begin -->
                    {% if post.user_id and post.user.signature %}
                    <hr>
                    {{ post.user.signature|markup }}
                    {% endif %}
                    <!-- Signaure End -->
                {% endautoescape %}
                </div>
            </td>
        </tr>
        <tr>
            <td>
                <span class="pull-left">
                    {% if current_user.is_authenticated and post.user_id %}
                    <a href="{{ url_for('user.new_message') }}?to_user={{ post.user.username }}">PM</a>
                    {% endif %}
                    {% if post.user.website %}
                    | <a href="{{post.user.website}}">Website</a>
                    {% endif %}
                </span>

                <span class="pull-right">
                    <a href="{{ url_for('forum.report_post', post_id=post.id) }}" onclick="window.open(this.href, 'wio_window','width=500,height=500'); return false;">
                        Report
                    </a> |
                    {% if current_user|edit_post(post.user_id, topic.forum) %}
                    <a href="{{ url_for('forum.edit_post', post_id=post.id) }}">Edit</a> |
                    {% endif %}
                    {% if topic.first_post_id == post.id %}
                        {% if current_user|delete_topic(topic.first_post.user_id, topic.forum) %}
                        <a href="{{ url_for('forum.delete_topic', topic_id=topic.id, slug=topic.slug) }}">Delete</a> |
                        {% endif %}
                    {% else %}
                        {% if current_user|delete_post(post.user_id, topic.forum) %}
                        <a href="{{ url_for('forum.delete_post', post_id=post.id) }}">Delete</a> |
                        {% endif %}
                    {% endif %}

                    <a href="#">Quote</a>
                </span>
            </td>
        </tr>
        {% endfor %}

    </tbody>
</table>

{% if form %}
    {% from "macros.html" import render_field %}
<form class="form" action="#" method="post">
    {{ form.hidden_tag() }}

    {{ render_field(form.content, div_class="col-sm-12", rows=5) }}

    <button type="submit" class="btn btn-success">Reply!</button>
</form>
{% endif %}

{% endblock %}
