{% import "macros/form.html" as form %}
{% set sort_name = collection.name ~ ":sort" %}
{% set sort_value = collection.params.sort %}

{% block table %}
    <table id="{{ collection.serializer.table_id }}" class="table collection-table">

        {% block header %}
            <tr class="collection-table--row collection-table--header">
                {% block header_rows %}

                    {% for col in collection.columns.names if col in collection.columns.visible %}
                        {% set is_sortable = col in collection.columns.sortable %}
                        {% set primary_order = collection.columns.get_primary_order(col) %}
                        {% set secondary_order = collection.columns.get_secondary_order(col) %}

                        {% set is_current_order = sort_value == col %}

                        <td class="collection-table--cell
                                   {{- ' collection-table--cell-sortable' if is_sortable }}
                                   {{- ' collection-table--cell-sortable--primary-order' if sort_value == primary_order }}
                                   {{- ' collection-table--cell-sortable--secondary-order' if sort_value == secondary_order }}">
                            {% block header_cell scoped %}
                                {% if is_sortable %}
                                    <a class="sort-trigger"
                                       href="{{ h.remove_url_param(sort_name, replace=secondary_order if sort_value == primary_order else primary_order) }}">
                                        {{ collection.columns.labels[col] or col }}
                                    </a>
                                {% else %}
                                    <span>{{ collection.columns.labels[col] or col }}</span>
                                {% endif %}
                            {% endblock header_cell %}

                        </td>
                    {% endfor %}

                {% endblock header_rows %}
            </tr>
        {% endblock header %}

        {% block body %}
            {% for record in collection %}
                {% snippet collection.serializer.record_template, record=record, collection=collection %}
            {% endfor %}

        {% endblock body %}
    </table>
{% endblock table %}
