{% extends 'geomanager/modeladmin/index_without_custom_create.html' %}
{% load i18n modeladmin_tags static adminsortable_tags %}


{% block result_list %}
    {% if not all_count %}
        <div class="nice-padding" style="margin-top:30px;">
            {% if no_valid_parents %}
                <p>{% blocktrans with view.verbose_name_plural as name %}No {{ name }} have been created yet. One of the
                    following must be created before you can add any {{ name }}:{% endblocktrans %}</p>
                <ul>
                    {% for type in required_parent_types %}
                        <li><b>{{ type|title }}</b></li>{% endfor %}
                </ul>
            {% else %}
                <p>{% blocktrans with view.verbose_name_plural as name %}No {{ name }} have been created
                    yet.{% endblocktrans %}
                    {% if user_can_create %}
                        {% blocktrans with view.create_url as url %}
                            Why not <a href="{{ url }}">add one</a>?
                        {% endblocktrans %}
                    {% endif %}</p>
            {% endif %}
        </div>
    {% else %}
        {% sortable_result_list %}
        {% if model_admin.sortable %}
            {% csrf_token %}
            <script type="text/javascript">
                $(document).ready(function () {
                    var ordering = {};
                    var orderingField = '{{ model_admin.ordering_field }}';
                    $('#sortable').sortable({
                        items: 'tr',
                        axis: 'y',
                        scroll: true,
                        cursor: 'ns-resize',
                        containment: $('#sortable tbody'),
                        tolerance: 'pointer',
                        stop: function (event, dragged_rows) {

                            $('#sortable tbody tr').each(function (index) {
                                ordering[$(this).attr('data-object-pk')] = index
                            });

                            var objects = [];
                            for (const [pk, order] of Object.entries(ordering)) {
                                objects.push({'pk': pk, [orderingField]: order})
                            }
                            if (objects) {
                                $.post('{{ view.index_url }}', {
                                        'objects': JSON.stringify(objects),
                                        'csrfmiddlewaretoken': $('input[name="csrfmiddlewaretoken"]').val()
                                    },
                                    function (response) {
                                        console.log(response)
                                    });
                            }
                        }
                    });
                    $('#sortable, tbody, tr, td, th').disableSelection();
                });
            </script>
        {% endif %}
    {% endif %}
{% endblock %}