{% extends "base.html" %}

{% from "macros/box.html" import m_box_menu %}
{% from "macros/tag.html" import m_tags_from_hit %}

{% block main %}

{# Search bar on top #}

<div class="ab-search-input row">
  <div class="col-sm-8 col-sm-offset-2">
    <form role="search" method="GET" action="{{ url_for('search.search_main') }}">
      <div class="input-group">
        <input type="search" name="q" class="search-query form-control" value="{{ q }}"
               placeholder="{{ _('Search') }}" autocomplete="off" id="search-box"
               autofocus="autofocus" />
        <span class="input-group-btn">
          <button class="btn btn-default" type="submit">
            <span class="glyphicon glyphicon-search"></span>
          </button>
        </span>
      </div>
    </form>
    <div class="clearfix visible-xs-block"></div>

    {%- if not results %}
      <p class="lead">{{ _("Your query didn't match any results.") }}</p>
    {%- else %}
      <p class="ab-search-results-count">
        {{ _('%(count)d items found', count=results_count) }}
      </p>
    {%- endif -%}
  </div>
</div>


<div class="row">
  <div class="col-sm-3">
    {%- if by_object_type %}
      {% call m_box_menu(title=_('Filter by type'), omit_body=True) %}
        <div class="list-group">
          {%- for name, count, link, is_active in by_object_type %}
            <a class="list-group-item{% if is_active %} active"{%- endif %}"
               href="{{ link }}">{{ name }}
              {%- if count %}
              <span class="badge">{{ count|roughsize }}</span>
              {%- endif %}
            </a>
          {%- endfor %}
        </div>
      {% endcall %}
    {%- endif %}
  </div>

  <div class="col-sm-9">
    {% if results %}
      <div class="ab-search-results">
      {%- for hit in results %}
        <div class="ab-search-result">
          {%- if (filtered_by_type|length) != 1 %}
            <span class="label label-info">{{ friendly_fqcn(hit['object_type']) }}</span> &nbsp;
          {%- endif %}
          <a href="{{ url_for_hit(hit)|safe }}"><span class="title">{{ hit.highlights('name')|safe or hit['name']}}</span></a>
          {%- if hit['tag_ids'] %}
            <div class="tags">
              {{ m_tags_from_hit(hit) }}
            </div>
          {%- endif  %}

          {%- if 'description' in hit %}
            <div class="snippet text-muted">{{ hit.highlights('description')|safe }}</div>
          {%- endif %}

          {%- if 'creator' in hit %}
          {%- set author_type, author_id = hit['creator'].split(':') %}
          {%- set author_href = url_for('social.' + author_type, **{(author_type + '_id'): author_id}) %}
            <div class="metadata">
              <i>{{ _('by') }}
                <a href="{{ author_href }}">{{hit['creator_name'] }}</a>
                &ndash;
                {{ _('Last modification: %(date)s',
                     date=hit['updated_at'] | age(date_threshold='day')) }}
              </i><br />
            </div>
          {%- endif %}
        </div>
      {%- endfor %}
      </div>

      {%- if pagecount > 1 %}
        <ul class="pagination">

          {%- if next_pages_numbered and next_pages_numbered[0][0] > 1 %}
            <li><a href="{{ first_page }}">
              <i class="glyphicon glyphicon-step-backward"></i>
            </a></li>
            <li class="disabled"><span>…</span></li>
          {%- endif %}

          {%- for page_number, page_url in next_pages_numbered %}
            {%- set is_current = (page_number == results.pagenum) %}
            <li{% if is_current %} class="active"{%- endif %}>
            {%- if not is_current %}<a href="{{ page_url }}">{%- endif %}
            <span>{{ page_number }}</span>
            {%- if not is_current %}</a>{%- endif %}
          {%- endfor %}

          {%- if next_pages_numbered and next_pages_numbered[-1][0] < pagecount %}
          <li class="disabled"><span>…</span></li>
          <li><a href="{{ last_page }}">
            <i class="glyphicon glyphicon-step-forward"></i>
          </a></li>
          {%- endif %}

        </ul>
      {%- endif %}
    {%- endif %}
  </div>

</div>
{% endblock %}
