{% extends "_layouts/cp" %}
{% set fullPageForm = true %}

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


{% block content %}
    <input type="hidden" name="action" value="fields/save-field">
    {{ redirectInput('settings/fields/{groupId}') }}
    {% if fieldId is defined and fieldId %}
        <input type="hidden" name="fieldId" value="{{ fieldId }}">
    {% endif %}

    {{ forms.selectField({
        first: true,
        label: "Group"|t('app'),
        instructions: "Which group should this field be displayed in?"|t('app'),
        id: 'group',
        name: 'group',
        options: groupOptions,
        value: groupId
    }) }}

    {{ forms.textField({
        label: "Name"|t('app'),
        instructions: "What this field will be called in the CP."|t('app'),
        id: 'name',
        name: 'name',
        value: field.name,
        errors: field.getErrors('name'),
        required: true,
        autofocus: true
    }) }}

    {{ forms.textField({
        label: "Handle"|t('app'),
        instructions: "How you’ll refer to this field in the templates."|t('app'),
        id: 'handle',
        name: 'handle',
        class: 'code',
        autocorrect: false,
        autocapitalize: false,
        maxlength: 64,
        value: field.handle,
        errors: field.getErrors('handle'),
        required: true,
    }) }}

    {{ forms.textareaField({
        label: "Instructions"|t('app'),
        instructions: "Helper text to guide the author."|t('app'),
        id: 'instructions',
        class: 'nicetext',
        name: 'instructions',
        value: field.instructions,
        errors: field.getErrors('instructions'),
    }) }}

    {{ forms.checkboxField({
        label: "Use this field’s values as search keywords?"|t('app'),
        id: 'searchable',
        name: 'searchable',
        checked: field.searchable
    }) }}

    {{ forms.selectField({
        label: "Field Type"|t('app'),
        instructions: "What type of field is this?"|t('app'),
        warning: (fieldId is not empty and not field.hasErrors('type') ? "Changing this may result in data loss."|t('app')),
        id: 'type',
        name: 'type',
        options: fieldTypeOptions,
        value: className(field),
        toggle: true
    }) }}

    {{ missingFieldPlaceholder|raw }}

    {% if craft.app.getIsMultiSite() %}
        {% set translationMethods = field.supportedTranslationMethods %}
        {% if translationMethods|length > 1 %}
            <div id="translation-settings">
                {{ forms.selectField({
                    label: "Translation Method"|t('app'),
                    instructions: "How should this field’s values be translated?"|t('app'),
                    id: 'translation-method',
                    name: 'translationMethod',
                    options: [
                        'none' in translationMethods ? { value: 'none', label: "Not translatable"|t('app') },
                        'site' in translationMethods ? { value: 'site', label: "Translate for each site"|t('app') },
                        'siteGroup' in translationMethods ? { value: 'siteGroup', label: "Translate for each site group"|t('app') },
                        'language' in translationMethods ? { value: 'language', label: "Translate for each language"|t('app') },
                        'custom' in translationMethods ? { value: 'custom', label: "Custom…"|t('app') }
                    ]|filter,
                    value: field.translationMethod,
                    toggle: true,
                    targetPrefix: 'translation-method-'
                }) }}

                {% if 'custom' in translationMethods %}
                    <div id="translation-method-custom" {% if field.translationMethod != 'custom' %}class="hidden"{% endif %}>
                        {{ forms.textField({
                            label: "Translation Key Format"|t('app'),
                            instructions: "Template that defines the field’s custom “translation key” format. Field values will be copied to all sites that produce the same key. For example, to make the field translatable based on the first two characters of the site handle, you could enter `{site.handle[:2]}`.",
                            id: 'translation-key-format',
                            name: 'translationKeyFormat',
                            value: field.translationKeyFormat,
                            errors: field.getErrors('translationKeyFormat')
                        }) }}
                    </div>
                {% endif %}
            </div>
        {% endif %}
    {% endif %}

    <hr>

    {% for type in allFieldTypes %}
        {% set isCurrent = (type == className(field)) %}
        <div id="{{ type|id }}"{% if not isCurrent %} class="hidden"{% endif %}>
            {% namespace 'types['~type~']' %}
                {% set _field = isCurrent ? field : craft.app.fields.createField(type) %}
                {{ _field.getSettingsHtml()|raw }}
            {% endnamespace %}
        </div>
    {% endfor %}
{% endblock %}


{% if field is empty or field.handle is empty %}
    {% js %}
        new Craft.HandleGenerator('#name', '#handle');
    {% endjs %}
{% endif %}

{% js %}
    Craft.supportedTranslationMethods = {{ supportedTranslationMethods|json_encode|raw }};

    Craft.updateTranslationMethodSettings = function(type, container) {
        var $container = $(container);
        if (!Craft.supportedTranslationMethods[type] || Craft.supportedTranslationMethods[type].length == 1) {
            $container.addClass('hidden');
        } else {
            $container.removeClass('hidden');
            // Rebuild the options based on the field type's supported translation methods
            $container.find('select').html(
                ($.inArray('none', Craft.supportedTranslationMethods[type]) != -1 ? '<option value="none">{{ "Not translatable"|t('app')|e('js') }}</option>' : '') +
                ($.inArray('site', Craft.supportedTranslationMethods[type]) != -1 ? '<option value="site">{{ "Translate for each site"|t('app')|e('js') }}</option>' : '') +
                ($.inArray('siteGroup', Craft.supportedTranslationMethods[type]) != -1 ? '<option value="site">{{ "Translate for each site group"|t('app')|e('js') }}</option>' : '') +
                ($.inArray('language', Craft.supportedTranslationMethods[type]) != -1 ? '<option value="language">{{ "Translate for each language"|t('app')|e('js') }}</option>' : '') +
                ($.inArray('custom', Craft.supportedTranslationMethods[type]) != -1 ? '<option value="custom">{{ "Custom…"|t('app')|e('js') }}</option>' : '')
            );
        }
    }

    var $fieldTypeInput = $("#{{ 'type'|namespaceInputId|e('js') }}"),
        $translationSettings = $("#{{ 'translation-settings'|namespaceInputId|e('js') }}");

    $fieldTypeInput.change(function(e) {
        Craft.updateTranslationMethodSettings($(this).val(), $translationSettings);
    });
{% endjs %}
