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

{% set drafts = entry.id ? craft.app.entryRevisions.getEditableDraftsByEntryId(entry.id, entry.siteId) : [] %}
{% set baseUrl = "entries/#{sectionHandle}/#{craft.app.request.getSegment(3)}/" %}
{% set parentIdParam = craft.app.request.getParam('parentId.0') ?: craft.app.request.getParam('parentId') %}
{% set params = parentIdParam ? { parentId: parentIdParam } : null %}

<div id="revision-btn" class="btn menubtn"{% if showSiteLabel %} data-icon="world"{% endif %}>{{ revisionLabel }}</div>

<div class="menu">
    {% if showSites %}
        {% set siteGroups = craft.app.sites.getAllGroups() %}
        {% for group in siteGroups %}
            {% set groupSiteIds = group.getSiteIds()|intersect(siteIds) %}
            {% if groupSiteIds %}
                {% if siteGroups|length > 1 %}<h6>{{ group.name|t('site') }}</h6>{% endif %}
                <ul class="padded">
                    {% for siteId in groupSiteIds %}
                        {% set site = craft.app.sites.getSiteById(siteId) %}
                        {% set status = siteId in enabledSiteIds ? 'enabled' : 'disabled' %}
                        <li>
                            {% if siteId == entry.siteId %}
                                <a class="sel">
                                    <div class="status {{ status }}"></div>{{ site.name|t('site') }}
                                </a>
                            {% else %}
                                {% set url = url(baseUrl~site.handle, params) %}
                                <a href="{{ url }}">
                                    <div class="status {{ status }}"></div>{{ site.name|t('site') }}
                                </a>
                            {% endif %}
                        </li>
                    {% endfor %}
                </ul>
            {% endif %}
        {% endfor %}
    {% endif %}

    {% if showSites and entry.id %}
        <hr>
    {% endif %}

    {% if entry.id %}
        <ul class="padded">
            {% set currentVersionEditTime = currentVersionEditTime ?? entry.dateUpdated %}
            <li><a{% if className(entry) == 'craft\\elements\\Entry' %} class="sel"{% endif %} href="{{ entry.getCpEditUrl() }}">{{ "Current"|t('app') }}
                <span class="light">
                    {{ currentVersionEditTime|timestamp('short') }}
                    {%- if currentVersionCreator is defined %}, {{ currentVersionCreator }}{% endif %}
                </span>
            </a></li>
        </ul>

        {% if drafts %}
            <h6>{{ "Drafts"|t('app') }}</h6>
            <ul class="padded">
                {% for draft in drafts %}
                    <li><a{% if className(entry) == 'craft\\models\\EntryDraft' and draft.draftId == draftId %} class="sel"{% endif %} href="{{ url(baseUrl~'drafts/'~draft.draftId) }}">
                        {{ draft.name|t('site') }}
                        <span class="light">{{ "by {creator}"|t('app', { creator: draft.creator }) }}</span>
                    </a></li>
                {% endfor %}
            </ul>
        {% endif %}

        {% if section.enableVersioning %}
            {% set versions = craft.app.entryRevisions.getVersionsByEntryId(entry.id, entry.siteId, 10) %}
            {% if versions %}
                <h6>{{ "Recent Versions"|t('app') }}</h6>
                <ul class="padded">
                    {% for version in versions %}
                        <li><a{% if className(entry) == 'craft\\models\\EntryVersion' and version.versionId == versionId %} class="sel"{% endif %} href="{{ url(baseUrl~'versions/'~version.versionId) }}">
                            {{ "Version {num}"|t('app', { num: version.num }) }}
                            <span class="light">{{ version.dateCreated|timestamp('short') }}, {{ version.getCreator() }}</span>
                        </a></li>
                    {% endfor %}
                </ul>
            {% endif %}
        {% endif %}
    {% endif %}
</div>

{% if className(entry) == 'craft\\models\\EntryDraft' %}
    <a id="editdraft-btn" class="btn edit icon" title="{{ 'Edit Draft Settings'|t('app') }}"></a>
    {% js %}
        new Craft.EntryDraftEditor({{ entry.draftId }}, '{{ revisionLabel|e("js") }}', '{{ entry.revisionNotes|e("js") }}');
    {% endjs %}
    {% do view.registerTranslations('app', [
        "Draft Name",
        "Notes",
    ]) %}
{% endif %}
