{% extends "_layouts/element" %}
{% import "_includes/forms" as forms %}

{% set isSingle = section.type == 'single' %}
{% set isDraft = entry.getIsDraft() %}
{% set isRevision = entry.getIsRevision() %}

{% set element = entry %}
{% set hasRevisions = section.enableVersioning %}
{% set redirectUrl = 'entries/' ~ (isSingle ? 'singles' : section.handle) %}
{% set saveSourceAction = 'entries/save-entry' %}
{% set duplicateSourceAction = 'entries/duplicate-entry' %}
{% set deleteSourceAction = 'entries/delete-entry' %}
{% set deleteForSiteAction = 'entries/delete-for-site' %}
{% set saveDraftAction = 'entry-revisions/save-draft' %}
{% set deleteDraftAction = 'entry-revisions/delete-draft' %}
{% set publishDraftAction = 'entry-revisions/publish-draft' %}
{% set revertSourceAction = 'entry-revisions/revert-entry-to-version' %}
{% set canUpdateSource = (
    currentUser.can('publishEntries'~permissionSuffix) and
    (isSingle or entry.authorId == currentUser.id or currentUser.can('publishPeerEntries'~permissionSuffix)) and
    (not isDraft or entry.creatorId == currentUser.id or currentUser.can('publishPeerEntryDrafts'~permissionSuffix))
) %}
{% set canDuplicateSource = canUpdateSource and not isSingle %}
{% set canDeleteDraft = currentUser.can('deletePeerEntryDrafts'~permissionSuffix) %}

{% if not isSingle and canUpdateSource %}
    {% set nextEntryParams = ["site=#{entry.site.handle}"] %}
    {% if showEntryTypes %}
        {% set nextEntryParams = nextEntryParams|merge(['typeId={type.id}']) %}
    {% endif %}
    {% if section.type == 'structure' %}
        {% set nextEntryParams = nextEntryParams|merge(['parentId={parent.id}']) %}
    {% endif %}
    {% set canDuplicateSource = currentUser.can("createEntries#{permissionSuffix}") %}
    {% set canAddAnother = currentUser.can("createEntries#{permissionSuffix}") %}
    {% set addAnotherRedirectUrl = "entries/#{section.handle}/new?" ~ nextEntryParams|join('&') %}
{% endif %}


{% hook "cp.entries.edit" %}


{% block content %}
    {{ hiddenInput('sectionId', section.id) }}
    {{ parent() }}

    {# Give plugins a chance to add other things here #}
    {% hook "cp.entries.edit.content" %}
{% endblock %}

{% block settings %}
    {% if not isSingle and showEntryTypes %}
        {{ forms.selectField({
            status: entry.getAttributeStatus('typeId'),
            label: "Entry Type"|t('app'),
            id: 'entryType',
            name: 'typeId',
            value: entryType.id,
            options: entryTypeOptions,
            errors: entry.getErrors('typeId'),
        }) }}
    {% endif %}

    {{ forms.textField({
        status: entry.getAttributeStatus('slug'),
        label: "Slug"|t('app'),
        siteId: entry.siteId,
        translationDescription: 'This field is translated for each site.'|t('app'),
        id: 'slug',
        name: 'slug',
        autocorrect: false,
        autocapitalize: false,
        value: '__temp_' in entry.slug ? '' : entry.slug,
        placeholder: "Enter slug"|t('app'),
        errors: (not isRevision ? entry.getErrors('slug')|merge(entry.getErrors('uri'))),
        disabled: isRevision
    }) }}

    {% if not isSingle %}
        {% if parentOptionCriteria is defined %}
            {{ forms.elementSelectField({
                label: "Parent"|t('app'),
                id: 'parentId',
                name: 'parentId',
                elementType: elementType,
                selectionLabel: "Choose"|t('app'),
                sources: ['section:'~section.uid],
                criteria: parentOptionCriteria,
                limit: 1,
                elements: (parent is defined and parent ? [parent]),
                errors: entry.getErrors('parent'),
                disabled: entry.getIsDerivative() and not entry.isProvisionalDraft,
            }) }}
        {% endif %}

        {% if CraftEdition == CraftPro and currentUser.can('editPeerEntries'~permissionSuffix) %}
            {{ forms.elementSelectField({
                status: entry.getAttributeStatus('authorId'),
                label: "Author"|t('app'),
                id: 'author',
                name: 'author',
                elementType: userElementType,
                selectionLabel: "Choose"|t('app'),
                criteria: authorOptionCriteria,
                limit: 1,
                elements: (author is defined and author ? [author])
            }) }}
        {% endif %}

        {{ forms.dateTimeField({
            status: entry.getAttributeStatus('postDate'),
            label: "Post Date"|t('app'),
            id: 'postDate',
            name: 'postDate',
            value: (entry.postDate ? entry.postDate : null),
            errors: entry.getErrors('postDate'),
            disabled: isRevision
        }) }}

        {{ forms.dateTimeField({
            status: entry.getAttributeStatus('expiryDate'),
            label: "Expiry Date"|t('app'),
            id: 'expiryDate',
            name: 'expiryDate',
            value: (entry.expiryDate ? entry.expiryDate : null),
            errors: entry.getErrors('expiryDate'),
            disabled: isRevision
        }) }}
    {% endif %}

    {{ parent() }}
    {# Give plugins a chance to add other things here #}
    {% hook "cp.entries.edit.settings" %}
{% endblock %}

{% block meta %}
    {{ parent() }}
    {# Give plugins a chance to add other things here #}
    {% hook "cp.entries.edit.meta" %}
{% endblock %}

{% block details %}
    {{ parent() }}
    {# Give plugins a chance to add other things here #}
    {% hook "cp.entries.edit.details" %}
{% endblock %}


{% if not entry.slug or '__temp_' in entry.slug %}
    {% js %}
        window.slugGenerator = new Craft.SlugGenerator('#title', '#slug', {
            charMap: {{ craft.cp.getAsciiCharMap(entry.site.language)|json_encode|raw }}
        });
    {% endjs %}
{% endif %}
