{% extends 'base.html' %}

{% block body %}
<form action="{{ url_for("user.update", uid=user.uid) }}" 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("user.index") }}" class="btn btn-secondary">{{_("Cancel")}}</a>
		{% if user.uid %}
			<a href="{{ url_for("mfa.admin_disable", uid=user.uid) }}" class="btn btn-secondary">{{_("Reset 2FA")}}</a>
			<a href="{{ url_for("user.delete", uid=user.uid) }}" onClick='return confirm({{_("Are you sure?")|tojson}});' class="btn btn-danger"><i class="fa fa-trash" aria-hidden="true"></i> {{_("Delete")}}</a>
		{% else %}
			<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="profile-tab" data-toggle="tab" href="#profile" role="tab" aria-controls="profile" aria-selected="true">{{_("Profile")}}</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">{{_("Roles")}}</a>
		</li>
	</ul>
	<div class="tab-content border mb-2 pt-2" id="tabcontent">
		<div class="tab-pane fade show active" id="profile" role="tabpanel" aria-labelledby="roles-tab">
			<div class="form-group col">
				<label for="user-uid">
					{{_('User ID')}}
					{% if user.is_service_user %}
					<span class="badge badge-secondary">{{_('service')}}</span>
					{% endif %}
				</label>
				{% if user.uid %}
				<input type="number" class="form-control" id="user-uid" name="uid" value="{{ user.uid or '' }}" readonly>
				{% else %}
				<input type="text" class="form-control" id="user-uid" name="uid" placeholder="{{_('will be choosen')}}" readonly>
				{% endif %}
			</div>
			{% if not user.uid %}
			<div class="form-group col">
				<div class="form-check">
					<input class="form-check-input" type="checkbox" id="user-serviceaccount" name="serviceaccount" value="1" aria-label="enabled">
					<label class="form-check-label" for="user-serviceaccount">{{_('Service User')}}</label>
				</div>
			</div>
			{% endif %}
			<div class="form-group col">
				<label for="user-loginname">{{_("Login Name")}}</label>
				<input type="text" class="form-control" id="user-loginname" name="loginname" value="{{ user.loginname or '' }}" {% if user.uid %}readonly{% endif %}>
				<small class="form-text text-muted">
					{{_("Only letters, numbers, dashes (\"-\") and underscores (\"_\") are allowed. At most 32, at least 2 characters. There is a word blocklist. Must be unique.")}}
				</small>
			</div>
			{% if not user.uid %}
			<div class="form-group col">
				<div class="form-check">
					<input class="form-check-input" type="checkbox" id="ignore-loginname-blocklist" name="ignore-loginname-blocklist" value="1" aria-label="enabled">
					<label class="form-check-label" for="ignore-loginname-blocklist">{{_('Ignore login name blocklist')}}</label>
				</div>
			</div>
			{% endif %}
			<div class="form-group col">
				<label for="user-loginname">{{_("Display Name")}}</label>
				<input type="text" class="form-control" id="user-displayname" name="displayname" value="{{ user.displayname or '' }}">
				<small class="form-text text-muted">
					{{_("If you leave this empty it will be set to the login name. At most 128, at least 2 characters. No character restrictions.")}}
				</small>
			</div>
			<div class="form-group col">
				<label for="user-mail">{{_("Mail")}}</label>
				<input type="email" class="form-control" id="user-mail" name="mail" value="{{ user.mail or '' }}">
				<small class="form-text text-muted">
					{{_("Do a sanity check here. A user can take over another account if both have the same mail address set.")}}
				</small>
			</div>
			<div class="form-group col">
				<label for="user-loginname">{{_("Password")}}</label>
				{% if user.uid %}
				<input type="password" class="form-control" id="user-password" name="password" placeholder="●●●●●●●●" minlength={{ User.PASSWORD_MINLEN }} maxlength={{ User.PASSWORD_MAXLEN }} pattern="{{ User.PASSWORD_REGEX }}">
				{% else %}
				<input type="password" class="form-control" id="user-password" name="password" placeholder="{{_("mail to set it will be sent")}}" readonly>
				{% endif %}
				<small class="form-text text-muted">
					{{ User.PASSWORD_DESCRIPTION|safe }}
				</small>
			</div>
		</div>
		<div class="tab-pane fade" id="roles" role="tabpanel" aria-labelledby="roles-tab">
			<div class="form-group col">
				<span>{{_("Roles")}}:</span>
				<table class="table table-striped table-sm">
					<thead>
						<tr>
							<th scope="col"></th>
							<th scope="col">{{_("Name")}}</th>
							<th scope="col">{{_("Description")}}</th>
						</tr>
					</thead>
					<tbody>
						{% for role in roles|sort(attribute="name") %}
						<tr id="role-{{ role.id }}">
							<td>
								<div class="form-check">
									<input class="form-check-input" type="checkbox" id="role-{{ role.id }}-checkbox" name="role-{{ role.id }}" value="1" aria-label="enabled"
										{% if user in role.members %}checked {% endif %}
										{% if role.is_default and not user.is_service_user %}disabled {% endif %}>
								</div>
							</td>
							<td>
								<a href="{{ url_for("role.show", roleid=role.id) }}">
									{{ role.name }}
								</a>
							</td>
							<td>
								{{ role.description }}
							</td>
						</tr>
						{% endfor %}
					</tbody>
				</table>
			</div>
			<div class="form-group col">
				<span>{{_("Resulting groups (only updated after save)")}}:</span>
				<table class="table table-striped table-sm">
					<thead>
						<tr>
							<th scope="col">{{_("Name")}}</th>
							<th scope="col">{{_("Description")}}</th>
						</tr>
					</thead>
					<tbody>
						{% for group in user.groups|sort(attribute="name") %}
						<tr id="group-{{ group.gid }}">
							<td>
								<a href="{{ url_for("group.show", gid=group.gid) }}">
									{{ group.name }}
								</a>
							</td>
							<td>
								{{ group.description }}
							</td>
						</tr>
						{% endfor %}
					</tbody>
				</table>
			</div>
		</div>
	</div>
</div>
</form>
{% endblock %}
