{% load permissions_widget_tags i18n %}

<table class="table">
    <thead>
        <tr>
           <th colspan="{{ permission_types|length|add:"1" }}"><h2>Standard permissions</h2></th>
        </tr>
        <tr>
            <th>{% trans 'App & Model' %}</th>
            {% for permission_type in permission_types %}
            <th>
                {{ permission_type|capfirst }}
            </th>
            {% endfor %}
        </tr>
    </thead>
    <tbody>
        {% for row in table %}
            {% ifchanged row.app %}
                <tr>
                   <th colspan="{{ permission_types|length|add:"1" }}">
                        {{ row.app|capfirst }}
                   </th>
                </tr>
            {% endifchanged %}

            {% ifchanged row.model %}
                <tr>
                    <td>
                        {{ row.model|capfirst }}
                    </td>
                    {% for permission_type in permission_types %}
                        {% with row.permissions|get_item:permission_type as permission %}
                        <td>
                            {% if permission %}
                                <input name="{{ name }}" title="{{ permission.name }}" {% if permission.value in value %}checked="checked"{% endif %} value="{{ permission.value }}" type="checkbox" />
                            {% endif %}
                        </td>
                        {% endwith %}
                    {% endfor %}
                </tr>
            {% endifchanged %}
        {% endfor %}

        {% if permission_types_custom %}
            <tr>
               <th colspan="{{ permission_types|length|add:"1" }}"><h2>Custom permissions</h2></th>
            </tr>
        {% endif %}

        {% for row in table %}
            {% ifchanged row.model %}
                {% with permission_types_custom|get_for_model:row as permissions %}
                    {% if permissions %}
                        <tr>
                           <th colspan="{{ permission_types|length|add:"1" }}">
                                {{ row.app|capfirst }} |
                                {{ row.model|capfirst }}
                           </th>
                        </tr>
                        {% for permission in permissions %}
                            <tr>
                                <td>
                                    {{ permission.name }}
                                </td>
                                <td>
                                    <input name="{{ name }}" title="{{ permission.name }}" {% if permission.value in value %}checked="checked"{% endif %} value="{{ permission.value }}" type="checkbox" />
                                </td>
                                <td colspan="{{ permission_types|length|add:"-1" }}">&nbsp;</td>
                            </tr>
                        {% endfor %}
                    {% endif %}
                {% endwith %}
            {% endifchanged %}
        {% endfor %}
    </tbody>
</table>
