{#
# This file is part of Invenio.
# Copyright (C) 2013, 2014 CERN.
#
# Invenio is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# Invenio is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Invenio; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
#}
{%- if request.args.of == 'id' -%}
{{ records }}
{%- else -%}
{% from "_formhelpers.html" import render_filter_form with context %}
{% from "search/helpers.html" import collection_tree, portalbox_sidebar, search_also,  record_brief_links with context %}
{% extends "search/searchbar_frame.html" %}
{# set title = collection.name_ln if collection.id > 1 else None #}
{% set title = None %}

{%- set portalboxes = dict() -%}
{%- for k,l in collection.portalboxes_ln|groupby('position') -%}
  {%- do portalboxes.update({k:l}) -%}
{%- endfor -%}

{% block title %}
  {{ portalboxes.tp }}
  {{ super() }}
  {{ portalboxes.te }}
{% endblock %}
{% block header %}
  {{ super() }}
  {% if records|length > 0 %}
  {%- set args = request.args.copy().to_dict() -%}
  {%- set jrec = (pagination.page-2)*pagination.per_page+1 if pagination.has_prev else 1 -%}
  {%- do args.update({'p': records[0][0], 'f': f}) -%}
  {%- if pagination.has_prev -%}
  <link rel="prev" href="{{ url_for(request.endpoint, **args) }}"/>
  {%- endif -%}
  {%- if pagination.has_next -%}
  {%- set jrec = (pagination.page)*pagination.per_page+1 if pagination.has_next else (pagination.pages-1)*pagination.per_page+1 -%}
  {%- do args.update({'p': f ~ (':' if f) ~ records[-1][0]}) -%}
  <link rel="next" href="{{ url_for(request.endpoint, **args) }}"/>
  {%- endif -%}
  {% endif %}
{% endblock %}


{% block inner_content %}

<style>
.modal-backdrop {
  /* Show lighter modal window for search. */
  background-color: rgba(255,255,255,0.5);
}

body {
  position: relative;
}

</style>


{% if not records|length %}

<div class="row">
  <div class="col-md-12">
    <p>
      <strong>{{ _('Your search did not match any records. Please try again.') }}</strong>
    </p>
    {{ create_nearest_terms_box()|safe }}
  </div>
</div>

{% else %}

<div class="row">
  <div class="col-md-12">
    <table class="table table-striped table-hover">
      <tr>
        <th style="width: 20%">{{ _('Hits') }}</th>
        <th>{{ _('Values') }} <small>{{ f if f }}</small></th>
      </tr>
      {%- set args = request.args.copy().to_dict() -%}
      {%- do args.pop('action_browse', None) -%}
      {%- do args.pop('f', None) -%}
      {% for record in records %}
      <tr>
        <td>{{ record[1] }}</td>
        <td>
          {%- do args.update({'p': (f ~ ':' if f) ~ records[0]}) -%}
          <a href="{{ url_for('search.search',  **args) }}">{{ record[0] }}</a>
        </td>
      </tr>
    {% endfor %}
    </table>

    <ul class="pager">
      <li class="previous">
        {%- do args.update({'p': (f ~ ':' if f) ~ records[0][0], 'action_browse': ''}) -%}
        <a href="{{ url_for('search.search',  **args) }}">
          &larr; {{ _('Previous') }}
        </a>
      </li>
      <li class="next">
        {%- do args.update({'p': (f ~ ':' if f) ~ records[-1][0], 'action_browse': ''}) -%}
        <a href="{{ url_for('search.search',  **args) }}">
          {{ _('Next') }} &rarr;
        </a>
      </li>
    </ul>
  </div>
</div>
{%- endif -%}
{% endblock inner_content %}

{%- endif -%}

