{% extends config.kerko.templates.layout %}
{% from "kerko/_attributes.html.jinja2" import title_aria_label %}
{% from "kerko/_breadcrumb.html.jinja2" import breadcrumb %}

{%- set show_print_link = config.kerko.features.print_results_max_count == 0 or total_count <= config.kerko.features.print_results_max_count %}
{%- set show_print_link = show_print_link and config.kerko.features.print_results %}
{%- set show_download_link = config.kerko.features.download_results_max_count == 0 or total_count <= config.kerko.features.download_results_max_count %}
{%- set show_download_link = show_download_link and config.kerko.features.download_results and config.kerko_composer.bib_formats %}
{%- set abstracts_toggler_title = _('Hide abstracts') if show_abstracts else _('Show abstracts') %}

{%- block metas %}
    {{- super() }}
    {%- include "kerko/_search-metas.html.jinja2" %}
{%- endblock metas %}

{%- block breadcrumb %}
    {%- if is_searching %}
        {{ breadcrumb([_("Your search")]) }}
    {%- else %}
        {{ breadcrumb() }}
    {%- endif %}
{%- endblock breadcrumb %}

{%- block content_header %}
    <div class="my-2 row">
        <h1 class="{% block search_heading_classes %}h2 col-auto pr-1 mr-auto d-inline{% endblock %}{% if not is_searching %} d-print-none{% endif %}">
            {%- if is_searching %}
                {{ _("Your search") }}
            {%- else %}
                {{ _("Search") }}
            {%- endif -%}
        </h1>
        {%- if is_searching or facet_results %}
            <div class="col-auto pl-0 ml-auto text-right">
                <div class="{% block help_link_wrapper_classes %}d-inline-block d-print-none mt-1 mb-2{% endblock %}">
                    <button class="{% block help_btn_classes %}btn btn-light{% endblock %}" data-toggle="modal" data-target="#search-help">
                        <span class="fas fa-question-circle" aria-hidden="true"></span> {{ _("Help") }}
                    </button>
                </div>
                {%- if facet_results and search_results %}
                    <div class="{% block facets_link_wrapper_classes %}d-inline-block d-lg-none d-print-none mt-1 mb-2{% endblock %}">
                        <button id="facets-modal-toggle" class="{% block facets_btn_classes %}btn btn-primary{% endblock %}">
                            <span class="fas fa-compass" aria-hidden="true"></span> {{ _("Explore") }}
                        </button>
                    </div>
                {%- endif %}
                {%- if is_searching %}
                    <div class="{% block reset_link_wrapper_classes %}d-inline-block d-print-none mt-1 mb-2{% endblock %}">
                        <a class="{% block reset_btn_classes %}btn btn-danger{% endblock %}" href="{{ url_for('kerko.search') }}">
                            <span class="fas fa-trash-alt" aria-hidden="true"></span> {{ _("Reset search") }}
                        </a>
                    </div>
                {%- endif %}
            </div>
        {%- endif %}
    </div>
    {%- block search_help %}
        {%- include "kerko/_search-help.html.jinja2" %}
    {%- endblock search_help %}
{%- endblock content_header %}

{%- block content_inner %}
    {%- block search_criteria %}
        <div class="{% block search_criteria_wrapper_classes %}card breadbox mb-4{% endblock %}{% if not is_searching %} d-print-none{% endif %}">
            <div class="card-body">
                {%- include "kerko/_breadbox.html.jinja2" %}
                {%- include "kerko/_search-form.html.jinja2" %}
            </div>
        </div>
    {%- endblock search_criteria %}
    {%- block search_results %}
        <div class="my-2 row">
            <div class="col-auto mr-auto">
                <h2 class="{% block results_heading_classes %}d-inline mb-2{% endblock %}">
                    {{- title }}
                    {%- if search_results %}
                        <span class="{% block results_count_classes %}badge badge-light{% endblock %}">{% trans count=total_count, count_formatted=total_count_formatted|safe %}{{ count_formatted }} resource{% pluralize %}{{ count_formatted }} resources{% endtrans %}</span>
                    {%- endif %}
                </h2>
            </div>
            {%- if search_results %}
                <div class="{% block sorter_wrapper_classes %}col-auto pl-0 ml-auto mt-1 mb-2 text-right{% endblock %}">
                    <div class="d-inline-block d-print-none">
                        {%- include "kerko/_sorter.html.jinja2" %}
                    </div>
                </div>
            {%- endif %}
        </div>
        {%- if search_results %}
            {%- if pager or config.kerko.features.results_abstracts_toggler %}
                <div class="row mb-2">
                    {%- if pager %}
                        <div class="col-auto mr-auto">
                            {%- block pager_top %}
                                {%- include "kerko/_pager.html.jinja2" %}
                            {%- endblock pager_top %}
                        </div>
                    {%- endif %}
                    {%- if config.kerko.features.results_abstracts_toggler %}
                        <div class="col-auto ml-auto pl-0">
                            {%- block abstracts_toggler %}
                                <a class="btn btn-light d-print-none" href="{{ abstracts_toggler_url }}" title="{{ abstracts_toggler_title }}" aria-label="{{ abstracts_toggler_title }}">{% if show_abstracts %}<span class="fas fa-toggle-on" aria-hidden="true"></span>{% else %}<span class="fas fa-toggle-off" aria-hidden="true"></span>{% endif %} {{ _("Abstracts") }}</a>
                            {%- endblock abstracts_toggler %}
                        </div>
                    {%- endif %}
                </div>
            {%- endif %}
            {%- block search_results_list %}
                <ul class="mb-4 list-unstyled {% if print_preview %}search-print-preview{% endif %}">
                    {%- for result, result_url in search_results %}
                        {%- include "kerko/_search-result.html.jinja2" %}
                    {%- endfor %}
                </ul>
            {%- endblock search_results_list %}
            {%- block pager_bottom %}
                {%- include "kerko/_pager.html.jinja2" %}
            {%- endblock pager_bottom %}
            {%- block search_results_footer scoped %}
                {%- if show_print_link or show_download_link or atom_feed_url %}
                    <div class="row">
                        <div class="col-auto ml-auto mt-2 mb-4 text-right">
                            {%- if show_print_link %}
                                <div class="{% block print_link_wrapper_classes %}d-none d-md-inline-block d-print-none mb-1{% endblock %}">
                                    <button id="print-link" class="{% block print_btn_classes %}btn btn-light{% endblock %}" type="button" data-url="{{ print_url }}">
                                        <span class="fas fa-print" aria-hidden="true"></span> {% trans count=total_count, count_formatted=total_count_formatted|safe %}Print this citation{% pluralize %}Print {{ count_formatted }} citations{% endtrans %}
                                    </button>
                                </div>
                            {%- endif %}
                            {%- if show_download_link %}
                                <div class="{% block download_link_wrapper_classes %}d-inline-block d-print-none mb-1{% endblock %}">
                                    <div class="dropdown">
                                        <button class="{% block download_btn_classes %}btn btn-light dropdown-toggle{% endblock %}" type="button" id="download-options" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                                            <span class="fas fa-download" aria-hidden="true"></span> {% trans count=total_count, count_formatted=total_count_formatted|safe %}Download this record{% pluralize %}Download {{ count_formatted }} records{% endtrans -%}
                                        </button>
                                        <div class="dropdown-menu dropdown-menu-right dropdown-width-300" aria-labelledby="download-options">
                                            {%- for bib_format in config.kerko_composer.get_ordered_specs('bib_formats') %}
                                                <a class="dropdown-item" href="{{ download_urls[bib_format.key] }}" rel="nofollow" type="{{ bib_format.mime_type|escape }}" {{ title_aria_label(_('Download in {download_option} format').format(download_option=bib_format.label|escape)) }}>
                                                    {{- bib_format.label -}}
                                                </a>
                                                <p class="px-4 text-muted">{{ bib_format.help_text }}</p>
                                            {%- endfor %}
                                        </div>
                                    </div>
                                </div>
                            {%- endif %}
                            {%- if atom_feed_url %}
                                <div class="{% block atom_feed_link_wrapper_classes %}d-inline-block d-print-none mb-1{% endblock %}">
                                    <a class="{% block atom_feed_btn_classes %}btn btn-light{% endblock %}" href="{{ atom_feed_url }}" type="application/atom+xml" {% if atom_feed_help %}title="{{ atom_feed_help|escape }}"{% endif %} target="_blank" rel="noopener">
                                        <span class="fas fa-rss" aria-hidden="true"></span> {{ atom_feed_title }}
                                    </a>
                                </div>
                            {%- endif %}
                        </div>
                    </div>
                {%- endif %}
                <div class="text-right mb-4 text-muted d-print-none">
                    {%- if config.DEBUG %}
                        {% trans time="%.2f"|format(time) %}Processing time: {{ time }} seconds{% endtrans %} &mdash;
                    {%- endif %}
                    {%- if last_sync %}
                        {% trans when=last_sync|kerko_format_datetime(convert_tz=True, show_tz=True) %}Last update from database: {{ when }}{% endtrans %}
                    {%- endif %}
                </div>
            {%- endblock search_results_footer %}
        {%- else %}
            {%- block search_suggestions %}
                <p>{{ _("Suggestions:") }}</p>
                <ul>
                    <li>{{ _("Make sure that all words are spelled correctly.") }}</li>
                    <li>{{ _("Try different words.") }}</li>
                    <li>{{ _("Try more general terms.") }}</li>
                </ul>
            {%- endblock search_suggestions %}
        {%- endif %}
    {%- endblock search_results %}
{%- endblock content_inner %}

{%- block sidebar_inner %}
    {%- block facet_results %}
        {%- if facet_results and search_results %}
            <div id="facets-container" class="d-none d-lg-block d-print-none">
                <h2 class="{% block facets_heading_classes %}mt-2 mb-4{% endblock %}">{{ _("Explore") }}</h2>
                <div id="facets" class="facets">
                    {%- for f in config.kerko_composer.get_ordered_specs('facets') %}
                        {{ f.render(facet_results[f.key], 'search') }}
                    {%- endfor %}
                </div>
            </div>
            <div id="facets-modal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="facets-modal-label" aria-hidden="true">
                <div class="modal-dialog modal-lg modal-dialog-scrollable" role="document">
                    <div class="modal-content">
                        <div class="modal-header">
                            {%- block facets_modal_header %}
                                <h2 id="facets-modal-label" class="modal-title">{{ _("Explore") }}</h2>
                                <button type="button" class="btn btn-secondary" data-dismiss="modal" {{ title_aria_label(_('Close Explore panel')) }}>
                                    <span class="fas fa-times" aria-hidden="true"></span>
                                </button>
                            {%- endblock facets_modal_header %}
                        </div>
                        <div id="facets-modal-body" class="modal-body">
                            {%- block facets_modal_inner %}
                                <div class="my-3 text-right">
                                    <button type="button" class="btn btn-secondary" data-dismiss="modal" aria-label="{{ _('Close Explore panel') }}">{{ _("Close") }}</button>
                                </div>
                            {%- endblock facets_modal_inner %}
                        </div>
                    </div>
                </div>
            </div>
        {%- endif %}
    {%- endblock facet_results %}
{%- endblock sidebar_inner %}

{%- block body_scripts %}
    {{- super() }}
    {%- block kerko_search_js %}
        <script src="{{ url_for('kerko.static', filename='kerko/js/search.js') }}?20221229"></script>
    {%- endblock kerko_search_js %}
    {%- block kerko_print_js %}
        {%- if show_print_link %}
            <script src="{{ url_for('kerko.static', filename='kerko/js/print.js') }}"></script>
        {%- endif %}
    {%- endblock kerko_print_js %}
{%- endblock body_scripts %}
