{% if resultSet.totalHits > 0 %}

    {% set route = app.request.attributes.get('_route') %}
    {% set route_parameters = app.request.attributes.get('_route_params')|merge(app.request.query.all) %}

    {% set criteria = app.request.query.get('criteria', {}) %}

    {% set default_path = path(route, route_parameters|merge({'sorting': null, 'criteria': criteria})) %}
    {% set from_a_to_z_path = path(route, route_parameters|merge({'sorting': {'name': 'asc'}, 'criteria': criteria})) %}
    {% set from_z_to_a_path = path(route, route_parameters|merge({'sorting': {'name': 'desc'}, 'criteria': criteria})) %}
    {% set oldest_first_path = path(route, route_parameters|merge({'sorting': {'created_at': 'asc'}, 'criteria': criteria})) %}
    {% set newest_first_path = path(route, route_parameters|merge({'sorting': {'created_at': 'desc'}, 'criteria': criteria})) %}
    {% set cheapest_first_path = path(route, route_parameters|merge({'sorting': {'price': 'asc'}, 'criteria': criteria})) %}
    {% set most_expensive_first_path = path(route, route_parameters|merge({'sorting': {'price': 'desc'}, 'criteria': criteria})) %}

    {% if app.request.query.get('sorting') is empty %}
        {% set current_sorting_label = 'sylius.ui.by_position'|trans %}
        {% set current_sorting_active = default_path %}
    {% elseif app.request.query.get('sorting').name is defined and app.request.query.get('sorting').name == 'asc'%}
        {% set current_sorting_label = 'sylius.ui.from_a_to_z'|trans %}
        {% set current_sorting_active = from_a_to_z_path %}
    {% elseif app.request.query.get('sorting').name is defined and app.request.query.get('sorting').name == 'desc'%}
        {% set current_sorting_label = 'sylius.ui.from_z_to_a'|trans %}
        {% set current_sorting_active = from_z_to_a_path %}
    {% elseif app.request.query.get('sorting').created_at is defined and app.request.query.get('sorting').created_at == 'desc'%}
        {% set current_sorting_label = 'sylius.ui.newest_first'|trans %}
        {% set current_sorting_active = newest_first_path %}
    {% elseif app.request.query.get('sorting').created_at is defined and app.request.query.get('sorting').created_at == 'asc'%}
        {% set current_sorting_label = 'sylius.ui.oldest_first'|trans %}
        {% set current_sorting_active = oldest_first_path %}
    {% elseif app.request.query.get('sorting').price is defined and app.request.query.get('sorting').price == 'asc'%}
        {% set current_sorting_label = 'sylius.ui.cheapest_first'|trans %}
        {% set current_sorting_active = cheapest_first_path %}
    {% elseif app.request.query.get('sorting').price is defined and app.request.query.get('sorting').price == 'desc' %}
        {% set current_sorting_label = 'sylius.ui.most_expensive_first'|trans %}
        {% set current_sorting_active = most_expensive_first_path %}
    {% else %}
        {% set current_sorting_label = '-----------' %}
        {% set current_sorting_active = default_path %}
    {% endif %}

    <div class="listing-sort">
        <div class="listing-sort-mobile">
            <div class="row">
                {# Bouton filtrer #}
                <div class="col-6">
                    <div class="collapse-group">
                        <button class="btn btn-primary btn-sm btn-block" type="button" data-collapse="filters-wrapper">
                            {{ 'sylius.ui.filter'|trans }}
                        </button>
                    </div>
                </div>
                {# Bouton sort #}
                <div class="col-6">
                    <div class="collapse-group">
                        <button class="btn btn-outline-primary btn-sm btn-block" type="button" data-collapse="listing-sort-dropdown">
                            {{ 'sylius.ui.sort'|trans }}
                        </button>
                    </div>
                </div>
            </div>

            <div class="collapse-panel" id="listing-sort-dropdown">
                <a class="dropdown-item {% if current_sorting_active == default_path %}active{% endif %}" href="{{ default_path }}">{{ 'sylius.ui.by_position'|trans }}</a>
                <a class="dropdown-item {% if current_sorting_active == from_a_to_z_path %}active{% endif %}" href="{{ from_a_to_z_path }}">{{ 'sylius.ui.from_a_to_z'|trans }}</a>
                <a class="dropdown-item {% if current_sorting_active == from_z_to_a_path %}active{% endif %}" href="{{ from_z_to_a_path }}">{{ 'sylius.ui.from_z_to_a'|trans }}</a>
                <a class="dropdown-item {% if current_sorting_active == newest_first_path %}active{% endif %}" href="{{ newest_first_path }}">{{ 'sylius.ui.newest_first'|trans }}</a>
                <a class="dropdown-item {% if current_sorting_active == oldest_first_path %}active{% endif %}" href="{{ oldest_first_path }}">{{ 'sylius.ui.oldest_first'|trans }}</a>
                <a class="dropdown-item {% if current_sorting_active == cheapest_first_path %}active{% endif %}" href="{{ cheapest_first_path }}">{{ 'sylius.ui.cheapest_first'|trans }}</a>
                <a class="dropdown-item {% if current_sorting_active == most_expensive_first_path %}active{% endif %}" href="{{ most_expensive_first_path }}">{{ 'sylius.ui.most_expensive_first'|trans }}</a>
            </div>
        </div>
        <div class="listing-sort-desktop">
            <label>{{ 'sylius.ui.sort'|trans }} :</label>
            <select class="selectpicker form-control" title="{{ current_sorting_label }}" onchange="window.location.href=this.value">
                <option value="{{ default_path }}">{{ 'sylius.ui.by_position'|trans }}</option>
                <option value="{{ from_a_to_z_path }}">{{ 'sylius.ui.from_a_to_z'|trans }}</option>
                <option value="{{ from_z_to_a_path }}">{{ 'sylius.ui.from_z_to_a'|trans }}</option>
                <option value="{{ newest_first_path }}">{{ 'sylius.ui.newest_first'|trans }}</option>
                <option value="{{ oldest_first_path }}">{{ 'sylius.ui.oldest_first'|trans }}</option>
                <option value="{{ cheapest_first_path }}">{{ 'sylius.ui.cheapest_first'|trans }}</option>
                <option value="{{ most_expensive_first_path }}">{{ 'sylius.ui.most_expensive_first'|trans }}</option>
            </select>
        </div>
    </div>
{% endif %}
