{% extends 'wagtailadmin/pages/side_panels/includes/action_list_item.html' %}
{% load wagtailadmin_tags i18n %}

{% comment %}
    This template is used to show Live, Draft, Live and Draft, In Moderation or Live and In Moderation.
    Sometimes {{ block.super }} will be called two times in the instances where the page is in multiple states eg. Live + Draft
{% endcomment %}

{% block content %}
    <div class="w-space-y-3">
        {% trans 'Page status: ' as screen_reader_title_prefix %}
        {% with workflow_state=page.current_workflow_state draft_revision=page.get_latest_revision live_revision=page.live_revision %}

            {# Live section #}
            {% if page.live %}
                {% trans 'Live' as title %}
                {% if live_revision %}
                    {% timesince_last_update live_revision.created_at user_display_name=live_revision.user|user_display_name use_shorthand=True as help_text %}
                {% endif %}

                {% with icon_name='doc-full-inverse' %}
                    {% if page.has_unpublished_changes or workflow_state %}
                        {% with hide_action=True %}
                            {{ block.super }}
                        {% endwith %}
                    {% else %}
                        {{ block.super }}
                    {% endif %}
                {% endwith %}

            {% endif %}

            {# Draft and In moderation settings #}
            {% if workflow_state %}

                {# In Moderation Settings #}
                {% trans 'In Moderation' as title %}
                {% if workflow_state.created_at %}
                    {% timesince_last_update workflow_state.created_at user_display_name=workflow_state.requested_by|user_display_name use_shorthand=True as help_text %}
                {% endif %}
                {% url 'wagtailadmin_pages:workflow_status' page.id as action_url %}
                {% trans 'View details' as action_text %}

                {# Icon #}
                {% with icon_name='draft' hide_action=False %}
                    {{ block.super }}
                {% endwith %}

                {# Draft Settings #}
            {% elif page.has_unpublished_changes %}
                {% trans 'Draft' as title %}
                {% if draft_revision.created_at %}
                    {% timesince_last_update draft_revision.created_at user_display_name=draft_revision.user|user_display_name use_shorthand=True as help_text %}
                {% endif %}

                {# Icon #}
                {% with icon_name='draft' %}
                    {{ block.super }}
                {% endwith %}
            {% endif %}
        {% endwith %}
    </div>
{% endblock %}

{% block action %}
    {% if workflow_state %}
        {% include 'wagtailadmin/pages/side_panels/includes/side_panel_button.html' with attr='data-action-workflow-status' data_url=action_url text=action_text  %}
    {% else %}
        {% url 'wagtailadmin_pages:history' page.id as action_url %}
        {% trans 'View history' as action_text %}
        {{ block.super }}
    {% endif %}
{% endblock %}

{% block bottom %}
    {# Workflow Status #}
    {% with workflow_state=page.current_workflow_state draft_revision=page.get_latest_revision %}
        {% if workflow_state %}
            <div class="w-flex w-space-x-3 w-mt-3">
                {% icon name='warning' class_name='w-w-4 w-h-4 w-text-info-100 w-shrink-0' %}
                <div class="w-label-3 w-flex-1">
                    {% if workflow_state.status == 'needs_changes' %}
                        {% trans " Changes requested" %}
                        {{ workflow_state.current_task_state.finished_at|naturaltime }}
                    {% else %}
                        {% if workflow_state.status == "in_progress" %}
                            {% trans "Awaiting" %}
                            {% trans "since" as since_text %}
                        {% else %}
                            {{ workflow_state.get_status_display }}
                        {% endif %}
                        {{ workflow_state.current_task_state.task.name }}
                        {{ workflow_state.current_task_state.started_at|naturaltime }}
                    {% endif %}
                </div>
            </div>
        {% endif %}

        {# Scheduled publishing #}
        {% if draft_revision and draft_revision.approved_go_live_at %}
            <div class="w-flex w-space-x-3">
                {% icon name='info-circle' class_name='w-w-4 w-h-4 w-text-info-100 w-shrink-0' %}
                <div class="w-label-3 w-flex-1">
                    {% trans 'This will publish at ' %}{{ draft_revision.approved_go_live_at }}
                </div>
            </div>
        {% endif %}
    {% endwith %}
{% endblock %}
