{% extends "newman/ajax_content_base.html" %}
{% load admin_list newman_list cache %}

{% block breadcrumbs %}
    {% if not is_popup %}
        <div class="breadcrumbs">
            <div style="float: left;">
                <a href="{% url newman:index %}#">{% trans "Home" %}</a>
                {# % if app_label %}&rsaquo; <a class="js-hashadr" href="/{{ app_label }}/">{% trans app_label.title %}</a>{% endif % #}
                {% if title %}&rsaquo; <strong id="doc-title">{{ title }}</strong>{% endif %}
            </div>
        </div>
    {% endif %}
{% endblock %}

{% spaceless %}
{% block content_toolbar_left %}
    <div id="filters-handler">
        {% if cl.has_filters %}
                {% if is_filtered %}
                    <a class="icn btn filter filter-active {% if is_popup %}popup-filter{% else %}js-simpleload{% endif %}" href="filters::::filters/{{ cl.get_query_string }}">{% trans 'Filters' %}<span> ({% trans 'on!' %})</span></a>
                {% else %}
                    <a class="icn btn filter {% if is_popup %}popup-filter{% else %}js-simpleload{% endif %}" href="filters::::filters/{{ cl.get_query_string }}">{% trans 'Filters' %}</a>
                {% endif %}
        {% endif %}
        {% comment %}
        {% if is_user_category_filtered %}
            <a style="background-color: orange;" class="icn btn visible def" href="{% url newman:index %}#/nm/">{% trans "Categories filtered by user. Click to change." %}</a>
        {% endif %}
        {% endcomment %}
        {% if cl.search_fields %}
            {% block search %}{% newman_search_form cl %}{% endblock %}
        {% endif %}
    </div>
{% endblock %}
{% block content_toolbar_right %}
    {% if has_add_permission %}
            <a href="{% if is_popup %}::overlay-content::{% endif %}add/" class="js-{% if is_popup %}adrstack-push{% else %}hashadr{% endif %} icn btn add">{% blocktrans with cl.opts.verbose_name as name %}Add {{ name }}{% endblocktrans %}</a>
    {% endif %}
    {% if not is_popup %}
        <a href="service-container::::log/" class="icn btn history js-simpleload">{% trans "Last edited" %}</a>
    {% endif %}
{% endblock %}
{% endspaceless %}

{% block content_services %}
    {{ block.super }}
    <div id="filters"></div>

        {% if is_filtered %}
            {# Active filters listing #}
            <div id="id-active-filters" class="active-filters">
                <span class="filtered-by">{% trans "Filtered by" %}:</span>
                {% for active_filter in cl.active_filters %}
                        <span class="filter-name">{{ active_filter.0 }}:</span>
                        <span class="filter-value">{{ active_filter.1 }}</span>
                        {% if not forloop.last %}
                        &nbsp;,&nbsp;
                        {% endif %}
                {% endfor %}
                {% if is_user_category_filtered %}
                    {% if cl.active_filters %}
                        &nbsp;,&nbsp;
                    {% endif %}
                    <span class="filter-name">{% trans "Main categories filtered." %}</span>
                {% endif %}
            </div>
        {% else %}
            {% if is_user_category_filtered %}
                <div id="id-active-filters" class="active-filters">
                    <span class="filtered-by">{% trans "Filtered by" %}:</span>
                    {% if cl.active_filters %}
                        &nbsp;,&nbsp;
                    {% endif %}
                    <span class="filter-name">{% trans "Main categories filtered." %}</span>
                </div>
            {% endif %}
        {% endif %}
        
{% endblock %}

{% if cl.formset.errors %}
    <p class="errornote">
    {% blocktrans count cl.formset.errors|length as counter %}Please correct the error below.{% plural %}Please correct the errors below.{% endblocktrans %}
    </p>
    <ul class="errorlist">{% for error in cl.formset.non_field_errors %}<li>{{ error }}</li>{% endfor %}</ul>
{% endif %}

{% block content %}
    <div class="js-hashadr-container js-app-{{ cl.opts }}" id="changelist">

        <form class="js-form js-dyn-adr" action="" method="post"{% if cl.formset.is_multipart %} enctype="multipart/form-data"{% endif %}>
            {% csrf_token %}
            {% if cl.formset %}
                {{ cl.formset.management_form }}
            {% endif %}

            {% ifnotequal cl.paginator.num_pages 1 %}{% pagination cl %}{% endifnotequal %}
            {% block result_list %}
                {# % if actions_on_top and cl.full_result_count and not is_popup %}{% admin_actions %}{% endif % #}
                {% if action_form and actions_on_top and cl.full_result_count %}{% admin_actions %}{% endif %}
                {% newman_result_list cl %}
                {% if action_form and actions_on_bottom and cl.full_result_count %}{% admin_actions %}{% endif %}
            {% endblock %}
            {% block pagination %}{% pagination cl %}{% endblock %}
            <div class="js-form-metadata">
                <input type="hidden" name="success" id="changelist-batch-success" />
                <script type="text/javascript">
                    $('#changelist-batch-success').data('callback', changelist_batch_success);
                </script>
            </div>
        </form>

    </div>
{% endblock %}

{% block content_js %}
    {{ block.super }}

    {% if media %}
        {% cache 300 'nm-cl-js' cl %}
            <script type="text/javascript">
            {% for m in media %}
                request_media('{{ m }}');
            {% endfor %}

            function trigger_changelist_shown() {
                $(document).trigger('changelist_shown'); // bound in newman.js
            }
            $(document).one('media_loaded', trigger_changelist_shown);
        </script>
        {% endcache %}
    {% endif %}
{% endblock %}
