{% 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 = "General Settings"|t("freeform") %}

{% block content %}

    {{ parent() }}

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

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

    {% if craft.freeform.pro %}

        {{ forms.textField({
            class: "code",
            label: "Custom Plugin Name"|t('freeform'),
            instructions: "Override the plugin name by specifying your own here."|t('freeform'),
            placeholder: craft.freeform.name,
            id: "pluginName",
            name: "settings[pluginName]",
            value: settings.pluginName,
            errors: settings.getErrors("pluginName"),
            disabled: readOnly,
        }) }}

    {% endif %}

    {{ forms.selectField({
        label: "Default View"|t('freeform'),
        instructions: "The default page to go to when clicking the Freeform nav item."|t('freeform'),
        name: "settings[defaultView]",
        value: settings.defaultView,
        errors: settings.errors("defaultView"),
        disabled: readOnly,
        options: {
            forms: "Forms"|t('freeform'),
            submissions: "Submissions"|t('freeform'),
        },
    }) }}

    {{ forms.selectField({
        label: "Plugin Badge"|t('freeform'),
        instructions: "Select the options you'd like to be included in the plugin badge count (when applicable)."|t('freeform'),
        name: "settings[badgeType]",
        value: settings.badgeType,
        errors: settings.errors("badgeType"),
        disabled: readOnly,
        options: {
            '': "Don't Show"|t('freeform'),
            all: "Update Notices & Logged Errors"|t('freeform'),
            notices: "Update Notices only"|t('freeform'),
            errors: "Logged Errors only"|t('freeform'),
            submissions: "Submission Count"|t('freeform'),
            spam: "Spam Folder Count"|t('freeform'),
        },
    }) }}

    {% if craft.freeform.pro %}

        {{ forms.booleanMenuField({
            includeEnvVars: true,
            label: "Site-Aware Forms"|t('freeform'),
            instructions: "When enabled, forms become aware of the currently active Site when loaded on the front end. In the control panel, you can filter form lists by Site and prevent other admins from accessing forms that belong to Sites they don't have access to. By default, any new forms created will be visible for the Site they were created on (with the ability to enable additional sites)."|t('freeform'),
            name: "settings[sitesEnabled]",
            value: settings.sitesEnabled,
            errors: settings.errors("sitesEnabled"),
            disabled: readOnly,
            tip: "To enable translations on forms per Site, go to the form builder and enable the 'Translatable' setting under the 'Settings' tab."|t('freeform'),
        }) }}

    {% endif %}

    {{ forms.textField({
        label: "Queue Job Priority"|t('freeform'),
        instructions: "Set the queue job priority for background Freeform tasks to determine if they should run with a different priority compared to other jobs. Jobs with a lower priority are executed first."|t('freeform'),
        name: "settings[queuePriority]",
        value: settings.queuePriority,
        errors: settings.errors("queuePriority"),
        disabled: readOnly,
        placeholder: "Leave empty to use the Craft default of 1024."|t('freeform'),
    }) }}

    {{ forms.selectField({
        label: "Logging Level"|t('freeform'),
        instructions: "Set the logging level for Freeform. 'Errors' logs only errors, 'Info' logs information about what's been processed, and 'Debug' logs all processes occurring, which is useful for setup and debugging."|t('freeform'),
        name: "settings[loggingLevel]",
        value: settings.loggingLevel,
        errors: settings.errors("loggingLevel"),
        disabled: readOnly,
        options: {
            error: "Errors (recommended)"|t('freeform'),
            info: "Info"|t('freeform'),
            debug: "Debug"|t('freeform'),
        },
    }) }}

    <hr>

    <h2>{{ "Element Field Type"|t('freeform') }}</h2>

    {{ forms.booleanMenuField({
        includeEnvVars: true,
        label: "Restrict Form options in Form Field Type to User permissions"|t('freeform'),
        instructions: "Enable this to only show forms the user or group has permissions to manage in the list of options for the Form Element Field Type (relating forms in Entries, etc)."|t('freeform'),
        name: "settings[formFieldShowOnlyAllowedForms]",
        value: settings.formFieldShowOnlyAllowedForms,
        errors: settings.errors("formFieldShowOnlyAllowedForms"),
        disabled: readOnly,
    }) }}

    <hr>

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

    {{ forms.booleanMenuField({
        includeEnvVars: true,
        label: "Remove Newlines from Textareas for Exporting"|t('freeform'),
        instructions: "Enable this to have newlines removed from Textarea fields in submissions when exporting."|t('freeform'),
        name: "settings[removeNewlines]",
        value: settings.removeNewlines,
        errors: settings.errors("removeNewlines"),
        disabled: readOnly,
    }) }}

    {{ forms.booleanMenuField({
        includeEnvVars: true,
        label: "Use Option Labels when Exporting"|t('freeform'),
        instructions: "Enable this to have fields with options use the submission's option labels instead of values when exporting."|t('freeform'),
        name: "settings[exportLabels]",
        value: settings.exportLabels,
        errors: settings.errors("exportLabels"),
        disabled: readOnly,
    }) }}

    {{ forms.booleanMenuField({
        includeEnvVars: true,
        label: "Use Field Handles for Headings when Exporting"|t('freeform'),
        instructions: "Enable this to use field handles as headings instead of field labels when exporting submissions."|t('freeform'),
        name: "settings[exportHandlesAsNames]",
        value: settings.exportHandlesAsNames,
        errors: settings.errors("exportHandlesAsNames"),
        disabled: readOnly,
    }) }}

    {% if craft.freeform.pro %}

        {{ forms.booleanMenuField({
            includeEnvVars: true,
            label: "Include Tracked URL Parameters when Exporting"|t('freeform'),
            instructions: "Enable this to include tracked URL parameter values (e.g. UTM campaign data) in exported submission data."|t('freeform'),
            name: "settings[exportTrackedUrlParameters]",
            value: settings.exportTrackedUrlParameters,
            errors: settings.errors("exportTrackedUrlParameters"),
            disabled: readOnly,
        }) }}

    {% endif %}

    <hr>

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

    {{ forms.booleanMenuField({
        includeEnvVars: true,
        label: "Fill Form Values from the GET Query String"|t('freeform'),
        instructions: "Enable this to be able to fill form field values from a GET query string in URI."|t('freeform'),
        name: "settings[fillWithGet]",
        value: settings.fillWithGet,
        errors: settings.errors("fillWithGet"),
        disabled: readOnly,
    }) }}

    {{ forms.booleanMenuField({
        includeEnvVars: true,
        label: "Allow Dashes in Field Handles"|t('freeform'),
        instructions: "Enable this to be able to allow users to include dashes in field handle names in the form builder."|t('freeform'),
        warning: "Dashes in field handle names are not allowed by default. Enabling this could negatively affect other parts of Craft, especially GraphQL functionality. Use with caution and only if necessary."|t('freeform'),
        name: "settings[allowDashesInFieldHandles]",
        value: settings.allowDashesInFieldHandles,
        errors: settings.errors("allowDashesInFieldHandles"),
        disabled: readOnly,
    }) }}

    <hr>

    <h2>{{ "Submission Data"|t('freeform') }}</h2>

    {{ forms.booleanMenuField({
        includeEnvVars: true,
        label: "Enable Search Index Updating on New Submissions"|t('freeform'),
        instructions: "Enable this to have Craft update search indexes whenever a new submission is created."|t('freeform'),
        name: "settings[updateSearchIndexes]",
        value: settings.updateSearchIndexes,
        errors: settings.errors("updateSearchIndexes"),
        disabled: readOnly,
    }) }}

    {% if craft.freeform.pro %}

        {{ forms.lightswitchField({
            label: "Automatically Purge Submission Data"|t('freeform'),
            instructions: "If you wish to have Freeform automatically purge submission data after a specified number of days, enable this setting and specify the number of days after submission date it should purge submissions."|t('freeform'),
            warning: "Enabling this and saving this settings page will result in purging feature beginning, and cannot be undone (it may not happen immediately, and may take a couple hours before the next process runs)."|t('freeform'),
            name: "purge-toggle",
            on: settings.purgableSubmissionAgeInDays,
            toggle: "purge",
            errors: settings.errors("purgableSubmissionAgeInDays"),
            disabled: readOnly,
        }) }}

        <div id="purge" class="field {{ not settings.purgableSubmissionAgeInDays ? 'hidden' }}">
            {{ forms.selectField({
                label: "Days After Submission Date to Purge"|t('freeform'),
                instructions: "Select the number of days after submissions have been submitted for Freeform to begin automatically purging. This process will only run every hour, and only when Freeform is accessed on the front end in templates or in the control panel."|t('freeform'),
                name: "settings[purgableSubmissionAgeInDays]",
                value: settings.purgableSubmissionAgeInDays,
                errors: settings.errors("purgableSubmissionAgeInDays"),
                disabled: readOnly,
                id: "purge-value",
                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.checkboxSelectField({
                id: "purge-forms",
                label: "Apply to Forms"|t('freeform'),
                instructions: "Choose which forms the automatic submission purge should apply to."|t('freeform'),
                name: "settings[purgableFormIds]",
                values: settings.purgableFormIds is null ? '*' : settings.purgableFormIds,
                showAllOption: true,
                options: forms|sort,
                errors: settings.errors("purgableFormIds"),
            }) }}
        </div>

    {% endif %}

    {{ forms.lightswitchField({
        label: "Automatically Purge Unfinalized Assets"|t('freeform'),
        instructions: "Enable this to have Freeform automatically purge unfinalized asset files after a specified period of time."|t('freeform'),
        name: "settings[purgeAssets]",
        on: settings.purgeAssets,
        toggle: "purge-assets",
        errors: settings.errors("purgeAssets"),
        disabled: readOnly,
    }) }}

    <div id="purge-assets" class="field {{ not settings.purgeAssets ? 'hidden' }}">
        {{ forms.selectField({
            label: "Time to Wait Before Purging Assets"|t('freeform'),
            instructions: "Select how long Freeform should keep unfinalized asset files before purging."|t('freeform'),
            name: "settings[purgableUnfinalizedAssetAgeInMinutes]",
            value: settings.purgableUnfinalizedAssetAgeInMinutes,
            errors: settings.getErrors("purgableUnfinalizedAssetAgeInMinutes"),
            disabled: readOnly,
            id: "purgableUnfinalizedAssetAgeInMinutes",
            options: {
                60: "1 hour"|t("freeform"),
                120: "2 hours"|t("freeform"),
                180: "3 hours (recommended)"|t("freeform"),
                240: "4 hours"|t("freeform"),
                480: "8 hours"|t("freeform"),
                720: "12 hours"|t("freeform"),
                1440: "24 hours"|t("freeform"),
                2880: "48 hours"|t("freeform"),
                4320: "72 hours"|t("freeform"),
                10080: "1 week"|t("freeform"),
                20160: "2 weeks"|t("freeform"),
            }
        }) }}
    </div>

    <div id="logging-level-warning" class="hidden">
        {{- "A logging level of 'Info' or 'Debug' should not be used in production regularly as logs may contain sensitive data."|t('freeform') -}}
    </div>

{% endblock %}
