{% macro sort(field, title_header=None) -%}
    {% set sort = request.args.get('sort', None) %}
    {% set direction = request.args.get('direction', 'asc') %}
    {% set q = request.args.get('q', '') %}
    {% set genreSort = request.args.get('genreSort', None) %}
    {% set status = request.args.get('status', None) %}

    {% if title_header == None %}
        {% set label = field | title %}
    {% else %}
        {% set label = title_header %}
    {% endif %}

    {% if sort == field and direction == 'asc' %}
        {% set direction = 'desc' %}
    {% else %}
        {% set direction = 'asc' %}
    {% endif %}

    {% if sort == field %}
        {% set css = direction %}
    {% else %}
        {% set css = 'sort' %}
    {% endif %}

    <a class="{{ css }}" href="{{ url_for(request.endpoint,
                                        q=q,
                                        sort=field,
                                        direction=direction,
                                        genreSort=genreSort,
                                        status=status,
                                        **request.view_args) }}">{{ label }}</a>
{%- endmacro %}

{# Paginate through a resource. #}
{% macro paginate(resource) -%}
    {% set args = request.args.to_dict() %}
    <nav class="md-margin-top " style="margin-bottom: 100px">
        <ul class="pagination margin-center">
            <li class="page-item {{ 'disabled' if resource.page == 1 }}">
                <a href="{{ url_for(request.endpoint, page=1, **args) }}"
                   aria-label="First" class="page-link">
                    &laquo; First
                </a>
            </li>
            <li class="page-item {{ 'disabled' if not resource.has_prev }}">
                <a href="{{ url_for(request.endpoint, page=resource.page - 1, **args) }}"
                   aria-label="Previous" class="page-link">
                    Prev
                </a>
            </li>
            {%- for page in resource.iter_pages() %}
                <li class="page-item {{ 'active' if page and page == resource.page }}">
                    {% if page %}
                        {% if page != resource.page %}
                            <a href="{{ url_for(request.endpoint, page=page, **args) }}"
                               class="page-link">{{ page }}</a>
                        {% else %}
                            <span class="page-link">{{ page }}</span>
                        {% endif %}
                    {% else %}
                        <span class="page-link">…</span>
                    {% endif %}
                </li>
            {%- endfor %}
            <li class="page-item {{ 'disabled' if not resource.has_next }}">
                <a href="{{ url_for(request.endpoint, page=resource.page + 1, **args) }}"
                   class="page-link"
                   aria-label="Next">
                    Next
                </a>
            </li>
            <li class="page-item {{ 'disabled' if resource.page == resource.pages or resource.pages == 0 }}">
                <a href="{{ url_for(request.endpoint, page=resource.pages, **args) }}"
                   class="page-link"
                   aria-label="Last">
                    Last &raquo;
                </a>
            </li>
        </ul>
    </nav>
{%- endmacro %}
