{% extends "_base.html" %}
{% load i18n %}
{% load static %}
{% load content_filters %}
{% load page_filters %}
{% load rules %}
{% block content %}
    {% has_perm 'cms.change_page_object' request.user as can_edit_pages %}
    {% with filter_form.filters_visible as filters_visible %}
        <div class="table-header">
            <div class="flex flex-wrap justify-between gap-4">
                <h1 class="heading">
                    {% translate "Archived Pages" %}
                </h1>
                <a href="{% url 'pages' region_slug=request.region.slug language_slug=language.slug %}"
                   class="font-bold text-sm text-gray-800 flex items-center gap-1 mb-2 hover:underline">
                    <span><i icon-name="layout" class="align-top h-5"></i> {% translate "Page Tree" %}</span>
                </a>
            </div>
            <div class="flex flex-wrap justify-between gap-4">
                <div class="flex flex-wrap gap-4">
                    {% include "generic_language_switcher.html" with target="archived_pages" %}
                    {% include "_search_input.html" with object_type="page" object_archived=True related_form="page-filter-form" %}
                </div>
                {% if request.user.expert_mode %}
                    <button id="filter-toggle" class="btn btn-ghost">
                        <span class="filter-toggle-text {% if filters_visible %}hidden{% endif %}">{% translate "Show filters" %}</span>
                        <span class="filter-toggle-text {% if not filters_visible %}hidden{% endif %}">{% translate "Hide filters" %}</span>
                    </button>
                {% endif %}
            </div>
        </div>
        <div id="filter-form-container"
             class="{% if not filters_visible %}hidden{% endif %} w-full mt-4 rounded border border-solid border-gray-200 shadow bg-white">
            {% include "pages/_page_filter_form.html" %}
        </div>
    {% endwith %}
    <div class="table-listing">
        <div class="overflow-x-auto">
            <form id="bulk-action-form" method="post">
                {% csrf_token %}
            </form>
            <table data-activate-tree-drag-drop
                   class="w-full mt-4 rounded border-2 border-solid border-gray-200 shadow bg-white table-auto">
                <thead>
                    <tr class="border-b border-solid border-gray-200">
                        <th class="text-sm text-left uppercase py-3 pl-4 pr-2 min">
                            <input form="bulk-action-form" type="checkbox" id="bulk-select-all" />
                        </th>
                        {% if not filter_form.is_enabled %}
                            <th class="text-sm text-left uppercase py-3 pr-2 min">
                                {% translate "Hierarchy" %}
                            </th>
                        {% endif %}
                        <th class="text-sm text-left uppercase py-3 px-2">
                            {% translate "Title in" %} {{ language.translated_name }}
                        </th>
                        {% get_current_language as LANGUAGE_CODE %}
                        {% get_language LANGUAGE_CODE as backend_language %}
                        {% if backend_language and backend_language != language %}
                            <th class="text-sm text-left uppercase py-3 px-2">
                                {% translate "Title in" %} {{ backend_language.translated_name }}
                            </th>
                        {% endif %}
                        <th class="text-sm text-left uppercase py-3 px-2">
                            <div class="lang-grid flags whitespace-nowrap">
                                {% spaceless %}
                                    {% for lang in languages %}
                                        <a href="{% url 'pages' region_slug=request.region.slug language_slug=lang.slug %}">
                                            <span class="fp fp-rounded fp-{{ lang.primary_country_code }}"
                                                  title="{{ lang.translated_name }}"></span>
                                        </a>
                                    {% endfor %}
                                {% endspaceless %}
                            </div>
                        </th>
                        <th class="text-sm text-left uppercase py-3 px-2">
                            {% translate "Status" %}
                        </th>
                        <th class="text-sm text-left uppercase py-3 pl-2">
                            {% translate "Last updated" %}
                        </th>
                        <th class="text-sm text-right uppercase py-3 pl-2 pr-4 min">
                            {% translate "Options" %}
                        </th>
                    </tr>
                </thead>
                <tbody>
                    {% for page in pages %}
                        {% get_translation page language.slug as page_translation %}
                        {% include "pages/page_tree_archived_node.html" with page_translation=page_translation %}
                    {% empty %}
                        <tr>
                            <td colspan="7" class="px-4 py-3">
                                {% if filter_form.is_enabled %}
                                    {% translate "No archived pages found with these filters." %}
                                {% else %}
                                    {% translate "No pages archived yet." %}
                                {% endif %}
                            </td>
                        </tr>
                    {% endfor %}
                </tbody>
            </table>
            {% if can_edit_pages %}
                <div class="flex flex-wrap gap-2 mt-4">
                    <select id="bulk-action" class="w-auto max-w-full">
                        <option>
                            {% translate "Select bulk action" %}
                        </option>
                        <option data-bulk-action="{% url 'bulk_restore_pages' region_slug=request.region.slug language_slug=language.slug %}">
                            {% translate "Restore pages" %}
                        </option>
                    </select>
                    <button form="bulk-action-form" id="bulk-action-execute" class="btn" disabled>
                        {% translate "Execute" %}
                    </button>
                </div>
            {% endif %}
        </div>
    </div>
    {% include "../generic_confirmation_dialog.html" %}
    {% include "pages/_page_preview_overlay.html" %}
{% endblock content %}
