{% extends "freeform/_layouts/settings" %}

{% import "_includes/forms" as forms %}

{% set crumbs = [
    { label: craft.freeform.name, url: url("freeform") },
    { label: "Settings"|t("freeform"), url: url("freeform/settings") },
] %}

{% set title = "Form Behavior"|t("freeform") %}

{% block content %}

    <input type="hidden" name="action" value="freeform/settings/save-settings">
    {{ redirectInput('freeform/settings/form-behavior') }}
    {{ csrfInput() }}

    <h2 class="first">{{ "Submit Behavior"|t('freeform') }}</h2>

    {{ forms.lightswitchField({
        label: "Disable Submit Button on Form Submit"|t('freeform'),
        instructions: "Enable this to automatically disable the form's submit button when the form is submitted. This will prevent the form from double-submitting."|t('freeform'),
        name: "settings[formSubmitDisable]",
        on: settings.formSubmitDisable,
        errors: settings.errors("formSubmitDisable"),
    }) }}

    {{ forms.lightswitchField({
        label: "Automatically Scroll to Form on Errors and Multipage forms"|t('freeform'),
        instructions: "Enable this to have Freeform use JS to automatically scroll the page down to the form upon submit when there are errors or the form is continuing to the next page in multipage forms."|t('freeform'),
        name: "settings[autoScrollToErrors]",
        on: settings.autoScrollToErrors,
        errors: settings.errors("autoScrollToErrors"),
    }) }}

    {{ forms.lightswitchField({
        label: "Automatically Scroll to top of the Form on AJAX submit"|t('freeform'),
        instructions: "Enable this when using AJAX to have Freeform use JS to automatically scroll the top of the form on submit. This is especially beneficial when you have longer forms and success/error messages at the top of the form become out of sight."|t('freeform'),
        name: "settings[autoScroll]",
        on: settings.autoScroll,
        errors: settings.errors("autoScroll"),
    }) }}

    {{ forms.lightswitchField({
        label: "Remember the Page Order in Multi-page forms"|t('freeform'),
        instructions: "When enabled, Freeform will take into account Conditional Rules page skipping when the user clicks the 'Previous' button on multi-page forms."|t('freeform'),
        name: "settings[rememberPageSubmitOrder]",
        on: settings.rememberPageSubmitOrder,
        errors: settings.errors("rememberPageSubmitOrder"),
    }) }}

    {% if (craft.app.config.general.asyncCsrfInputs) %}
        {{ forms.selectField({
            label: "CSRF Token Refresh Method"|t('freeform'),
            instructions: "You can control how CSRF tokens refresh by choosing between refreshing once per page view (recommended) or on every AJAX request. This helps ensure secure, automatic token handling even when using page caching."|t('freeform'),
            name: "settings[csrfRefresh]",
            value: settings.csrfRefresh,
            errors: settings.errors("csrfRefresh"),
            options: {
                "once": "Once Per Page View (recommended)"|t('freeform'),
                "always": "Always on Each Request"|t('freeform'),
                "none": "Never"|t('freeform'),
            },
        }) }}
    {% endif %}

    <hr>

    <h2 class="first">{{ "Form Processing"|t('freeform') }}</h2>

    {{ forms.lightswitchField({
        label: "Use Queue for Email Notifications"|t('freeform'),
        instructions: "Use Craft's queue system to trigger emails. Enabling this will speed up submission processing for your users."|t('freeform'),
        name: "settings[useQueueForEmailNotifications]",
        on: settings.useQueueForEmailNotifications,
        errors: settings.errors("useQueueForEmailNotifications"),
        warning: "This feature relies on either regular traffic to your Craft control panel or implementing a proper queue-processing method that does not depend on someone visiting your control panel. <a class='go' href='https://craftcms.com/docs/5.x/system/queue.html' rel='noopener' target='_blank'>Learn more</a>"|t('freeform'),
    }) }}

    {{ forms.lightswitchField({
        label: "Use Queue for Integrations"|t('freeform'),
        instructions: "Use Craft's queue system to trigger integrations (CRM and Email Marketing). Enabling this will speed up submission processing for your users."|t('freeform'),
        name: "settings[useQueueForIntegrations]",
        on: settings.useQueueForIntegrations,
        errors: settings.errors("useQueueForIntegrations"),
        warning: "This feature relies on either regular traffic to your Craft control panel or implementing a proper queue-processing method that does not depend on someone visiting your control panel. <a class='go' href='https://craftcms.com/docs/5.x/system/queue.html' rel='noopener' target='_blank'>Learn more</a>"|t('freeform'),
    }) }}

    <hr>

    <h2>{{ "Freeform Scripts"|t('freeform') }}</h2>

    {{ forms.selectField({
        label: "Script Insert Location"|t('freeform'),
        instructions: "The location of where you want Freeform to insert its scripts for form and field functionality."|t('freeform'),
        name: "settings[scriptInsertLocation]",
        value: settings.scriptInsertLocation,
        errors: settings.errors("scriptInsertLocation"),
        options: {
            "footer": "Page Footer (recommended)"|t('freeform'),
            "header": "Page Header"|t('freeform'),
            "form": "Inside Form"|t('freeform'),
            "manual": "None (add manually)"|t('freeform'),
        },
    }) }}

    {{ forms.selectField({
        label: "Script Insert Type"|t('freeform'),
        instructions: "Specify the way Freeform scripts are inserted."|t('freeform'),
        name: "settings[scriptInsertType]",
        value: settings.scriptInsertType,
        errors: settings.errors("scriptInsertType"),
        options: {
            "files": "Asset Bundles (recommended)"|t('freeform'),
            "inline": "Inline"|t('freeform'),
        },
    }) }}

    <hr>

    <h2>{{ "Form Sessions"|t('freeform') }}</h2>

    {% if settings.sessionContext != "payload" %}

        {{ forms.selectField({
            label: "Freeform Session Context"|t('freeform'),
            instructions: "Choose the way form context is passed between form submits."|t('freeform'),
            id: 'session-context',
            name: "settings[sessionContext]",
            value: settings.sessionContext,
            errors: settings.errors("sessionContext"),
            options: {
                "payload": "As an encrypted payload (recommended)"|t('freeform'),
                "session": "Using PHP's sessions"|t('freeform'),
                "database": "Using a database table"|t('freeform'),
            },
        }) }}

    {% else %}

        {{ forms.hidden({
            id: 'session-context',
            name: "settings[sessionContext]",
            value: settings.sessionContext,
        }) }}

    {% endif %}

    <div id="session-time" class="field {{ settings.sessionContext not in ["session", "database"] ? "hidden" }}">
        {{ forms.autosuggestField({
            class: "code",
            label: "Session length in minutes"|t('freeform'),
            instructions: "Specify when the sessions should expire. Set to 0 for unlimited."|t('freeform'),
            name: "settings[sessionContextTimeToLiveMinutes]",
            value: settings.sessionContextTimeToLiveMinutes,
            errors: settings.getErrors("sessionContextTimeToLiveMinutes"),
            suggestEnvVars: true,
        }) }}
    </div>

    <div id="session-count" class="field {{ settings.sessionContext not in ["session", "database"] ? "hidden" }}">
        {{ forms.autosuggestField({
            class: "code",
            label: "Amount of active forms per user"|t('freeform'),
            instructions: "Specify the amount of active forms a user can have."|t('freeform'),
            name: "settings[sessionContextCount]",
            value: settings.sessionContextCount,
            errors: settings.getErrors("sessionContextCount"),
            suggestEnvVars: true,
        }) }}
    </div>

    <div id="session-secret"  class="field {{ settings.sessionContext not in ["payload"] ? "hidden" }}">
        {{ forms.autosuggestField({
            class: "code",
            label: "Secret key"|t('freeform'),
            instructions: "Specify a secret key to encrypt the form payload with. Will use server key if left empty."|t('freeform'),
            name: "settings[sessionContextSecret]",
            value: settings.sessionContextSecret,
            errors: settings.getErrors("sessionContextSecret"),
            suggestEnvVars: true,
        }) }}
    </div>


    {% if craft.freeform.pro %}

        <hr>

        <h2>{{ "Save & Continue Later"|t('freeform') }}</h2>

        {{ forms.selectField({
            label: "Number of Days to Keep Saved Form Data"|t('freeform'),
            instructions: "The number of days to store saved form progress in the database before clearing."|t('freeform'),
            name: "settings[saveFormTtl]",
            value: settings.saveFormTtl,
            errors: settings.errors("saveFormTtl"),
            options: {
                0: "Please select"|t("freeform"),
                1: "1 day"|t("freeform"),
                2: "2 days"|t("freeform"),
                3: "3 days"|t("freeform"),
                4: "4 days"|t("freeform"),
                5: "5 days"|t("freeform"),
                6: "6 days"|t("freeform"),
                7: "7 days"|t("freeform"),
                14: "14 days"|t("freeform"),
                30: "30 days"|t("freeform"),
                60: "60 days"|t("freeform"),
                90: "90 days"|t("freeform"),
                120: "120 days"|t("freeform"),
                180: "180 days"|t("freeform"),
                365: "365 days"|t("freeform"),
            }
        }) }}

        {{ forms.textField({
            label: "Maximum Number of Saved Forms Per Session"|t('freeform'),
            instructions: "The maximum number of saved forms per session (per user, though it's possible a user may have multiple sessions when returning to the site over a span of several days). Default is 10, which should be a comfortable and reasonable number. Once the maximum has been reached, Freeform will begin overwriting existing older entries to create new ones."|t('freeform'),
            name: "settings[saveFormSessionLimit]",
            value: settings.saveFormSessionLimit,
            errors: settings.errors("saveFormSessionLimit"),
            placeholder: "Leave empty or 0 for unlimited amount."|t('freeform'),
        }) }}

    {% endif %}


    <div id="script-insert-warning" class="hidden">
        {{- "Please ensure that you manually load Freeform's JS and CSS using the 'freeform.loadScripts()' function in your template(s)."|t('freeform') -}}
    </div>

{% endblock %}
