{% load i18n static %}


<!-- Stylesheet connection -->
{% block stylesheet %}
<link rel="stylesheet" href="{% static 'django_admin_search/css/styles.css' %}">
{% endblock %}

{% block modal %}

 <form id="advanced_admin_search" class="modal" style="display: none;">
        
        <div class="advanced_search_form_body">
            {% block form_fields %}
                {% include "admin/custom_fieldset.html" %}
            {% endblock %}
        </div>

        {% if show_result_count %}
            <span class="small quiet">{% blocktrans count counter=cl.result_count %}{{ counter }} result{% plural %}{{ counter }} 
                                      results{% endblocktrans %} (<a href="?{% if cl.is_popup %}_popup=1{% endif %}">
                                      {% if cl.show_full_result_count %}{% blocktrans with full_result_count=cl.full_result_count %}
                                      {{ full_result_count }} total{% endblocktrans %}{% else %}{% trans "Show all" %}{% endif %}</a>)
            </span>
        {% endif %}
        {% for pair in cl.params.items %}
            {% if pair.0 != search_var %}<input type="hidden" name="{{ pair.0 }}" value="{{ pair.1 }}"/>{% endif %}
        {% endfor %}
        
        {% block modal_search_button %}
        <button id="submit_form" type="submit" class="form_submit_button">
            {% block modal_search_button_text %}
                {% trans 'filter'|title %}
            {% endblock %}
        </button>
        {% endblock %}

    </form>

{% endblock %}

{% block search_admin_button %}
    <div class="search_button">
        <!-- Link to open the modal -->
        <a href="#advanced_admin_search" rel="modal:open" class="search_button">
            <img src="{% static "admin/img/search.svg" %}" alt="Search" height="15"/> 
            <span>
                {% block search_admin_button_text %}
                    {% trans 'advanced search'|title %}
                {% endblock %}
            </span>
        </a>
    </div>
{% endblock %}


<!-- Scripts -->
{% block scripts %}
    <!-- jQuery -->
    <script src="{% static 'django_admin_search/jquery/jquery.min.js' %}"></script>

    <!-- JQuery Mask-->
    <script src="{% static 'django_admin_search/jquery_mask/jquery.mask.min.js' %}"></script>

    <!-- jQuery Modal -->
    <script src="{% static 'django_admin_search/jquery_modal/jquery.modal.min.js' %}"></script>
    <link rel="stylesheet" href="{% static 'django_admin_search/jquery_modal/jquery.modal.min.css' %}" />

    <!-- Submit Button (Get parameters)-->
    <script>
        let searchParams = new URLSearchParams(window.location.search)
        
        page = searchParams.get('p')
        page = page == null ? 0 : ( $.isNumeric(page) ? parseInt(page) : 0 );
        
        $( "#advanced_admin_search" ).submit(function() {
            $('#advanced_admin_search input').each(function(k, v) {
                v.disabled = v.value === '';
            });
            
            params = $('#advanced_admin_search').serialize()

            /* 
                
                after apply filter is more common to leave user to the first page
                because a data length is changed, current page may no longer exist 
                (Ex, filter return empty queryset, nos has second page).
                but if your need to keep a page, uncomment this
                if (page != 0) { // this main current page
                    // params = 'p=' + page + '&' + params;
                }
            */
            
            href = window.location.pathname
            window.location.href = href + '?' + params;
            return false;
        });
    </script>

{% endblock %}
