{% load django_listing %}

<form{{ attached_form.attrs }} method="post" enctype="multipart/form-data">
    {% csrf_token %}
    {% gethiddens_listing listing without="page,sort" %}
    {% block hiddens %}
        {% for hidden in listing.form_input_hiddens %}
            <input type="hidden" name="{{ hidden.0 }}" value="{{ hidden.1 }}">{% endfor %}
    {% endblock %}

    {% with attached_form.get_form as form %}

        {% if attached_form.display_errors and form.non_field_errors %}
            <ul class="errorlist nonfield">
            {% for error in form.non_field_errors %}
                <li><span class="listing-icon-attention"></span> {{error}}</li>
            {% endfor %}
            </ul>
        {% endif %}

        {% for row in attached_form.layout %}
            <div class="form-row d-lg-flex">
                {% for field_name in row %}
                    {% get_form_field form field_name as field %}
                    {% if field %}
                        <div class="form-field {{ field_name }}{{ attached_form.theme_field_class }}{% if attached_form.display_errors and field.errors %} errors{% endif %}">
                            {% if attached_form.display_errors and field.errors %}<span class="listing-icon-attention"></span> {{ field.errors }}{% endif %}
                            <div class="label_input">
                                {{ field.label_tag }}{% if field.field.required %}<span class="asteriskField">*</span>{% endif %}
                                {{ field }}
                            </div>
                            {% if ending %}<span class="ending">{{ ending }}</span>{% endif %}
                            {% if field.help_text %}
                                <p class="help">{{ field.help_text|safe }}</p>
                            {% endif %}
                        </div>
                    {% endif %}
                {% endfor %}
            </div>
        {% endfor %}

        <div class="form-buttons">
            {% for action,label,icon,css_class in attached_form.buttons %}
                {% listing_has_permission_for_action listing action as has_button_perm %}
                {% listing_confirm_msg_for_action listing action as confirm_msg %}
                {% listing_confirm_msg_nb_items_for_action listing action as confirm_msg_nb_items %}
                <button type="{% if action == 'reset' %}reset{% elif listing.accept_ajax %}button{% else %}submit{% endif %}"
                        class="attached-form-nav btn btn-primary submit-button {{ action }}{% if not has_button_perm %} no-perm disabled{% endif %}{% if css_class %} {{ css_class }}{% endif %}"
                        {% if confirm_msg %}confirm-msg="{{ confirm_msg }}"{% endif %}
                        {% if confirm_msg_nb_items %}confirm-msg-nb-items="{{ confirm_msg_nb_items }}"{% endif %}
                        {% if action != 'reset' %}name="action_button" value="{{ action }}"{% endif %}>
                    {% if label %}
                        <span class="label">{{ label|safe }}</span>
                    {% endif %}
                    {% if icon %}
                        <span class="icon"><span class="{{ icon }}"></span></span>
                    {% endif %}
                </button>
            {% endfor %}
        </div>
    {% endwith %}
</form>