{% load i18n l10n wagtailadmin_tags %}

{% comment %}
    The confirmation dialog is initially rendered with a message for a network error.
    If there is a conflict due to a new version being saved in another window,
    the message is updated to reflect that instead, using the w-teleport controller.
{% endcomment %}
{% blocktranslate trimmed asvar dialog_title %}
    A network error occurred
{% endblocktranslate %}
{% blocktranslate trimmed asvar dialog_subtitle %}
    Troubleshoot your connection to the site before proceeding.
    Open the editor in a separate tab to view the last saved version.
{% endblocktranslate %}
{% dialog id="w-save-confirmation-dialog" dialog_root_selector='[data-edit-form]' icon_name="warning" icon_classname="w-text-text-error" title=dialog_title subtitle=dialog_subtitle %}
    <div class="w-editing-sessions-dialog-buttons">
        <button class="button" type="button" data-w-dialog-target="confirm" data-action="w-dialog#confirm">{% trans "Continue" %}</button>
        <a href="" target="_blank" class="button button-secondary" data-controller="w-action">{% trans "View latest version" %}</a>
    </div>
{% enddialog %}

<form
    method="post"
    class="w-editing-sessions"
    data-controller="w-swap w-action w-session"
    data-w-swap-target-value="#w-editing-sessions"
    data-w-swap-src-value="{{ ping_url }}"
    data-w-swap-json-path-value="html"
    data-w-swap-defer-value="true"
    data-w-action-continue-value="true"
    data-w-action-url-value="{{ release_url }}"
    data-w-session-interval-value="{{ ping_interval|unlocalize }}"
    data-w-session-w-dialog-outlet="[data-edit-form] [data-controller='w-dialog']#w-save-confirmation-dialog"
    data-action="
                 w-session:ping->w-swap#submit
                 visibilitychange@document->w-session#dispatchVisibilityState
                 w-session:visible->w-session#ping
                 w-session:visible->w-session#addInterval
                 w-session:hidden->w-session#clearInterval
                 beforeunload@window->w-session#pause
                 pagehide@window->w-action#sendBeacon
                 w-unsaved:add@document->w-session#setUnsavedChanges
                 w-unsaved:clear@document->w-session#setUnsavedChanges
                 w-swap:json->w-session#updateSessionData
                 w-swap:success->w-session#setNetworkIntercept
                 w-swap:error->w-session#setNetworkIntercept
                 w-autosave:save@document->w-session#pause
                 w-autosave:success@document->w-session#updateSessionData
                 w-autosave:success@document->w-session#resume
                 w-autosave:success@document->w-session#setNetworkIntercept
                 w-autosave:error@document->w-session#resume
                 w-autosave:error@document->w-session#setNetworkIntercept
                 w-autosave:deactivated@document->w-session#ping
                "
>
    <input type="checkbox" name="is_editing" value="1" data-w-session-target="unsavedChanges" hidden />
    {% if revision_id %}
        {# The ID of the revision that is being edited #}
        <input type="hidden" name="revision_id" value="{{ revision_id|unlocalize }}" data-w-session-target="revisionId" />
    {% endif %}
    {% if revision_created_at %}
        {# The created_at of the revision that is being edited #}
        <input type="hidden" name="revision_created_at" value="{{ revision_created_at }}" data-w-session-target="revisionCreatedAt" />
    {% endif %}
    <div id="w-editing-sessions">
        {% component sessions_list %}
    </div>
</form>
