{% load i18n %}
{% load get_field %}
<form class="form form-vertical crd-form"
      method="post"
      {% if form_action %}action="{{ form_action }}"{% endif %}>
    {% csrf_token %}
    {% include "frontend/forms/errors.html" %}
    <ul class="nav nav-tabs" id="myTab" role="tablist">
        {% for fieldset in form.get_fieldsets %}
            {% if not fieldset.hidden %}
                <li class="nav-item" role="presentation">
                    <button class="nav-link {% if forloop.first %}active{% endif %}{% if fieldset.has_mandatory %} has-mandatory{% endif %}"
                            id="{{ fieldset.title|slugify }}-tab"
                            data-bs-toggle="tab"
                            data-bs-target="#{{ fieldset.title|slugify }}"
                            type="button"
                            role="tab"
                            aria-controls="{{ fieldset.title|slugify }}"
                            aria-selected="{% if forloop.first %}true{% else %}false{% endif %}">
                        {{ fieldset.title }}
                        {% if fieldset.has_mandatory %}<span class="mandatory-indicator">*</span>{% endif %}
                    </button>
                </li>
            {% endif %}
        {% endfor %}
    </ul>
    <div class="tab-content" id="myTabContent">
        {% for fieldset in form.get_fieldsets %}
            <div class="tab-pane fade my-2 {% if fieldset.hidden %}d-none{% endif %}{% if forloop.first %}show active{% endif %}"
                 id="{{ fieldset.title|slugify }}"
                 role="tabpanel"
                 aria-labelledby="{{ fieldset.title|slugify }}-tab">
                {% for field in fieldset.fields %}
                    {% with field=form|get_field:field %}{{ field.as_field_group }}{% endwith %}
                {% endfor %}
                {% for subfieldset in fieldset.fieldsets %}
                    {% if subfieldset.fields %}
                        <h4 class="mt-3">{{ subfieldset.title }}</h4>
                        {% for field in subfieldset.fields %}
                            {% with field=form|get_field:field %}{{ field.as_field_group }}{% endwith %}
                        {% endfor %}
                    {% endif %}
                {% endfor %}
            </div>
        {% endfor %}
    </div>
    <div class="col-sm-12 d-flex justify-content-end">
        <button class="btn btn-primary me-1 mb-1" type="submit">
            {% if form_submit_label %}
                {{ form_submit_label }}
            {% else %}
                {% translate "Save" %}
            {% endif %}
        </button>
    </div>
</form>
