{% extends "page.html" %}
{% import 'macros/form.html' as form %}

{% block subtitle %}{{ _("Search for a Dataset") }}{% endblock %}

{% block add_action_content %}
  {% if h.check_access('package_create') %}
    {% link_for _('Add Dataset'), controller='package', action='new', class_='btn btn-primary', icon='plus-sign-alt' %}
  {% endif %}
{% endblock %}

{% block breadcrumb_content %}
  <li class="active">{{ h.nav_link(_('Datasets'), controller='package', action='search', highlight_actions = 'new index') }}</li>
{% endblock %}

{% block primary_content %}
  <section class="module">
    <div class="module-content">
      <form id="dataset-search" class="dataset-search clearfix" method="get" data-module="select-switch">
        <span class="control-group search-giant">
          <input type="text" class="search" name="q" value="{{ c.q }}" autocomplete="off" placeholder="{{ _('Search...') }}" />
          <button type="submit" value="search">{{ _('Submit') }}</button>
        </span>

        {{ h.snippet('snippets/sort_by.html', sort=c.sort_by_selected) }}

        {% if c.fields -%}
          <span>
            {{ form.hidden_from_list(fields=c.fields) }}
          </span>
        {%- endif %}

        <div id="dataset-search-ext">{% block dataset_search_ext %}{% endblock %}</div>
      </form>
      <div class="results">
        <strong>
          {% snippet 'snippets/search_result_text.html', query=c.q, count=c.page.item_count, type='dataset' %}
        </strong>
        <div class="filter-list">
          {% for field in c.fields_grouped %}
            {% set search_facets_items = c.search_facets.get(field)['items'] %}
            <span class="facet">{{ c.facet_titles.get(field) }}:</span>
            {% for value in c.fields_grouped[field] %}
              <span class="filtered pill">
                {%- if c.translated_fields and c.translated_fields.has_key((field,value)) -%}
                  {{ c.translated_fields[(field,value)] }}
                {%- else -%}
                  {{ h.list_dict_filter(search_facets_items , 'name', 'display_name', value) }}
                {%- endif %}
                <a href="{{ c.remove_field(field, value) }}" class="remove" title="{{ _('Remove') }}"><i class="icon-remove"></i></a>
              </span>
            {% endfor %}
          {% endfor %}
        </div>
        {% if request.params and c.page.item_count == 0 %}
          {% trans %}
            <p class="extra">Please try another search.</p>
          {% endtrans %}
        {% endif %}
      </div>

      {% if c.query_error %}
        {% trans %}
          <p><strong>There was an error while searching.</strong> Please try again.</p>
        {% endtrans %}
      {% endif %}
      {{ h.snippet('snippets/package_list.html', packages=c.page.items) }}
    </div>

    {{ c.page.pager(q=c.q) }}
  </section>

  <section class="module">
    <div class="module-content">
      <small>
        {% set api_link = h.link_to(_('API'), h.url_for(controller='api', action='get_api', ver=3)) %}
        {% set api_doc_link = h.link_to(_('API Docs'), 'http://docs.ckan.org/{0}/{1}/api.html'.format(request.environ.CKAN_LANG, g.ckan_doc_version)) %}
        {% if g.dumps_url -%}
          {% set dump_link = h.link_to(_('full {format} dump').format(format=g.dumps_format), g.dumps_url) %}
          {% trans %}
            You can also access this registry using the {{ api_link }} (see {{ api_doc_link }}) or download a {{ dump_link }}.
          {% endtrans %}
        {% else %}
          {% trans %}
            You can also access this registry using the {{ api_link }} (see {{ api_doc_link}}).
          {% endtrans %}
        {%- endif %}
      </small>
    </div>
  </section>
{% endblock %}

{% block secondary_content %}
  {% for facet in c.facet_titles %}
      {{ h.snippet('snippets/facet_list.html', title=c.facet_titles[facet], name=facet) }}
  {% endfor %}
{% endblock %}
