{% load i18n wagtailadmin_tags %}

{% trans "Object permissions" as heading %}
{% panel id="object-permissions" heading=heading %}
    <fieldset>
        <legend class="w-sr-only">{% trans "Object permissions" %}</legend>

        <table class="listing" data-controller="w-bulk">
            <colgroup>
                <col />
                <col width="10%" />
                <col width="10%" />
                <col width="10%" />
                {% if extra_perms_exist.custom %}
                    <col width="25%" />
                {% endif %}
            </colgroup>
            <thead>
                <tr>
                    <th>{% trans "Name" %}</th>
                    <th>{% trans "Add" %}</th>
                    <th>{% trans "Change" %}</th>
                    <th>{% trans "Delete" %}</th>
                    {% if extra_perms_exist.custom %}
                        <th>{% trans "Custom permissions" %}</th>
                    {% endif %}
                </tr>
            </thead>
            <tbody>
                {% for content_perms_dict in object_perms %}
                    <tr>
                        <td class="title"><h3>{{ content_perms_dict.object|capfirst }}</h3></td>
                        <td>
                            {% if content_perms_dict.add %}
                                <label for="{{ content_perms_dict.add.checkbox.id_for_label }}" class="w-sr-only">{{ content_perms_dict.add.perm.name }}</label>
                                {{ content_perms_dict.add.checkbox.tag }}
                            {% endif %}
                        </td>
                        <td>
                            {% if content_perms_dict.change %}
                                <label for="{{ content_perms_dict.change.checkbox.id_for_label }}" class="w-sr-only">{{ content_perms_dict.change.perm.name }}</label>
                                {{ content_perms_dict.change.checkbox.tag }}
                            {% endif %}
                        </td>
                        <td>
                            {% if content_perms_dict.delete %}
                                <label for="{{ content_perms_dict.delete.checkbox.id_for_label }}" class="w-sr-only">{{ content_perms_dict.delete.perm.name }}</label>
                                {{ content_perms_dict.delete.checkbox.tag }}
                            {% endif %}
                        </td>
                        {% if extra_perms_exist.custom %}
                            <td>
                                {% if content_perms_dict.custom %}
                                    <fieldset class="w-p-0 w-flex w-flex-col w-gap-1">
                                        <legend class="w-sr-only">{% trans "Custom permissions" %}</legend>
                                        {% for custom_perm in content_perms_dict.custom %}
                                            {% trans custom_perm.name as custom_perm_label %}
                                            {% include "wagtailadmin/shared/forms/single_checkbox.html" with name="permissions" value=custom_perm.perm.id checked=custom_perm.selected text=custom_perm_label attrs=custom_perm.attrs %}
                                        {% endfor %}
                                    </fieldset>
                                {% endif %}
                            </td>
                        {% endif %}
                    </tr>
                {% endfor %}
            </tbody>
            <tfoot>
                <tr>
                    <th scope="row">
                        <input id="toggle-all" type="checkbox" data-action="w-bulk#toggleAll" data-w-bulk-target="all">
                        <label for="toggle-all" >{% trans "Toggle all" %}</label>
                    </th>
                    <td>
                        <label for="toggle-all-add" class="visuallyhidden">{% trans "Toggle all add permissions" %}</label>
                        <input id="toggle-all-add" type="checkbox" data-action="w-bulk#toggleAll" data-w-bulk-target="all" data-w-bulk-group-param="add">
                    </td>
                    <td>
                        <label for="toggle-all-change" class="visuallyhidden">{% trans "Toggle all change permissions" %}</label>
                        <input id="toggle-all-change" type="checkbox" data-action="w-bulk#toggleAll" data-w-bulk-target="all" data-w-bulk-group-param="change">
                    </td>
                    <td>
                        <label for="toggle-all-delete" class="visuallyhidden">{% trans "Toggle all delete permissions" %}</label>
                        <input id="toggle-all-delete" type="checkbox" data-action="w-bulk#toggleAll" data-w-bulk-target="all" data-w-bulk-group-param="delete">
                    </td>
                    {% if extra_perms_exist.custom %}
                        <td>
                            <label for="toggle-all-custom" class="visuallyhidden">{% trans "Toggle all custom permissions" %}</label>
                            <input id="toggle-all-custom" type="checkbox" data-action="w-bulk#toggleAll" data-w-bulk-target="all" data-w-bulk-group-param="custom">
                        </td>
                    {% endif %}
                </tr>
            </tfoot>
        </table>
    </fieldset>
{% endpanel %}

{% trans "Other permissions" as heading %}
{% panel id="other-permissions" heading=heading %}
    <fieldset>
        <legend class="w-sr-only">{% trans "Other permissions" %}</legend>

        <table class="listing">
            <col />
            <col width="45%" />
            <thead>
                <tr>
                    <th>{% trans "Name" %}</th>
                </tr>
            </thead>
            <tbody>
                {% for perm_tuple in other_perms %}
                    <tr>
                        <td class="title"><label for="{{ perm_tuple.1.id_for_label }}" class="w-label-3">{% trans perm_tuple.0.name %}</label></td>
                        <td>
                            {{ perm_tuple.1.tag }}
                        </td>
                    </tr>
                {% endfor %}
            </tbody>
        </table>
    </fieldset>
{% endpanel %}
