{% load i18n l10n wagtailadmin_tags %}

{{ self.formset.management_form }}

{% if self.formset.non_form_errors %}
    <div class="error-message">
        {% for error in self.formset.non_form_errors %}
            <span>{{ error|escape }}</span>
        {% endfor %}
    </div>
{% endif %}

{% if self.help_text %}
    {% help_block status="info" %}{{ self.help_text }}{% endhelp_block %}
{% endif %}

<div id="id_{{ self.formset.prefix }}-FORMS">
    {% comment %}

    Child elements of this div will become orderable elements. Do not place additional
    "furniture" elements here unless you intend them to be part of the child ordering.

    {% endcomment %}

    {% for child in self.children %}
        {% include "wagtailadmin/panels/inline_panel_child.html" %}
    {% endfor %}
</div>

<script type="text/django-form-template" id="id_{{ self.formset.prefix }}-EMPTY_FORM_TEMPLATE">
    {% escapescript %}
        {% include "wagtailadmin/panels/inline_panel_child.html" with child=self.empty_child %}
    {% endescapescript %}
</script>

{# Align with guiding line of the preceding child panel. #}
<div class="w-mb-4 -w-ml-0.5">
    <button type="button" class="button button-small button-secondary chooser__choose-button" id="id_{{ self.formset.prefix }}-ADD">
        {% icon name=icon|default:"plus-inverse" %}{% blocktrans trimmed with label=self.label|lower %}Add {{ label }}{% endblocktrans %}
    </button>
</div>

<script>
    (function() {
        var panel = InlinePanel({
            formsetPrefix: "id_{{ self.formset.prefix }}",
            emptyChildFormPrefix: "{{ self.empty_child.form.prefix }}",
            canOrder: {% if can_order %}true{% else %}false{% endif %},
            maxForms: {{ self.formset.max_num|unlocalize }}
        });

        {% for child in self.children %}
            panel.initChildControls("{{ child.form.prefix }}");
        {% endfor %}
        panel.updateChildCount();
        panel.updateMoveButtonDisabledStates();
        panel.updateAddButtonState();
    })();
</script>
