{% extends "_layouts/cp" %}
{% set fullPageForm = true %}
{% set formActions = [
    {
        label: 'Save and continue editing'|t('app'),
        redirect: "settings/sections/#{section.id}/entrytypes/{id}"|hash,
        shortcut: true,
        retainScroll: true,
    },
] %}

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


{% block content %}
    {{ actionInput('sections/save-entry-type') }}

    {% if section.type == 'single' %}
        {{ redirectInput('settings/sections') }}
    {% else %}
        {{ redirectInput('settings/sections/'~section.id~'/entrytypes') }}
    {% endif %}
    {{ hiddenInput('sectionId', section.id) }}
    {% if entryType.id %}{{ hiddenInput('entryTypeId', entryType.id) }}{% endif %}

    {% if section.type != 'single' %}
        {{ forms.textField({
            first: true,
            label: "Name"|t('app'),
            instructions: "What this entry type will be called in the control panel."|t('app'),
            id: 'name',
            name: 'name',
            value: entryType.name,
            errors: entryType.getErrors('name'),
            autofocus: true,
            required: true
        }) }}

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

        <hr>

    {% endif %}

    {{ forms.checkboxField({
        first: (section.type == 'single'),
        label: "Show the Title field"|t('app'),
        name: 'hasTitleField',
        toggle: 'title-container',
        reverseToggle: '#titleFormat-container, .fld-title-field-icon',
        checked: entryType.hasTitleField
    }) }}

    {% if craft.app.getIsMultiSite() %}
        <div id="title-container"{% if not entryType.hasTitleField %} class="hidden"{% endif %}>
            {{ forms.selectField({
                label: 'Title Translation Method'|t('app'),
                instructions: 'How should {type} titles be translated?'|t('app', {
                    type: lowerTypeName,
                }),
                id: 'translation-method',
                name: 'titleTranslationMethod',
                options: [
                    { value: 'none', label: 'Not translatable'|t('app') },
                    { value: 'site', label: 'Translate for each site'|t('app') },
                    { value: 'siteGroup', label: 'Translate for each site group'|t('app') },
                    { value: 'language', label: 'Translate for each language'|t('app') },
                    { value: 'custom', label: 'Custom…'|t('app') },
                ]|filter,
                value: entryType.titleTranslationMethod,
                toggle: true,
                targetPrefix: 'translation-method-'
            }) }}

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

    <div id="titleFormat-container"{% if entryType.hasTitleField %} class="hidden"{% endif %}>
        {{ forms.textField({
            label: "Title Format"|t('app'),
            instructions: "What the auto-generated entry titles should look like. You can include tags that output entry properties, such as {ex}."|t('app', { ex: '<code>{myCustomField}</code>' }),
            id: 'titleFormat',
            name: 'titleFormat',
            class: 'code',
            value: entryType.titleFormat,
            errors: entryType.getErrors('titleFormat'),
            required: true
        }) }}
    </div>

    <hr>

    {{ forms.fieldLayoutDesignerField({
        fieldLayout: entryType.getFieldLayout(),
    }) }}
{% endblock %}


{% if not entryType.handle %}
    {% js "new Craft.HandleGenerator('#name', '#handle');" %}
{% endif %}
