{% 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 %}

    {{ parent() }}

    <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.booleanMenuField({
        includeEnvVars: true,
        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]",
        value: settings.formSubmitDisable,
        errors: settings.errors("formSubmitDisable"),
        disabled: readOnly,
    }) }}

    {{ forms.booleanMenuField({
        includeEnvVars: true,
        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]",
        value: settings.autoScrollToErrors,
        errors: settings.errors("autoScrollToErrors"),
        disabled: readOnly,
    }) }}

    {{ forms.booleanMenuField({
        includeEnvVars: true,
        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]",
        value: settings.autoScroll,
        errors: settings.errors("autoScroll"),
        disabled: readOnly,
    }) }}

    {{ forms.booleanMenuField({
        includeEnvVars: true,
        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]",
        value: settings.rememberPageSubmitOrder,
        errors: settings.errors("rememberPageSubmitOrder"),
        disabled: readOnly,
    }) }}

    {{ forms.booleanMenuField({
        includeEnvVars: true,
        label: "Prevent Duplicate Submissions (Idempotency Keys)"|t('freeform'),
        instructions: "When enabled, Freeform generates a unique token for each form instance to prevent duplicate submissions and actions. If the same data is submitted multiple times in quick succession, only the first submission is processed. This helps avoid duplicate actions, especially for payment or AI-powered forms."|t('freeform'),
        name: "settings[useIdempotencyKey]",
        value: settings.useIdempotencyKey,
        errors: settings.errors("useIdempotencyKey"),
        disabled: readOnly,
    }) }}

    {% 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"),
            disabled: readOnly,
            options: {
                "once": "Once Per Page View (recommended)"|t('freeform'),
                "always": "Always on Each Request"|t('freeform'),
                "none": "Never"|t('freeform'),
            },
        }) }}
    {% endif %}

    <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"),
        disabled: readOnly,
        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"),
        disabled: readOnly,
        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"),
            disabled: readOnly,
            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"),
            disabled: readOnly,
            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"),
            disabled: readOnly,
            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"),
            disabled: readOnly,
            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"),
            disabled: readOnly,
            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"),
            disabled: readOnly,
            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 %}
