{% extends 'base.html' %}

{% block body %}
{% if role.locked %}
<div class="alert alert-warning" role="alert">
{{_("Name, moderator group, included roles and groups of this role are managed externally.")}} <a href="{{ url_for("role.unlock", roleid=role.id) }}" class="alert-link">Unlock this role</a> to edit them at the risk of having your changes overwritten.
</div>
{% endif %}

<form action="{{ url_for("role.update", roleid=role.id) }}" method="POST">
<div class="align-self-center">
	<div class="float-sm-right pb-2">
		<button type="submit" class="btn btn-primary"><i class="fa fa-save" aria-hidden="true"></i> {{_("Save")}}</button>
		<a href="{{ url_for("role.index") }}" class="btn btn-secondary">{{_("Cancel")}}</a>
		{% if role.id %}
			{% if not role.is_default %}
			<a href="{{ url_for("role.set_default", roleid=role.id) }}" onClick='return confirm({{_("All non-service users will be removed as members from this role and get its permissions implicitly. Are you sure?")|tojson}});' class="btn btn-secondary">{{_("Set as default")}}</a>
			{% else %}
			<a href="{{ url_for("role.unset_default", roleid=role.id) }}" onClick='return confirm({{_("Are you sure?")|tojson}});' class="btn btn-secondary">{{_("Unset as default")}}</a>
			{% endif %}
			<a href="{{ url_for("role.delete", roleid=role.id) }}"  onClick='return confirm({{_("Are you sure?")|tojson}});' class="btn btn-danger {{ 'disabled' if role.locked }}"><i class="fa fa-trash" aria-hidden="true"></i> {{_("Delete")}}</a>
		{% else %}
			<a href="#" class="btn btn-secondary disabled">{{_("Set as default")}}</a>
			<a href="#" class="btn btn-danger disabled"><i class="fa fa-trash" aria-hidden="true"></i> {{_("Delete")}}</a>
		{% endif %}
	</div>
	<ul class="nav nav-tabs pt-2 border-0" id="tablist" role="tablist">
		<li class="nav-item">
			<a class="nav-link active" id="settings-tab" data-toggle="tab" href="#settings" role="tab" aria-controls="settings" aria-selected="true">{{_("Settings")}}</a>
		</li>
		<li class="nav-item">
			<a class="nav-link" id="roles-tab" data-toggle="tab" href="#roles" role="tab" aria-controls="roles" aria-selected="false">{{_("Included roles")}} <span class="badge badge-pill badge-secondary">{{ role.included_roles|length }}</span></a>
		</li>
		<li class="nav-item">
			<a class="nav-link" id="groups-tab" data-toggle="tab" href="#groups" role="tab" aria-controls="groups" aria-selected="false">{{_("Included groups")}} <span class="badge badge-pill badge-secondary">{{ role.groups|length }}</span></a>
		</li>
	</ul>

	<div class="tab-content border mb-2 pt-2" id="tabcontent">
		<div class="tab-pane fade show active" id="settings" role="tabpanel" aria-labelledby="settings-tab">
			<div class="form-group col">
				<label for="role-name">{{_("Role Name")}}</label>
				<input type="text" class="form-control" id="role-name" name="name" value="{{ role.name or '' }}" {{ 'disabled' if role.locked }}>
				<small class="form-text text-muted">
				</small>
			</div>
			<div class="form-group col">
				<label for="role-description">{{_("Description")}}</label>
				<textarea class="form-control" id="role-description" name="description" rows="5">{{ role.description or '' }}</textarea>
				<small class="form-text text-muted">
				</small>
			</div>
			<div class="form-group col">
				<label for="moderator-group">{{_("Moderator Group")}}</label>
				<select class="form-control" id="moderator-group" name="moderator-group" {{ 'disabled' if role.locked }}>
					<option value="" class="text-muted">{{_("No Moderator Group")}}</option>
					{% for group in groups %}
					<option value="{{ group.dn }}" {{ 'selected' if group == role.moderator_group }}>{{ group.name }}</option>
					{% endfor %}
				</select>
			</div>
			<div class="form-group col">
				<span>{{_("Moderators")}}:</span>
				<ul class="row">
					{% for moderator in role.moderator_group.members %}
					<li class="col-12 col-xs-6 col-sm-4 col-md-3 col-lg-2"><a href="{{ url_for("user.show", uid=moderator.uid) }}">{{ moderator.loginname }}</a></li>
					{% endfor %}
				</ul>
			</div>
			<div class="form-group col">
				<span>{{_("Members")}}:</span>
				<ul class="row">
				{% for member in role.members|sort(attribute='loginname') %}
					<li class="col-12 col-xs-6 col-sm-4 col-md-3 col-lg-2"><a href="{{ url_for("user.show", uid=member.uid) }}">{{ member.loginname }}</a></li>
				{% endfor %}
				</ul>
			</div>
		</div>
		<div class="tab-pane fade" id="roles" role="tabpanel" aria-labelledby="roles-tab">
			<div class="form-group col">
				<span>{{_("Roles to include groups from recursively")}}</span>
				<table class="table table-striped table-sm">
					<thead>
						<tr>
							<th scope="col"></th>
							<th scope="col">{{_("name")}}</th>
							<th scope="col">{{_("description")}}</th>
							<th scope="col">{{_("currently includes groups")}}</th>
						</tr>
					</thead>
					<tbody>
						{% for r in roles|sort(attribute="name")|sort(attribute='name') %}
						<tr id="include-role-{{ r.id }}">
							<td>
								<div class="form-check">
									<input class="form-check-input" type="checkbox" id="include-role-{{ r.id }}-checkbox" name="include-role-{{ r.id }}" value="1" aria-label="enabled"
										{% if r == role or role.locked %} disabled{% endif %}
										{% if r in role.included_roles %} checked{% endif %}>
								</div>
							</td>
							<td>
								<a href="{{ url_for("role.show", roleid=r.id) }}">
									{{ r.name }}
								</a>
							</td>
							<td>
								{{ r.description }}
							</td>
							<td>
								{% for group in r.groups_effective|sort(attribute='name') %}
									<a href="{{ url_for("group.show", gid=group.gid) }}">{{ group.name }}</a>{{ ', ' if not loop.last }}
								{% endfor %}
							</td>
						</tr>
						{% endfor %}
					</tbody>
				</table>
			</div>
		</div>
		<div class="tab-pane fade" id="groups" role="tabpanel" aria-labelledby="groups-tab">
			<div class="form-group col">
				<span>{{_("Included groups")}}</span>
				<table class="table table-striped table-sm">
					<thead>
						<tr>
							<th scope="col"></th>
							<th scope="col">{{_("name")}}</th>
							<th scope="col">{{_("description")}}</th>
							<th scope="col">{{_("2FA required")}}</th>
						</tr>
					</thead>
					<tbody>
						{% for group in groups|sort(attribute="name") %}
						<tr id="group-{{ group.gid }}">
							<td>
								<div class="form-check">
									<input class="form-check-input" type="checkbox" id="group-{{ group.gid }}-checkbox" name="group-{{ group.gid }}" value="1" aria-label="enabled" {% if group in role.groups %}checked{% endif %} {{ 'disabled' if role.locked }}>
								</div>
							</td>
							<td>
								<a href="{{ url_for("group.show", gid=group.gid) }}">
									{{ group.name }}
								</a>
							</td>
							<td>
								{{ group.description }}
							</td>
							<td>
								<div class="form-check">
									<input class="form-check-input" type="checkbox" id="group-mfa-{{ group.gid }}-checkbox" name="group-mfa-{{ group.gid }}" value="1" aria-label="enabled" {% if group in role.groups and role.groups[group].requires_mfa %}checked{% endif %} {{ 'disabled' if role.locked }}>
								</div>
							</td>
						</tr>
						{% endfor %}
					</tbody>
				</table>
			</div>
		</div>
	</div>
</div>
</form>
{% endblock %}
