{#
 # One or zero ModelForm with one or multiple one to many related inline formsets.
 # For barebone raw version, see bs_inline_formsets.htm.
 #}
{% macro bs_inline_formsets(related_form, formsets, action, opts) -%}
{% if not formsets|is_iterable -%}
    {{ raise('"formsets" argument should be iterable collection of formsets') }}
{% endif -%}
{% set wrapper_atts = {
    'class': 'formsets default-padding'
} -%}
{% if not opts -%}
    {% set opts = {} -%}
{% endif -%}
{% if opts.class -%}
    {% do tpl.add_css_classes_to_dict(wrapper_atts, opts.class) -%}
{% endif -%}
{% if action == '' -%}
    {% do tpl.add_css_classes_to_dict(wrapper_atts, 'display-only') -%}
{% endif -%}
{% do opts.update({'table_classes': 'table table-bordered table-collapse'}) -%}
<card-default {{ tpl.json_flatatt(wrapper_atts) }}>
    {% if opts.title is defined -%}
    <card-header>
        <card-title>{{ opts.title }}</card-title>
    </card-header>
    {% endif -%}
    {% if caller -%}
        {{ caller({'ff_heading': related_form, 'opts': opts}) }}
    {% endif -%}
    {% if action != '' -%}
        {% if opts.is_ajax -%}
            {% set atts = {'data-url': action, 'class': 'ajax-form'} -%}
        {% else -%}
            {% set atts = {'action': action} -%}
        {% endif -%}
        {% do atts.update({
            'method': 'post',
            'enctype': "multipart/form-data",
            'role': 'form'
        }) -%}
    <form{{ tpl.json_flatatt(atts) }}>
        <input type="hidden" name="csrfmiddlewaretoken" value="{{ csrf_token }}">
    {% endif -%}
        {% if related_form -%}
            {{ render_form(request, 'related', related_form, {
                'action': action,
                'caller': caller,
                'opts': opts,
            }) }}
        {% endif -%}
        {% for formset in formsets -%}
            {% if formset.inline_title is defined -%}
                {% do opts.update({'inline_title': formset.inline_title}) -%}
            {% else -%}
                {% do opts.update({'inline_title': formset.model._meta.verbose_name}) -%}
            {% endif -%}
            {% do formset.djk_renderer.update_context({
                'action': action,
                'caller': caller,
                'opts': opts,
                'loop': loop,
            }) -%}
            {{ formset.djk_renderer() }}
        {% endfor -%}
        {% if opts.submit_text is defined -%}
        <button type="submit" class="btn btn-primary">{{ opts.submit_text }}</button>
        {% endif -%}
        {% if caller -%}
            {{ caller({'buttons': True}) }}
        {% endif -%}
    {% if action != '' -%}
    </form>
    {% endif -%}
    {% for formset in formsets -%}
        {% if formset.knockout_template -%}
        <textarea class="ko-template">{{ formset.knockout_template }}</textarea>
        {% endif -%}
    {% endfor -%}
</card-default>
{% endmacro -%}
{% if _render_ -%}
    {% if not isinstance(_render_, dict) -%}
        {% set _render_ = {
            'related_form': form,
            'formsets': formsets,
            'action': action,
            'opts': opts,
        } -%}
    {% endif -%}
    {% call(kwargs) bs_inline_formsets(**_render_) -%}
    {% endcall -%}
{% endif -%}
