{% load widget_tweaks %}

{% load i18n staticfiles arctic_tags %}

    <div class="row">
        {% if has_filter %}
            <form action="" method="GET" class="columns expand">
                <div class="page-filters row">
                    {% include "arctic/partials/form_fields_inline.html" with form=filter.form hide_label=True %}
                    <div class="columns shrink left">
                        <button type="submit" class="button secondary">
                            <i class="fa fa-search"></i>
                        </button>
                    </div>
                </div>
            </form>
        {% endif %}
        {% if tool_links %}
            <div class="columns {% if has_filter %}shrink{% endif %}">
                {% for link in tool_links %}
                    {% if tool_links|length > 1 or not tool_links.0.icon %}
                        <button class="button secondary float-right" type="button" data-toggle="tool-links"><i class="fa fa-wrench"></i></button>
                        <div class="dropdown-pane bottom" id="tool-links" data-dropdown data-close-on-click="true">
                            <ul>
                                <li><a href="{% url link.url %}">{{ link.label }}</a></li>
                            </ul>
                        </div>
                    {% else %}
                        <a class="button secondary" title="{{ link.label }}" href="{% url link.url %}"><i class="fa {{ link.icon }}"></i></a>
                    {% endif %}
                {% endfor %}
            </div>
        {% endif %}
    </div>

    <div class="row">
    {# data table #}
    {% if list_items %}
        <div class="small-12 columns">
            <div class="table-scroll">
            <table class="table">
                <thead>
                    <tr>
                        {% for item in list_header %}
                            <th>
                                {% if item.order_url %}
                                    <a href="{{ item.order_url }}">{{ item.label }}
                                        {% if item.order_direction == 'desc' %}
                                            <i class="fa fa-sort-desc"></i>
                                        {% elif item.order_direction == 'asc' %}
                                            <i class="fa fa-sort-asc"></i>
                                        {% else %}
                                            <i class="fa fa-sort"></i>
                                        {% endif %}</a>
                                {% else %}
                                    {{ item.label }}
                                {% endif %}
                            </th>
                        {% endfor %}

                        {% if action_links %}
                            <th class="highlight-background">{% trans "Actions" %}</th>
                        {% endif %}
                    </tr>
                </thead>
                <tbody>
                    {% for item in list_items %}
                        <tr>
                            {% for v in item|slice:"1:" %}
                                {% if v|typename == 'ManyRelatedManager' or v|typename == 'RelatedManager' %}
                                    <td>
                                        {% for item in v.all %}
                                            <span class="badge highlight-background">{{ item }}</span>
                                        {% endfor %}
                                    </td>
                                {% else %}

                                    {% if list_header|index:forloop.counter0|get_item:'class' %}
                                        <td class="{{ list_header|index:forloop.counter0|get_item:'class' }}">
                                    {% else %}
                                        <td>
                                    {% endif %}

                                    {% if list_header|index:forloop.counter0|get_item:'link' %}
                                        {% if v.pk %}
                                            <a href="{% arctic_url list_header|index:forloop.counter0|get_item:'link' v.pk %}">
                                        {% else %}
                                            <a href="{% arctic_url list_header|index:forloop.counter0|get_item:'link' item.0 %}">
                                         {% endif %}
                                            {{ v|default_if_none:""|safe|linebreaks }}
                                        </a>
                                    {% else %}
                                        {% if v|typename == 'datetime' or v|typename == 'date' %}
                                            {{ v|default_if_none:"" }}
                                        {% else %}
                                            {{ v |default_if_none:"" | safe | linebreaks }}
                                        {% endif %}
                                    {% endif %}
                                        </td>
                                {% endif %}
                            {% endfor %}

                    {% if action_links %}
                        <td class="actions">
                            {% for link in action_links %}
                                <a href="{% arctic_url link.url item.0 %}" class="action-{{ link.label }}" title="{{ link.label|capfirst }}">
                                    {% if link.icon %}
                                        <i class="fa {{ link.icon }} fa-lg"></i>
                                    {% else %}
                                        {{ link.label|capfirst }}
                                    {% endif %}
                                </a>
                                &nbsp;
                            {% endfor %}
                            </td>
                        {% endif %}
                        </tr>
                    {% endfor %}
                </tbody>
            </table>

            {# modal iframe dialog #}
            <section id="reveal-iframe" class="reveal external" data-reveal>
                <iframe frameborder="0" allowfullscreen></iframe>
            </section>

            </div> {# table-scroll #}
        </div> {# small-12 columns #}
    </div> {# row #}
    {% block pagination %}
        {% with page_obj=page_obj paginator=page_obj.paginator %}
            {% include "arctic/partials/pagination.html" %}
        {% endwith %}
    {% endblock %}
{% endif %}
