{% import 'macros/form.html' as form %}

{% set placeholder = placeholder if placeholder else _('Search datasets...') %}
{% set sorting = sorting if sorting else [(_('Name Ascending'), 'name asc'), (_('Name Descending'), 'name desc')] %}
{% set search_class = search_class if search_class else 'search-giant' %}
{% set no_bottom_border = no_bottom_border if no_bottom_border else false %}
{% set form_id = form_id if form_id else false %}

<form {% if form_id %}id="{{ form_id }}" {% endif %}class="search-form{% if no_bottom_border %} no-bottom-border{% endif %}" hx-boost="true" hx-disinherit="hx-boost" method="get" hx-trigger="change, submit">

  {% block search_input %}
    <div class="input-group search-input-group">
      <input aria-label="{% block header_site_search_label %}{{ placeholder }}{% endblock %}" id="field-giant-search" type="text" class="form-control input-lg" name="q" value="{{ query }}" autocomplete="off" placeholder="{{ placeholder }}">
      {% block search_input_button %}
      <button class="btn btn-secondary btn-lg" type="submit" value="search" aria-label="{{_('Submit')}}">
        <i class="fa fa-search"></i>
      </button>
      {% endblock %}
    </div>
  {% endblock %}

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

  {% block search_sortby %}
    {% if sorting %}
      <div class="form-group control-order-by">
        <label for="field-order-by">{{ _('Order by') }}</label>
        <select id="field-order-by" name="sort" class="form-control form-select">
          {% for label, value in sorting %}
            {% if label and value %}
              <option value="{{ value }}"{% if sorting_selected == value %} selected="selected"{% endif %}>{{ label }}</option>
            {% endif %}
          {% endfor %}
        </select>
        {% block search_sortby_button %}
        <button class="btn btn-secondary js-hide" type="submit">{{ _('Go') }}</button>
        {% endblock %}
      </div>
    {% endif %}
  {% endblock %}

  {% block search_title %}
    <h1>
    {% if not error %}
      {% snippet 'snippets/search_result_text.html', query=query, count=count, type=type %}
    {% else %}
      Error
    {% endif %}
    </h1>
  {% endblock %}

  {% block search_facets %}
    {% if facets %}
      <p class="filter-list">
        {% for field in facets.fields %}
          {% set search_facets_items = facets.search.get(field)['items'] if facets.search and field in facets.search else [] %}
          <span class="facet">{{ facets.titles.get(field) }}:</span>
          {% for value in facets.fields[field] %}
            <span class="filtered pill">
              {%- if facets.translated_fields and (field,value) in facets.translated_fields -%}
                {{ facets.translated_fields[(field,value)] }}
              {%- else -%}
                {{ h.list_dict_filter(search_facets_items, 'name', 'display_name', value) }}
              {%- endif %}
              <a href="{{ facets.remove_field(field, value) }}" class="remove" aria-label="{{ _('Remove applied filter: {value}').format(value=value) }}" data-bs-title="{{ _('Remove') }}" data-bs-toggle="tooltip" hx-boost="true"><i class="fa fa-times"></i></a>
            </span>
          {% endfor %}
        {% endfor %}
      </p>
      <a class="show-filters btn btn-secondary">{{ _('Filter Results') }}</a>
    {% endif %}
  {% endblock %}

</form>

{% if show_empty and count == 0 and not error %}
  {% trans %}
    <p class="extra">Please try another search.</p>
  {% endtrans %}
{% endif %}

{% if error %}
  {% trans %}
    <p id="search-error"><strong>There was an error while searching.</strong> Please try again.</p>
  {% endtrans %}
{% endif %}
