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

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

{% 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"),
    }) }}

    <hr>

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

    {{ forms.selectField({
        label: "Freeform Script Insertion 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"|t('freeform'),
            "header": "Page Header"|t('freeform'),
            "form": "Inside Form"|t('freeform'),
            "manual": "None (add manually)"|t('freeform'),
        },
    }) }}

    {% set canUsePointers = craft.freeform.isPossibleLoadingStaticScripts() %}

    {{ forms.selectField({
        label: "Freeform Script Insert Type"|t('freeform'),
        instructions: "Specify the way Freeform scripts are inserted."|t('freeform'),
        warning: not canUsePointers and settings.scriptInsertType == "pointers" ? "It seems like your server configuration prevents static URL files to be used. Please choose another method."|t("freeform"),
        tip: "<b>As Static URLs</b> (recommended) offers built-in browser caching and a static endpoint that delivers the latest Freeform scripts, but are not physical files and therefore cannot have server rules applied. <b>As Files</b> (recommended if having issues with Static URLs) are actual physical files generated inside of the 'cpresources' folder by Craft's asset manager with no caching. <b>Inline</b> will add the scripts as inline scripts and may be useful as a workaround if the other 2 options are not working. This doesn't allow for caching, and pollutes the source code."|t('freeform'),
        name: "settings[scriptInsertType]",
        value: settings.scriptInsertType,
        errors: settings.errors("scriptInsertType"),
        options: {
            "pointers": "As Static URLs"|t('freeform') ~ (canUsePointers ? " (recommended)" : "")|t('freeform'),
            "files": "As Files"|t('freeform') ~ (not canUsePointers ? " (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">
        {{- "With 'None' chosen, please be sure to manually load Freeform's JS and CSS with the 'freeform.loadFreeformPlugin()' function in your template(s)."|t('freeform') -}}
    </div>

{% endblock %}
