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

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


{% block content %}
    <input type="hidden" name="action" value="sections/save-section">
    {{ redirectInput('settings/sections') }}

    {% if section.id %}<input type="hidden" name="sectionId" value="{{ section.id }}">{% endif %}

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

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

    {{ forms.checkboxField({
        label: "Enable versioning for entries in this section?"|t('app'),
        id: 'enableVersioning',
        name: 'enableVersioning',
        checked: section.enableVersioning
    }) }}

    {{ forms.selectField({
        label: "Section Type"|t('app'),
        instructions: "What type of section is this?"|t('app') ~ (section.id ? '<br><span class="error">'~"Careful—changing this may result in data loss."|t('app')~'</span>' : ''),
        id: 'type',
        name: 'type',
        options: typeOptions,
        value: section.type,
        toggle: true,
        targetPrefix: '.type-',
        errors: section.getErrors('type')
    }) }}

    <hr>

    {% set siteRows = [] %}
    {% set siteErrors = section.getErrors('siteSettings') %}

    {% for site in craft.app.sites.getAllSites() %}
        {% set siteSettings = section.siteSettings[site.id] ?? null %}
        {% if siteSettings %}
            {% for attribute, errors in siteSettings.getErrors() %}
                {% set siteErrors = siteErrors|merge(errors) %}
            {% endfor %}
        {% endif %}
        {% set siteRows = siteRows|merge({
            (site.handle): {
                heading: site.name|t('site'),
                enabled: include('_includes/forms/lightswitch', {
                    name: 'sites['~site.handle~'][enabled]',
                    on: brandNewSection or siteSettings,
                    value: site.id,
                    small: true
                }),
                singleUri: {
                    value: (section.type == 'single' and siteSettings and siteSettings.uriFormat != '__home__') ? siteSettings.uriFormat,
                    hasErrors: (section.type == 'single' and siteSettings ? siteSettings.hasErrors('uriFormat'))
                },
                uriFormat: {
                    value: siteSettings ? siteSettings.uriFormat,
                    hasErrors: (section.type != 'single' and siteSettings ? siteSettings.hasErrors('uriFormat'))
                },
                template: {
                    value: siteSettings ? siteSettings.template,
                    hasErrors: siteSettings ? siteSettings.hasErrors('template'),
                },
                enabledByDefault: siteSettings ? siteSettings.enabledByDefault : true,
            }
        }) %}
    {% endfor %}

    {{ forms.editableTableField({
        label: "Site Settings"|t('app'),
        instructions: "Choose which sites this section should be available in, and configure the site-specific settings."|t('app'),
        id: 'sites',
        name: 'sites',
        cols: {
            heading: {
                type: 'heading',
                heading: "Site"|t('app'),
                class: 'thin'
            },
            enabled: {
                type: 'heading',
                class: 'thin'~(not craft.app.getIsMultiSite() ? ' hidden')
            },
            singleUri: {
                type: 'singleline',
                heading: "URI"|t('app'),
                info: "What the entry URI should be for the site. Leave blank if this is the homepage."|t('app'),
                placeholder: "Leave blank if this is the homepage"|t('app'),
                code: true,
                class: 'type-single'~(section.type != 'single' ? ' hidden')
            },
            uriFormat: {
                type: 'singleline',
                heading: "Entry URI Format"|t('app'),
                info: "What entry URIs should look like for the site. Leave blank if entries don’t have URLs."|t('app'),
                placeholder: "Leave blank if entries don’t have URLs"|t('app'),
                code: true,
                class: 'type-channel type-structure'~(section.type == 'single' ? ' hidden')
            },
            template: {
                type: 'singleline',
                heading: "Template"|t('app'),
                info: "Which template should be loaded when an entry’s URL is requested."|t('app'),
                code: true
            },
            enabledByDefault: {
                type: 'lightswitch',
                heading: "Default Status"|t('app'),
                class: 'thin type-channel type-structure'~(section.type == 'single' ? ' hidden')
            }
        },
        rows: siteRows,
        staticRows: true,
        errors: siteErrors|unique
    }) }}

    {% if craft.app.getIsMultiSite() %}
        <div class="type-channel {% if section.type != 'channel' %}hidden{% endif %}">
            {{ forms.checkboxField({
                label: 'Propagate entries across all enabled sites?'|t('app'),
                instructions: 'Whether entries should be propagated across all the sites the section is enabled in. If this is disabled, each entry will only belong to the site it was created in.'|t('app'),
                id: 'propagateEntries',
                name: 'propagateEntries',
                checked: section.propagateEntries,
                warning: section.id and section.propagateEntries ? 'Changing this may result in data loss.'|t('app')
            }) }}
        </div>
    {% endif %}

    <div class="type-structure {% if section.type != 'structure' %}hidden{% endif %}">
        {{ forms.textField({
            label: "Max Levels"|t('app'),
            instructions: "The maximum number of levels this section can have. Leave blank if you don’t care."|t('app'),
            id: 'maxLevels',
            name: 'maxLevels',
            value: section.maxLevels,
            size: 5,
            errors: section.getErrors('maxLevels')
        }) }}
    </div>
{% endblock %}


{% js %}
    var $siteRows = $('#sites').children('tbody').children(),
        $lightswitches = $siteRows.children('th:nth-child(2)').children('.lightswitch');

    function updateSites() {
        $lightswitches.each(function() {
            if ($(this).data('lightswitch').on) {
                $(this).parent().nextAll('td').removeClass('disabled').find('textarea,div.lightswitch,input').attr('tabindex', '0');
            } else {
                $(this).parent().nextAll('td').addClass('disabled').find('textarea,div.lightswitch,input').attr('tabindex', '-1');
            }
        });
    }

    $lightswitches.on('change', updateSites);

    Garnish.$doc.ready(function() {
        updateSites();
    });

    {% if brandNewSection %}
        new Craft.HandleGenerator('#name', '#handle');

        {% for site in craft.app.sites.getAllSites() %}
            new Craft.UriFormatGenerator('#name', '#sites tr[data-id="{{ site.handle }}"] textarea[name$="[singleUri]"]');
            new Craft.UriFormatGenerator('#name', '#sites tr[data-id="{{ site.handle }}"] textarea[name$="[uriFormat]"]', { suffix: '/{slug}' });
            new Craft.UriFormatGenerator('#name', '#sites tr[data-id="{{ site.handle }}"] textarea[name$="[template]"]', { suffix: '/_entry' });
        {% endfor %}
    {% endif %}
{% endjs %}
