{% apply spaceless %}
    {#
        Default Formie Template

        Any templates for forms, pages and fields contained in this directory will
        be used instead of the default formie template. You can delete any template
        file not prefixed by an underscore, and the default template for that component
        will be used instead.

        Warning: By overriding template files, you will no longer receive bug fixes
        and improvements. If breaking changes are introduced, you will need to update
        your own templates. For more information on how to customize templates without
        overriding template files, please refer to the Formie documentation:
        https://verbb.io/craft-plugins/formie/docs/template-guides/theming
    #}

    {% formtag 'formWrapper' %}
        {% set showForm = form.isAvailable() %}
        {% set currentPage = form.getCurrentPage() %}
        {% set errors = submission.getErrors('form') ?? null %}

        {% set submitted = craft.formie.plugin.service.getFlash(form.id, 'submitted') %}
        {% set flashNotice = craft.formie.plugin.service.getFlash(form.id, 'notice') %}
        {% set flashError = craft.formie.plugin.service.getFlash(form.id, 'error') %}

        {% if submission is defined and form.settings.submitAction == 'message' %}
            {% if flashNotice and form.settings.submitActionMessagePosition == 'top-form' %}
                {{ formieInclude('_includes/alert-success') }}
            {% endif %}

            {% if flashError and form.settings.errorMessagePosition == 'top-form' %}
                {{ formieInclude('_includes/alert-error') }}
            {% endif %}
        {% endif %}

        {{ formieInclude('_includes/messages') }}

        {% if submitted and form.settings.submitAction == 'message' and form.settings.submitActionFormHide %}
            {% set showForm = false %}
        {% endif %}

        {% if showForm %}
            {% formtag 'form' %}
                {{ csrfInput({ autocomplete: 'off' }) }}
                {{ actionInput(form.getActionUrl()) }}
                {{ hiddenInput('submitAction', 'submit') }}
                {{ hiddenInput('handle', form.handle) }}
                {{ hiddenInput('siteId', craft.app.sites.currentSite.id) }}

                {% if form.getSessionKey() %}
                    {{ hiddenInput('sessionKey', form.getSessionKey()) }}
                {% endif %}

                {% if form.getRelations() %}
                    {{ hiddenInput('relations', form.getRelations()) }}
                {% endif %}

                {% if form.getPopulatedFieldValues() %}
                    {{ hiddenInput('extraFields', form.getPopulatedFieldValues()) }}
                {% endif %}

                {% if submission and submission.id %}
                    {{ hiddenInput('submissionId', submission.id) }}
                {% endif %}

                {% if form.isEditingSubmission() %}
                    {{ hiddenInput('editingSubmission', true) }}
                {% endif %}

                {% if form.getStorageBehaviour() != 'session' %}
                    {{ hiddenInput('storage', form.getStorageBehaviour()) }}
                    {{ hiddenInput('pageIndex', form.getCurrentPageIndex()) }}
                {% endif %}

                {% if form.getRedirectUrl() %}
                    {{ redirectInput(form.getRedirectUrl()) }}
                {% endif %}

                {% set customInputs = renderOptions.customInputs ?? {} %}

                {% for inputName, inputValue in customInputs %}
                    {{ hiddenInput(inputName, inputValue) }}
                {% endfor %}

                {% formtag 'formContainer' %}
                    {% hook 'formie.form.start' %}

                    {% if form.settings.displayFormTitle %}
                        {{ formieInclude('_includes/form-title') }}
                    {% endif %}

                    {% if form.settings.progressPosition == 'start' %}
                        {{ formieInclude('_includes/progress') }}
                    {% endif %}

                    {% if errors %}
                        {{ formieInclude('_includes/form-errors') }}
                    {% endif %}

                    {{ formieInclude('_includes/page-tabs') }}

                    {% for page in form.getPages() %}
                        {{ craft.formie.renderPage(form, page, renderOptions) }}
                    {% endfor %}

                    {% if form.settings.progressPosition == 'end' %}
                        {{ formieInclude('_includes/progress') }}
                    {% endif %}

                    {% hook 'formie.form.end' %}
                {% endformtag %}
            {% endformtag %}
        {% endif %}

        {# When the position of success message is set to bottom, but the form is hidden... #}
        {% if submission is defined and form.settings.submitAction == 'message' and form.settings.submitActionFormHide %}
            {% if flashNotice and form.settings.submitActionMessagePosition == 'bottom-form' %}
                {{ formieInclude('_includes/alert-success') }}
            {% endif %}
        {% endif %}
    {% endformtag %}
{% endapply %}
