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

{% set element = category %}
{% set redirectUrl = "categories/#{group.handle}" %}
{% set addAnotherRedirectUrl = "categories/#{group.handle}/new?site=#{element.site.handle}&parentId={parent.id}" %}
{% set canUpdateSource = true %}
{% set saveSourceAction = 'categories/save-category' %}
{% set deleteSourceAction = 'categories/delete-category' %}

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

{% block actionButton %}
    {% if showPreviewBtn or shareUrl is defined %}
        <div class="btngroup">
            {% if showPreviewBtn %}
                <button type="button" class="btn livepreviewbtn" data-icon="view">{{ "Preview"|t('app') }}</button>
            {% endif %}
            {% if shareUrl is defined %}
                <a href="{{ shareUrl }}" class="btn sharebtn" data-icon="share" rel="noopener" target="_blank">{{ 'View'|t('app') }}</a>
            {% endif %}
        </div>
    {% endif %}

    {{ parent() }}
{% endblock %}

{% block content %}
    {{ hiddenInput('groupId', group.id) }}
    {{ parent() }}
    {% hook 'cp.categories.edit.content' %}
{% endblock %}

{% block details %}
    {{ parent() }}
    {% hook "cp.categories.edit.details" %}
{% endblock %}

{% block settings %}
    {{ forms.textField({
        label: "Slug"|t('app'),
        siteId: element.siteId,
        id: 'slug',
        name: 'slug',
        autocorrect: false,
        autocapitalize: false,
        value: element.slug,
        placeholder: "Enter slug"|t('app'),
        errors: (element.getErrors('slug')|merge(element.getErrors('uri')))
    }) }}

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

    {{ parent() }}
    {% hook "cp.categories.edit.settings" %}
{% endblock %}

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

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