{#
# This file is part of Invenio.
# Copyright (C) 2012, 2014, 2015 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.
#}

{% extends "search/searchbar_frame.html" %}

{% from "search/results.html" import render_search_results with context %}

{% block global_bundles %}
  {{ super() }}
  {% bundles "formatter.css" %}
{% endblock %}

{%- 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() }}
  {%- 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({'jrec': jrec}) -%}
  {%- 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({'jrec': jrec}) -%}
  <link rel="next" href="{{ url_for(request.endpoint, **args) }}"/>
  {%- endif -%}
{% endblock %}

{%- block inner_content -%}
  {%- block index -%}
    {%- if not records|length -%}
      {%- block index_noresult -%}
        <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>
      {%- endblock index_noresult -%}
    {%- else -%}
      {%- block index_results -%}
        <div class="row">
          {%- block facets %}
            <div class="col-md-3 col-lg-2 visible-md visible-lg" id="facetparent">
              <div class="facet" id="facet_list" data-clampedwidth="#facetparent">
              {% for facet_name, data in response._search()['aggregations'].items() %}
              <div class="panel panel-default">
                <div class="panel-heading">{{ _(facet_name) }}</div>
                <table class="table table-condensed">
                {% for bucket in data['buckets'] %}
                <tr>
                  <td>
                    <div class="checkbox">
                      <label>
                          <input class="include-facet"
                                 {{ 'checked' if facet_name in filtered_facets
                                 and bucket['key'] in filtered_facets[facet_name]['inc']}}
                                 type="checkbox"
                                 name="{{ facet_name }}" value="{{ bucket['key'] }}">
                          {{ bucket['key'] }}
                      </label>
                    </div>
                  </td>
                  <td>
                    <span class="badge pull-right">{{ bucket['doc_count'] }}</span>
                  </td>
                </tr>
                {% endfor %}
                </table>
              </div>
              {% endfor %}
              </div>
            </div>
          {%- endblock facets %}
          {%- block index_right -%}
            <div class="col-md-9 col-lg-10">
              {%- block facets_filter %}
                <div class="row visible-md visible-lg">
                  <div class="col-md-10">
                    <div class="well" id="facet_filter" style="clear:both; display: none;">
                      <input type="hidden" value="{{ request.args.get('p', '') }}" name="p" />
                    </div>
                  </div>
                </div>
              {%- endblock facets_filter %}
              {%- block results %}
                <div class="row">
                  <div id="search_results" class="col-md-12">
                    {%- block search_results -%}
                      {{ render_search_results(records, collection, pagination, format_record) }}
                    {%- endblock search_results -%}
                  </div>
                </div>
              {%- endblock results %}
            </div>
          {%- endblock index_right -%}
        </div>
        {%- block filter_modal -%}
          <div class="modal hide" id="overlay">
            <div class="modal-body text-center">
              <img alt="" src="/img/loading.gif" id="img-load" />
              <strong>{{ _('Filtering') }} &hellip;</strong>
            </div>
          </div>
        {%- endblock filter_modal -%}
      {%- endblock index_results -%}
    {%- endif -%}
  {%- endblock index -%}
{%- endblock -%}

{% block javascript %}
  {{ super() }}

  <script type="text/javascript">
    require(["jquery",
             "js/search/facets_filter",
             "js/search/search",
             "js/search/query_filters_widget",
             "js/search/facets_menu"],
             function($,
                 FacetsFilter,
                 Search,
                 SearchQueryFiltersWidget,
                 FacetsMenu) {
      var facetsFilter = new FacetsFilter({{ filtered_facets | tojson | safe }});
      var context = {
        searchUrl: '{{ url_for('search.search') }}',
        urlArgs: {{ request.args.to_dict() | tojson | safe }},
        facetsMenuSelector: '.facet',
        searchFormSelector: '#searchform',
        searchQueryFiltersSelector: '#search-query-filters',
        searchQueryFiltersTextSelector: '.search-query-filters-text',
        searchQueryFiltersEditSelector: '.search-query-filters-edit',
        searchResultsSelector: '#search_results',
        facetsFilter: facetsFilter,
      }
      FacetsMenu.attachTo(context.facetsMenuSelector, context);
      SearchQueryFiltersWidget.attachTo(context.searchQueryFiltersSelector, context);
      Search.attachTo(document, context);
      $(document).trigger("initSearch");
    });
  </script>

{% endblock javascript %}
