<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" layout:decorate="~{template}" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<head>
<title>Camp Organizer Business</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
	<th:block layout:fragment="content">
		<div class="mainpage">
			<div class="card">
				<div class="card-header">
					<div>Nutzerverwaltung für die Abrechnung von Freizeiten</div>
					<div class="alert alert-info">
					  Ist die gewünschte Person nicht in der Liste aller Personen enthalten, fehlt ihr vermutlich die passende Rolle in Keycloak.
					</div>
				</div>
				<div class="card-body">
					<table id="privs" class="table table-striped">
						<thead>
							<tr>
								<th scope="col">Freizeit</th>
								<th scope="col">Abrechnung darf bearbeiten</th>
								<th scope="col"></th>
							</tr>
						</thead>
						<tbody>
							<th:block th:each="e : ${privileges.entrySet()}" sec:authorize="hasRole('admin')">
								<tr>
									<td class="middled"><span th:text="${e.value.name}"></span>&nbsp;<span th:text="${#numbers.formatDecimal(e.value.year, 1, 0)}"></span></td>
									<td><th:block th:each="p : ${e.value.profiles}">
											<div class="dropdown" style="display: inline" th:if="${p.pk != null}">
												<button class="btn dropdown-toggle" style="border: 1px solid silver" type="button" th:id="'btn_' + ${e.key} + '_' + ${p.pk}" data-bs-toggle="dropdown" aria-expanded="false">
													<span th:text="${p.forename} + ' ' + ${p.surname}"></span>
												</button>
												<ul class="dropdown-menu silver-to-gray-gradient" th:aria-labelledby="'btn_' + ${e.key} + '_' + ${p.pk}">
													<li><div style="padding: 8px">
															Login: <span th:text="${p.username}"></span>
														</div></li>
													<li><div style="padding: 8px">
															Ablaufdatum: <span th:text="${#temporals.format(p.duedate, 'dd.MM.yyyy')}"></span>
														</div></li>
													<li><hr class="dropdown-divider" th:if="${p.username != currentUser}"></li>
													<li><a class="dropdown-item menu-danger" th:if="${p.username != currentUser}" th:href="@{/business/privileges/delete?fkCamp={c}&fkProfile={r}(c=${e.key},r=${p.pk})}">Recht
															entziehen</a></li>
												</ul>
											</div>
										</th:block></td>
									<td>
										<form action="#" th:action="@{/business/privileges/add?fkCamp={cid}(cid=${e.key})}" th:object="${bean}" method="post">
											<div class="container">
												<div class="row">
													<div class="col-sm-9">
													 	<div th:if="${#fields.hasErrors('fkProfile')}" th:errorclass="error" th:errors="*{fkProfile}"></div>
														<select th:id="${e.value.pk}" class="form-control select2-single" th:field="*{fkProfile}">
															<option value="">Auswählen</option>
															<th:block th:each="u : ${profiles}">
																<option th:if="${u != null}" th:value="${u.pk}" th:text="${u.fullname}" th:title="${u.dropdown()}" />
															</th:block>
														</select>
													</div>
													<div class="col-sm-3">
														<input type="submit" class="btn btn-outline-primary" value="Recht erteilen">
													</div>
												</div>
											</div>
											<script>
												var id = "[[${e.value.pk}]]";
												$("#" + id).select2({
													theme : 'bootstrap-5',
												});
											</script>
										</form>
									</td>
								</tr>
							</th:block>
						</tbody>
					</table>
					<script>
						$(document).ready(function() {
							$("#privs").DataTable({
								language : locale_de
							});
						});
					</script>
				</div>
			</div>
		</div>
	</th:block>
</body>
</html>