{% set page_title = _("Search") %}

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

<div class="page-view">
    <ol class="breadcrumb flaskbb-breadcrumb">
        <li><a href="{{ url_for('forum.index') }}">{% trans %}Forum{% endtrans %}</a></li>
        <li class="active">{% trans %}Search{% endtrans %}</li>
    </ol>

    {% if result['post'] %}
    <div class="panel topic-panel">
        <div class="panel-heading topic-head">
            {% trans %}Posts{% endtrans %}
        </div>
        <div class="panel-body topic-body">
            {% for post in result['post'] %}
            <div id="{{ post.id }}" class="row post-row clearfix">

                <div class="author col-md-2 col-sm-3 col-xs-12">
                    <!-- Registered User -->
                    {% if post.user_id %}
                    <div class="author-name"><h4><a href="{{ post.user.url }}">{{ post.user.username }}</a></h4></div>

                    <!-- check if user is online or not -->
                    {% if post.user|is_online %}
                    <div class="author-online" data-toggle="tooltip" data-placement="top" title="online"></div>
                    {% else %}
                    <div class="author-offline" data-toggle="tooltip" data-placement="top" title="offline"></div>
                    {% endif %}
                    <div class="author-title"><h5>{{ post.user.primary_group.name }}</h5></div>

                    {% if post.user.avatar %}
                        <div class="author-avatar"><img src="{{ post.user.avatar }}" alt="avatar"></div>
                    {% endif %}

                    <div class="author-registered">{% trans %}Joined{% endtrans %}: {{ post.user.date_joined|format_date('%b %d %Y') }}</div>
                    <div class="author-posts">{% trans %}Posts{% endtrans %}: {{ post.user.post_count }}</div>
                    <div class="author-pm">
                        {% if current_user.is_authenticated and post.user_id %}
                        <a href="{{ url_for('message.new_conversation') }}?to_user={{ post.user.username }}">{% trans %}Message{% endtrans %}</a>
                        {% endif %}
                    </div>

                    {% if post.user.website %}
                    <div class="author-website"><a href="{{ post.user.website }}" rel="nofollow">{% trans %}Website{% endtrans %}</a></div>
                    {% endif %}

                    {% else %}
                    <!-- user deleted or guest -->
                    <div class="author-name"><h4><a href="{{ post.user.url }}">{{ post.username }}</a></h4></div>
                    <div class="author-title"><h5>{% trans %}Guest{% endtrans %}</h5></div>
                    {% endif %}

                </div>

                <div class="post-box col-md-10 col-sm-9 col-xs-12">

                    <div class="post-meta clearfix">
                        <div class="pull-left">
                            <!-- Creation date / Date modified -->
                            <a href="{{ post.url }}">
                                {{ post.date_created|format_date('%d %B %Y - %H:%M') }}
                            </a>
                            {% if post.user_id and post.date_modified %}
                            <small>
                                (Last modified: {{ post.date_modified|format_date('%d %B %Y - %H:%M') }} by
                                <a href="{{ url_for('user.profile', username=post.modified_by) }}">{{ post.modified_by }}</a>.)
                            </small>
                            {% endif %}
                        </div>

                        <!-- Post number -->
                        <div class="pull-right">
                            <strong>#{{ post.id }}</strong>
                        </div>
                    </div>

                    <div class="post-content post_body clearfix" id="pid{{ post.id }}">
                        {{ post.content|markup }}
                    </div>
                </div>
            </div>
            {% else %}
            <!-- cheating a bit here with page-row :P -->
            <div class="row page-row clearfix">
                <div class="col-md-12 col-sm-12 col-xs-12">{% trans %}No posts found matching your search criteria.{% endtrans %}</div>
            </div>
            {% endfor %}
        </div>
    </div>
    {% endif %}

    {% if result['user'] %}
    <div class="panel panel-default page-panel">
        <div class="panel-heading page-head">
            {% trans %}Users{% endtrans %}
        </div>
        <div class="panel-body page-body">
            <div class="page-meta">
                <div class="col-md-1 col-sm-1 col-xs-1 meta-item">#</div>
                <div class="col-md-3 col-sm-3 col-xs-5 meta-item">{% trans %}Username{% endtrans %}</div>
                <div class="col-md-2 col-sm-2 hidden-xs meta-item">{% trans %}Posts{% endtrans %}</div>
                <div class="col-md-3 col-sm-3 hidden-xs meta-item">{% trans %}Date registered{% endtrans %}</div>
                <div class="col-md-3 col-sm-3 col-xs-5 meta-item">{% trans %}Group{% endtrans %}</div>
            </div>
            {% for user in result['user'].all() %}
            <div class="row page-row hover clearfix">
                <div class="col-md-1 col-sm-1 col-xs-1">{{ user.id }}</div>
                <div class="col-md-3 col-sm-3 col-xs-5"><a href="{{ user.url }}">{{ user.username }}</a></div>
                <div class="col-md-2 col-sm-2 hidden-xs">{{ user.post_count }}</div>
                <div class="col-md-3 col-sm-3 hidden-xs">{{ user.date_joined|format_date('%b %d %Y') }}</div>
                <div class="col-md-3 col-sm-3 col-xs-5">{{ user.primary_group.name }}</div>
            </div>
            {% else %}
            <div class="row page-row hover clearfix">
                <div class="col-md-12 col-sm-12 col-xs-12">{% trans %}No users found matching your search criteria.{% endtrans %}</div>
            </div>
            {% endfor %}
        </div>
    </div>
    {% endif %}

    {% if result['topic'] %}
    <div class="panel panel-default forum-panel">
        <div class="panel-heading forum-head">
            {% trans %}Topics{% endtrans %}
        </div>

        <div class="panel-body forum-body">
            <div class="forum-meta">
                <div class="col-md-5 col-sm-5 col-xs-8 topic-name">{% trans %}Topic{% endtrans %}</div>
                <div class="col-md-2 col-sm-2 hidden-xs topic-stats">{% trans %}Posts{% endtrans %}</div>
                <div class="col-md-2 col-sm-2 hidden-xs topic-stats">{% trans %}Views{% endtrans %}</div>
                <div class="col-md-3 col-sm-3 col-xs-4 topic-last-post">{% trans %}Last Post{% endtrans %}</div>
            </div>

            {% for topic in result['topic'].all() %}
            <div class="row forum-row hover clearfix">

                <div class="col-md-5 col-sm-5 col-xs-8 topic-info">
                    <div class="row">
                        <div class="col-md-1 col-sm-2 col-xs-2 topic-status">
                        {% if topic.locked %}
                            <span class="fa fa-lock topic-locked"></span>
                        {% elif topic.important %}
                            <span class="fa fa-star-o topic-starred-read"></span>
                        {% else %}
                            <span class="fa fa-comment-o topic-read"></span>
                        {% endif %}
                        </div>
                        <div class="col-md-11 col-sm-10 col-xs-10">
                            <div class="topic-name">
                                <a href="{{ topic.url }}">{{ topic.title }}</a>
                                <!-- Topic Pagination -->
                                <span class="topic-pages">{{ topic_pages(topic, flaskbb_config["POSTS_PER_PAGE"]) }}</span>
                            </div>

                            <div class="topic-author">
                                {% trans %}by{% endtrans %}
                                {% if topic.user_id %}
                                 <a href="{{ topic.user.url }}">{{ topic.user.username }}</a>
                                {% else %}
                                {{ topic.username }}
                                {% endif %}
                            </div>
                        </div>
                    </div>
                </div>
                <div class="col-md-2 col-sm-2 hidden-xs topic-stats">
                    {{ topic.post_count }}
                </div>

                <div class="col-md-2 col-sm-2 hidden-xs topic-stats">
                    {{ topic.views }}
                </div>

                <div class="col-md-3 col-sm-3 col-xs-4 topic-last-post">
                    <a href="{{ topic.last_post.url }}">{{ topic.last_post.date_created|time_since }}</a><br />

                    <div class="topic-author">
                        {% trans %}by{% endtrans %}
                        {% if topic.last_post.user_id %}
                        <a href="{{ topic.last_post.user.url }}">{{ topic.last_post.user.username }}</a>
                        {% else %}
                        {{ topic.last_post.username }}
                        {% endif %}
                    </div>
                </div>

            </div>  <!-- end forum-row -->
            {% else %}
            <div class="row forum-row clearfix">
                <div class="col-md-12 col-sm-12 co-xs-12">
                    {% trans %}No topics found matching your search criteria.{% endtrans %}
                </div>
            </div> <!-- end forum-row -->
            {% endfor %}
        </div>
    </div>
    {% endif %}

    {% if result['forum'] %}
    <div class="panel panel-default category-panel">
        <div class="panel-heading category-head">
            Forums
        </div>

        <div class="panel-body category-body">
            <div class="category-meta">
                <div class="col-md-5 col-sm-5 col-xs-8 forum-name">{% trans %}Forum{% endtrans %}</div>
                <div class="col-md-2 col-sm-2 hidden-xs forum-stats">{% trans %}Topics{% endtrans %}</div>
                <div class="col-md-2 col-sm-2 hidden-xs forum-stats">{% trans %}Posts{% endtrans %}</div>
                <div class="col-md-3 col-sm-3 col-xs-4 forum-last-post">{% trans %}Last Post{% endtrans %}</div>
            </div>
            {% for forum in result['forum'].all() %}
            <div class="row category-row hover">

                {% if forum.external %}
                <div class="col-md-5 col-sm-5 col-xs-8 forum-info">
                    <div class="row">
                        <!-- Icon -->
                        <div class="col-md-1 col-sm-2 col-xs-2 forum-status">
                            <span class="fa fa-external-link forum-external"></span>
                        </div>

                        <div class="col-md-11 col-sm-10 col-xs-10">
                            <!-- Forum Name -->
                            <div class="forum-name">
                                <span class="forum-link">{% trans %}Link to{% endtrans %}:</span> <a href="{{ forum.url }}">{{ forum.title }}</a>
                            </div>

                            <!-- Forum Description -->
                            <div class="forum-description">
                                {{ forum.description|markup }}
                            </div>
                        </div>
                    </div>
                </div> <!-- end forum-info -->

                <!-- Post Count -->
                <div class="col-md-2 col-sm-2 hidden-xs forum-posts">
                    -
                </div>

                <!-- Topic Count -->
                <div class="col-md-2 col-sm-2 hidden-xs forum-topics">
                    -
                </div>

                <!-- Last Post -->
                <div class="col-md-3 col-sm-3 col-xs-4 forum-last-post">
                    ---
                </div>
                {% else %}
                <div class="col-md-5 col-sm-5 col-xs-8 forum-info">
                    <div class="row">
                        <!-- Icon -->
                        <div class="col-md-1 col-sm-2 col-xs-2 forum-status">
                            {% if forum.locked %}
                                <span class="fa fa-lock forum-locked"></span>
                            {% else %}
                                <span class="fa fa-comments-o forum-read"></span>
                            {% endif %}
                        </div>

                        <div class="col-md-11 col-sm-10 col-xs-10">
                            <!-- Forum Name -->
                            <div class="forum-name">
                                <a href="{{ forum.url }}">{{ forum.title }}</a>
                            </div>

                            <!-- Forum Description -->
                            <div class="forum-description">
                                {{ forum.description|markup }}
                            </div>

                            <!-- Forum Moderators -->
                            {% if forum.show_moderators %}
                            <div class="forum-moderators">
                                {% trans %}Moderators{% endtrans %}:
                                {% for moderator in forum.moderators %}
                                <a href="{{ url_for('user.profile', username=moderator.username) }}">{{ moderator.username }}</a>{% if not loop.last %}, {% endif %}
                                {% endfor %}
                            </div>
                            {% endif %}
                        </div>
                    </div>
                </div> <!-- end forum-info -->

                <!-- Post Count -->
                <div class="col-md-2 col-sm-2 hidden-xs forum-topics">
                {{ forum.topic_count }}
                </div>

                <!-- Topic Count -->
                <div class="col-md-2 col-sm-2 hidden-xs forum-posts">
                {{ forum.post_count }}
                </div>

                <!-- Last Post -->
                <div class="col-md-3 col-sm-3 col-xs-4 forum-last-post">
                    {% if forum.last_post_id %}
                    <div class="last-post-title">
                        <a href="{{ forum.last_post_url }}" title="{{ forum.last_post_title }}">
                            {{ forum.last_post_title|crop_title }}
                        </a>
                    </div>

                    <div class="last-post-time">
                        {{ forum.last_post_created|time_since }}
                    </div>

                    <div class="last-post-author">
                        {% trans %}by{% endtrans %}
                        {% if forum.last_post_user_id %}
                        <a href="{{ url_for('user.profile', username=forum.last_post_username) }}">{{ forum.last_post_username }}</a>
                        {% else %}
                        {{ forum.last_post_username }}
                        {% endif %}
                    </div>

                    {% else %}
                    {% trans %}No posts.{% endtrans %}
                    {% endif %} {# endif forum.last_post_id #}
                </div>

            {% endif %} {# endif forum.external #}
            </div> <!-- end category-content -->
            {% else %}
            <div class="row category-row">
                <div class="col-md-12 col-sm-12 col-xs-12">
                    {% trans %}No forums found matching your search criteria.{% endtrans %}
                </div>
            </div>
            {% endfor %}
        </div>
    </div>
    {% endif %}

</div>

{% endblock %}
