{% load i18n %}
{% load mptt_tags %}
{% load forum_tags %}
{% load forum_conversation_tags %}
{% load forum_tracking_tags %}

<div class="row">
    <div class="col-xs-12">
        {% get_unread_forums forums user as unread_forums %}
        {% if forums %}
            {% recursetree forums %}
                {% if node.level == root_level %}
                    {% if node.is_category %}
                        <div class="panel panel-default forumlist">
                            <div class="panel-heading">
                                <div class="row panel-row">
                                    <div class="col-md-8 col-sm-9 col-xs-12 forum-name-col">
                                        <h3 class="panel-title"><a href="{% url 'forum:forum' node.slug node.id %}" class="brand">{{ node.name }}</a></h3>
                                    </div>
                                    <div class="col-md-1 forum-count-col hidden-sm hidden-xs">{% trans "Topics" %}</div>
                                    <div class="col-md-1 forum-count-col hidden-sm hidden-xs">{% trans "Posts" %}</div>
                                    <div class="col-md-2 col-sm-3 hidden-xs forum-last-post-col">{% trans "Last post" %}</div>
                                </div>
                            </div>
                            <div class="panel-body">
                                {{ children }}
                            </div>
                        </div>
                    {% else %}
                        {% with node.get_previous_sibling as previous_sibling %}
                            {% if previous_sibling and previous_sibling.is_category or not previous_sibling in forums or not previous_sibling %}
                                <div class="panel panel-default forumlist">
                                    <div class="panel-heading">
                                        <div class="row panel-row">
                                            <div class="col-md-8 col-sm-9 col-xs-12 forum-name-col">
                                                <h3 class="panel-title">{% trans "Forums" %}</h3>
                                            </div>
                                            <div class="col-md-1 forum-count-col hidden-sm hidden-xs">{% trans "Topics" %}</div>
                                            <div class="col-md-1 forum-count-col hidden-sm hidden-xs">{% trans "Posts" %}</div>
                                            <div class="col-md-2 col-sm-3 hidden-xs forum-last-post-col">{% trans "Last post" %}</div>
                                        </div>
                                    </div>
                                    <div class="panel-body">
                            {% endif %}
                        {% endwith %}
                        <div class="row panel-row">
                            {% if not node.is_link %}
                                <div class="col-md-7 col-sm-9 col-xs-11 forum-name">
                                    <table class="forum-data-table">
                                        <tr>
                                            <td class="forum-icon {% if node in unread_forums %}unread{% endif %}">
                                                <i class="fa fa-circle-o fa-2x"></i>
                                            </td>
                                            <td>
                                                {% if node.image %}
                                                    <td>
                                                        <div class="visible-md visible-lg forum-image">
                                                            <img src="{{ node.image.url }}" alt="{{ node.name }}" />
                                                        </div>
                                                    </td>
                                                {% endif %}
                                            <td>
                                                <a href="{% url 'forum:forum' node.slug node.id %}" class="forum-name-link">{{ node.name }}</a>
                                                <div class="forum-description">
                                                    {{ node.description.rendered }}
                                                </div>
                                                <div class="sub-forums">{{ children }}</div>
                                            </td>
                                        </tr>
                                    </table>
                                </div>
                                <div class="col-md-1 forum-syndication hidden-sm hidden-xs"><a href="{% url 'forum_feeds:forum_latest_topics_with_descendants' node.slug node.pk %}" title="{% trans 'RSS feed' %}"><i class="fa fa-rss-square"></i></a></div>
                                <div class="col-md-1 forum-count hidden-sm hidden-xs">{{ node.topics_count }}</div>
                                <div class="col-md-1 forum-count hidden-sm hidden-xs">{{ node.posts_count }}</div>
                                <div class="col-md-2 col-sm-3 hidden-xs forum-last-post">
                                    {% get_forum_last_post node user as last_post %}
                                      {% if last_post %}
                                        {% if last_post.poster %}
                                          {% url 'forum_member:profile' last_post.poster_id as poster_url %}
                                          {% blocktrans trimmed with poster_url=poster_url username=last_post.poster.username %}
                                            By: <a href="{{ poster_url }}">{{ username }}</a>
                                          {% endblocktrans %}
                                        {% else %}
                                          {% blocktrans trimmed with poster_username=last_post.username %}
                                            By: {{ poster_username }}
                                          {% endblocktrans %}
                                        {% endif %}
                                        &nbsp;<a href="{% url 'forum_conversation:topic' node.slug node.pk last_post.topic.slug last_post.topic.pk %}?post={{ last_post.pk }}#{{ last_post.pk }}"><i class="fa fa-arrow-circle-o-right "></i></a>
                                        <br />
                                        {{ last_post.created }}
                                    {% else %}
                                        {% trans "No posts" %}
                                    {% endif %}
                                </div>
                            {% else %}
                                <div class="col-md-8 col-sm-9 col-xs-11 forum-name" colspan="2">
                                    <table class="forum-data-table">
                                        <tr>
                                            <td class="forum-icon link">
                                                <i class="fa fa-link fa-2x"></i>
                                            </td>
                                            <td>
                                                <a href="{% url 'forum:forum' node.slug node.id %}" class="forum-name-link">{{ node.name }}</a>
                                                <div class="forum-description">
                                                    {{ node.description.rendered }}
                                                </div>
                                            </td>
                                        </tr>
                                    </table>
                                </div>
                                <div class="col-md-1 forum-count hidden-sm hidden-xs"></div>
                                <div class="col-md-1 forum-count hidden-sm hidden-xs"></div>
                                <div class="col-md-2 col-sm-3 hidden-xs forum-link-redirects">
                                    {% if node.link_redirects %}
                                        {% trans "Total redirects:" %}&nbsp;{{ node.link_redirects_count|default:"0" }}
                                    {% endif %}
                                </div>
                            {% endif %}
                        </div>
                        {% with node.get_next_sibling as next_sibling %}
                            {% if next_sibling and next_sibling.is_category or not next_sibling in forums or not next_sibling %}
                                    </div>
                                </div>
                            {% endif %}
                        {% endwith %}
                    {% endif %}
                {% elif node.level == root_level_middle %}
                    {% if node.parent.is_category %}
                        <div class="row panel-row">
                            {% if not node.is_link %}
                                <div class="col-md-7 col-sm-9 col-xs-11 forum-name">
                                    <table class="forum-data-table">
                                        <tr>
                                            <td class="forum-icon {% if node in unread_forums %}unread{% endif %}">
                                                <i class="fa fa-circle-o fa-2x"></i>
                                            </td>
                                            {% if node.image %}
                                                <td>
                                                    <div class="visible-md visible-lg forum-image">
                                                        <img src="{{ node.image.url }}" alt="{{ node.name }}" />
                                                    </div>
                                                </td>
                                            {% endif %}
                                            <td>
                                                <a href="{% url 'forum:forum' node.slug node.id %}" class="forum-name-link">{{ node.name }}</a>
                                                <div class="forum-description">
                                                    {{ node.description.rendered }}
                                                </div>
                                                <div class="sub-forums">{{ children }}</div>
                                            </td>
                                        </tr>
                                    </table>
                                </div>
                                <div class="col-md-1 forum-syndication hidden-sm hidden-xs"><a href="{% url 'forum_feeds:forum_latest_topics_with_descendants' node.slug node.pk %}" title="{% trans 'RSS feed' %}"><i class="fa fa-rss-square"></i></a></div>
                                <div class="col-md-1 forum-count hidden-sm hidden-xs">{{ node.topics_count }}</div>
                                <div class="col-md-1 forum-count hidden-sm hidden-xs">{{ node.posts_count }}</div>
                                <div class="col-md-2 col-sm-3 hidden-xs forum-last-post">
                                    {% get_forum_last_post node user as last_post %}
                                    {% if last_post %}
                                      {% if last_post.poster %}
                                        {% url 'forum_member:profile' last_post.poster_id as poster_url %}
                                        {% blocktrans trimmed with poster_url=poster_url username=last_post.poster.username %}
                                          By: <a href="{{ poster_url }}">{{ username }}</a>
                                        {% endblocktrans %}
                                      {% else %}
                                        {% blocktrans trimmed with poster_username=last_post.username %}
                                          By: {{ poster_username }}
                                        {% endblocktrans %}
                                      {% endif %}
                                        &nbsp;<a href="{% url 'forum_conversation:topic' node.slug node.pk last_post.topic.slug last_post.topic.pk %}?post={{ last_post.pk }}#{{ last_post.pk }}"><i class="fa fa-arrow-circle-o-right "></i></a>
                                        <br />
                                        {{ last_post.created }}
                                    {% else %}
                                        {% trans "No posts" %}
                                    {% endif %}
                                </div>
                            {% else %}
                                <div class="col-md-8 col-sm-9 col-xs-11 forum-name" colspan="2">
                                    <table class="forum-data-table">
                                        <tr>
                                            <td class="forum-icon link">
                                                <i class="fa fa-link fa-2x"></i>
                                            </td>
                                            <td>
                                                <a href="{% url 'forum:forum' node.slug node.id %}" class="forum-name-link">{{ node.name }}</a>
                                                <div class="forum-description">
                                                    {{ node.description.rendered }}
                                                </div>
                                            </td>
                                        </tr>
                                    </table>
                                </div>
                                <div class="col-md-1 forum-count hidden-sm hidden-xs"></div>
                                <div class="col-md-1 forum-count hidden-sm hidden-xs"></div>
                                <div class="col-md-2 col-sm-3 hidden-xs forum-last-post forum-link-redirects">
                                    {% if node.link_redirects %}
                                        {% trans "Total redirects:" %}&nbsp;{{ node.link_redirects_count|default:"0" }}
                                    {% endif %}
                                </div>
                            {% endif %}
                        </div>
                    {% else %}
                        {% if not node.get_previous_sibling %}
                            <b>{% trans "Subforums:" %}</b>&nbsp;
                        {% endif %}
                        <i class="fa fa-file"></i>&nbsp;<a href="{% url 'forum:forum' node.slug node.id %}">{{ node.name }}</a>&nbsp;&nbsp;
                    {% endif %}
                {% elif node.level == root_level_sub %}
                    {% if not node.get_previous_sibling %}
                        <b>{% trans "Subforums:" %}</b>&nbsp;
                    {% endif %}
                    <i class="fa fa-file"></i>&nbsp;<a href="{% url 'forum:forum' node.slug node.id %}">{{ node.name }}</a>&nbsp;&nbsp;
                {% endif %}
            {% endrecursetree %}
        {% else %}
            <div class="panel panel-default forumlist">
                <div class="panel-heading">
                    <div class="row panel-row">
                        <div class="col-md-8 col-sm-9 col-xs-9 forum-name-col">
                            <h3 class="panel-title">{% trans "Forums" %}</h3>
                        </div>
                        <div class="col-md-1 hidden-sm hidden-xs forum-count-col">{% trans "Topics" %}</div>
                        <div class="col-md-1 hidden-sm hidden-xs forum-count-col">{% trans "Posts" %}</div>
                        <div class="col-md-2 col-sm-3 hidden-xs forum-last-post-col">{% trans "Last post" %}</div>
                    </div>
                </div>
                <div class="panel-body">
                    <div class="row panel-row">
                        <div class="col-md-12">
                            {% trans "No forums." %}
                        </div>
                    </div>
                </div>
            </div>
        {% endif %}
    </div>
</div>
