{% extends "admin/base_site.html" %} 
{% load i18n admin_urls static %}
{% load pfp_submit_row pfp_filter_field from pfp_tags %}

{% block extrastyle %}
{{ block.super }}
<link rel="stylesheet" type="text/css" href="{% static "admin/css/forms.css" %}" />
<link rel="stylesheet" type="text/css" href="{% static "perfieldperms/css/pfp_admin.css" %}" />
{% endblock %}

{% block breadcrumbs %}
<div class="breadcrumbs">
	<a href="{% url 'admin:index' %}">{% trans 'Home' %}</a>
	&rsaquo; <a href="{% url 'admin:app_list' app_label=opts.app_label %}">{{ opts.app_config.verbose_name }}</a>
	{% if title %} &rsaquo; {{ title }}{% endif %}
</div>
{% endblock %}

{% block content %}
<div id="content-main">
	{% block user_help %}
	To manage permissions:
	<ol>
		<li>Select the role type (e.g. User, Group) to manage permissions for, and the model whose permissions you want to allocate.</li>
		<li>Use the permission filters to select the subset of permissions you want to work with. By default model level permissions are shown, to display field level permissions check the appropriate box, or select individual fields.</li>
		<li>Use the "Filter" button to apply chosen filters.</li>
	</ol>

	{% endblock %}

	<div id="pfp-manage-form">

		<form {% if has_file_field %}enctype="multipart/form-data" {% endif %}action="{{ form_url }}" method="post" id="{{ opts.model_name }}_form" novalidate>{% csrf_token %}
			{% block form_top %}{% endblock %}
			<div class="module">
				{% if save_on_top %}{% block submit_buttons_top %}{% pfp_submit_row %}{% endblock %}{% endif %}
				{% if filter_form.errors %}
				<p class="errornote">
					{% if filter_form.errors|length == 1 %}{% trans "Please correct the error below." %}{% else %}{% trans "Please correct the errors below." %}{% endif %}
				</p>
				{{ filter_form.non_field_errors }}
				{% endif %}

				{% block permission_filter %}
				<div id="pfp-perm-filter">
					<h2>Permission filters</h2>
					{% pfp_filter_field perm_filter_form.model %}
					{% if perm_filter_form.model.data %}
						{% pfp_filter_field perm_filter_form.show_pfps %}
						{% pfp_filter_field perm_filter_form.permissions %}
						{% pfp_filter_field perm_filter_form.model_fields %}
					{% endif %}
				</div>
				{% endblock %}

				<div id="pfp-select-block">
					{% block role_filter %}
					<div id="pfp-role-filter">
						<h2>Role filters</h2>
						<ul>
							{{ role_filter_form.as_ul }}
						</ul>
					</div>
					{% endblock %}

					{% block pfp_select_form %}
					{% if perm_formset %}
						<div id="pfp-select-form">
							<h2>Permission assignment</h2>
							{{ perm_formset.management_form }}
								
							{% if perm_formset.total_error_count %}
								<p class="errornote">
									{% if perm_formset.total_error_count|length == 1 %}{% trans "Please correct the error below." %}{% else %}{% trans "Please correct the errors below." %}{% endif %}
								</p>
							{% endif %}
								
							<table>
								<tr>
									<th>Role name</th><th>Permissions</th>
								</tr>
								{% for form in perm_formset %}
								<tr class="{% cycle 'row1' 'row2' %}">
									<td>{{ form.initial.role_name }}{{ form.role_name }}{{ form.role_id }}</td>
									{% for perm in form.permissions %}<td>{{ perm }}</td>{% endfor%}
								</tr>
								{% endfor %}
							</table>
								
							{% block pagination %}
							<div class="pagination">
								<span class="step-links">
									{% if page.has_previous %}<a href="?page={{ page.previous_page_number }}">previous</a>{% endif %}
									<span class="current">Page {{ page.number }} of {{ page.paginator.num_pages }}</span>
									{% if page.has_next%}<a href="?page={{ page.next_page_number }}">next</a>{% endif %}
								</span>
							</div>
							{% endblock %}
								
						</div>
						{% endif %}
					{% endblock %}

					{% block submit_buttons_bottom %}{% pfp_submit_row %}{% endblock %}
					</div>
				</div>
			</div>
		</form>

	</div>
</div>
{% endblock %}
