{% do view.registerAssetBundle('Solspace\\Freeform\\Resources\\Bundles\\FormattingTemplates\\Foundation6Bundle') %}

{% import "freeform/_templates/formatting/foundation-6/_row.twig" as rowMacro %}

{# Render the opening form tag #}
{{ form.renderTag({
    attributes: {
        form: { class: "freeform-form", "data-freeform-foundation": true },
        row: { class: "grid-x grid-margin-x" },
        success: { class: "callout success" },
        errors: { class: "callout alert" },
        novalidate: true,
    },
    buttons: {
        attributes: {
            container: { class: "cell submit-buttons" },
            submit: { class: "button primary" },
            back: { class: "button secondary" },
            save: { class: "button primary" },
        },
    },
    fields: {
        "@global": {
            attributes: {
                container: { class: "cell" },
                label: { class: "freeform-label" },
                input: {
                    class: "freeform-field",
                },
                instructions: { class: "help-text" },
                error: { class: "no-bullet freeform-errors" },
            },
        },
        ":required": {
            attributes: {
                label: { "+class": "required" },
            },
        },
        ":errors": {
            attributes: {
                input: { "+class": "has-errors" },
            },
        },
        "@group": {
            attributes: {
                label: { "+class": "h6" },
            },
        },
        "@signature": {
            attributes: {
                input: { "+class": "button small secondary" },
            },
        },
    },
}) }}

{# Success and error message handling for non-AJAX forms #}
{% if not form.settings.ajax %}
    {% if form.submittedSuccessfully %}
        <div{{ form.attributes.success|raw }}>
            <p>{{ form.successMessage | t('freeform') }}</p>
        </div>
    {% endif %}
    {% if form.hasErrors %}
        <div{{ form.attributes.errors|raw }}>
            <p>{{ form.errorMessage | t('freeform') }}</p>

            {% if form.errors|length %}
                <ul class="mb-0">
                    {% for error in form.errors %}
                        <li>{{ error }}</li>
                    {% endfor %}
                </ul>
            {% endif %}
        </div>
    {% endif %}
{% endif %}

{# Render page tabs if multi-page #}
{% if form.pages|length > 1 %}
    <ul class="menu freeform-page-tabs">
        {% for page in form.pages %}
            <li class="menu-text{{ form.currentPage.index == page.index ? ' is-active' : '' }}">
                {{ page.label }}
            </li>
        {% endfor %}
    </ul>
{% endif %}

{# Display form field rows and columns #}
{{ rowMacro.render(form.rows, form) }}

{# Render the closing form tag #}
{{ form.renderClosingTag }}
