{% extends "admin/base_site.html" %}
{% load i18n admin_urls static admin_list %}

{% block extrastyle %}
    {{ block.super }}
    <link rel="stylesheet" href="{% static 'adminlte/plugins/select2/css/select2.min.css' %}">

    {% if cl.formset or action_form %}
        <script type="text/javascript" src="{% url 'admin:jsi18n' %}"></script>
    {% endif %}
    {{ media.css }}
    {% if not actions_on_top and not actions_on_bottom %}
        <style>
            #changelist table thead th:first-child {width: inherit}
        </style>
    {% endif %}
{% endblock %}

{% block extrahead %}
    {{ block.super }}
    {{ media.js }}
{% endblock %}

{% block bodyclass %}{{ block.super }} app-{{ opts.app_label }} model-{{ opts.model_name }} change-list{% endblock %}

{% if not is_popup %}
    {% block breadcrumbs %}
        <ol class="breadcrumb float-sm-right">
            <li class="breadcrumb-item"><a href="{% url 'admin:index' %}"><i class="fa fa-tachometer-alt"></i> {% trans 'Home' %}</a></li>
            <li class="breadcrumb-item"><a href="javascript:void(0)">{{ cl.opts.app_config.verbose_name }}</a></li>
            <li class="breadcrumb-item active">{{ cl.opts.verbose_name_plural|capfirst }}</li>
        </ol>
    {% endblock %}
{% endif %}

{% block content_title %} {{ cl.opts.verbose_name_plural|capfirst }} {% endblock %}

{% block coltype %}flex{% endblock %}

{% block content %}

    <div class="col-12">
        <div class="card card-primary card-outline">
            <div class="card-header">
                <h4 class="card-title">{{ title }}{% block pretitle %}{% endblock %}</h4>
                <div class="card-tools form-inline">
                    {% block date_hierarchy %}{% if cl.date_hierarchy %}{% date_hierarchy cl %}{% endif %}{% endblock %}
                    {% block search %}
                        {% search_form cl %}
                    {% endblock %}
                </div>
            </div>

            <div class="card-body">
                <div id="content-main">
                    {% if cl.formset and cl.formset.errors %}
                        <p class="errornote">
                            {% if cl.formset.total_error_count == 1 %}
                                {% trans "Please correct the error below." %}
                            {% else %}
                                {% trans "Please correct the errors below." %}
                            {% endif %}
                        </p>
                        {{ cl.formset.non_form_errors }}
                    {% endif %}
                    <div class="module{% if cl.has_filters %} filtered{% endif %}" id="changelist">
                        <div class="row">
                            <div class="col-12">
                                <form id="changelist-form" method="post"{% if cl.formset and cl.formset.is_multipart %}enctype="multipart/form-data"{% endif %} novalidate>{% csrf_token %}
                                    {% if cl.formset %}
                                        <div>{{ cl.formset.management_form }}</div>
                                    {% endif %}

                                    {% block result_list %}
                                        <div class="row">
                                            <div class=col-8>
                                                {% if action_form and actions_on_top and cl.show_admin_actions %}
                                                    {% admin_actions %}
                                                {% endif %}
                                            </div>
                                            <div class=col-4>
                                                {% block object-tools %}
                                                    {% block object-tools-items %}
                                                        {% change_list_object_tools %}
                                                    {% endblock %}
                                                {% endblock %}
                                            </div>
                                        </div>
                                        <hr/>
                                        {% result_list cl %}
                                        {% if action_form and actions_on_bottom and cl.show_admin_actions %}
                                            <div class="row">
                                                <div class=col-12>
                                                    {% admin_actions %}
                                                </div>
                                            </div>
                                        {% endif %}
                                    {% endblock %}
                                </form>
                            </div>
                        </div>
                        <div class="row">
                            {% block pagination %}{% pagination cl %}{% endblock %}
                        </div>
                    </div>
                </div>
            </div>

        </div>
        <br class="clear"/>
    </div>
{% endblock %}

{% block extrajs %}
    <script type="text/javascript" src="{% static 'adminlte/plugins/select2/js/select2.min.js' %}"></script>
    <script>
        $.fn.search_filters = function () {
            $(this).change(function () {
                var $field = $(this);
                var $option = $field.find('option:selected');
                var select_name = $option.data('name');
                if (select_name) {
                    $field.attr('name', select_name);
                } else {
                    $field.removeAttr('name');
                }
            });
            $(this).trigger('change');
        };

        var $ele = $('.search-filter');
        $ele.search_filters();
        $ele.select2();
    </script>
{% endblock %}
