{% load permissions_widget_tags i18n static %}

<table class="table permissions-widget">
    <thead>
        <tr>
            <th>App & Model</th>
            {% for permission_type in default_permission_types %}
                <th>
                    {{ permission_type|translate|capfirst }}
                </th>
            {% endfor %}
            <th>
                Other
            </th>
            {% if group.id %}
            <th>
              	Field Permissions
            </th>
            {% endif %}
        </tr>
    </thead>
    <tbody>
        {% for row in table %}
            {% ifchanged row.app %}
                <tr>
                    {% if group.id %}
                        <th colspan="{{ default_permission_types|length|add:"3" }}">
                    {% else %}
                        <th colspan="{{ default_permission_types|length|add:"2" }}">
                    {% endif %}
                    {{ row.app|translate|capfirst }}
                        </th>
                </tr>
            {% endifchanged %}

            {% ifchanged row.model %}
            <tr>
                <td>
                    {{ row.model|capfirst }}
                </td>
                {% for permission_type in default_permission_types %}
                    {% with row.permissions|get_item:permission_type as permission %}
                        <td class="{% if permission %}{{ permission_type }}{% endif %} {% if permission in groups_permissions %}disabled{% endif %}">
                            {% if permission %}
                                <input name="{{ name }}" title="{{ permission.name|translate|capfirst }}" {% if permission.pk in value or permission in groups_permissions %}checked="checked"{% endif %} value="{{ permission.pk }}" type="checkbox" {% if permission in groups_permissions or is_readonly %}disabled="disabled"{% endif %} />
                                {{ permission_type|translate|capfirst }}
                            {% endif %}
                        </td>
                    {% endwith %}
                {% endfor %}
                <td>
                    {% for permission_type in custom_permission_types %}
                        {% with row.permissions|get_item:permission_type as permission %}
                            {% if permission %}
                                <div {% if permission in groups_permissions %}class="disabled"{% endif %}>
                                    <input name="{{ name }}" title="{{ permission.name|translate|capfirst }}" {% if permission.pk in value or permission in groups_permissions %}checked="checked"{% endif %} value="{{ permission.pk }}" type="checkbox" {% if permission in groups_permissions or is_readonly %}disabled="disabled"{% endif %} />
                                    {{ permission.name|translate|capfirst }}
                                    <br>
                                </div>
                            {% endif %}
                        {% endwith %}
                    {% endfor %}
                </td>
                <td>
                    {% if row.model_class_name and row.model_class_name != 'groupadminsettings' and group.id %}
                        <div class="related-widget-wrapper" id="{{ row.app_label }}_{{ row.model_class_name }}">
                            <a class="related-widget-wrapper-link add-related"
                               id="add_{{ row.app_label }}_{{ row.model_class_name }}"
                               href="{% url 'admin:accounts_groupadminsettings_add' %}?_app_name={{ row.app_label }}&_model_name={{ row.model_class_name }}&_group_id={{ group.id }}&_popup=1&_to_field=id&rel_model=group"
                               title="{% blocktrans %}Change{% endblocktrans %}">
                                <img src="{% static 'admin/img/icon-changelink.svg' %}" alt="{% trans 'Change' %}"/>
                            </a>
                        </div>
                    {% endif %}
                </td>
            </tr>
            {% endifchanged %}
        {% endfor %}
    </tbody>
</table>
