<?php
use \sven\sys\core as CORE;
CORE::setLogin(true);
CORE::addReplacement('head', 'TITLE', 'Benutzerverwaltung');

$mysql = new \sven\sys\mysql\mysql();
$fluent = $mysql->getBuilder();

$tableString = "";
if($auth->loggedIn()) {
	$query = $fluent->from('t_benutzer')->select('t_benutzer.*');
	$rows = $query->fetchAll();
	foreach ($rows as $row) {
		$banned = ($row->banned === 1) ? "Ja" : "Nein";
		$tableString .= "<tr class='table-py-2'>";
			$tableString .= "<td>{$row->ID}</td>";
			$tableString .= "<td>{$row->Name}</td>";
			$tableString .= "<td>{$banned}</td>";
			$tableString .= "<td>";
				$tableString .= '<button type="button" class="btn btn-warning" data-toggle="modal" data-user="'.$row->Name.'" data-target="#modalChangePassword">Passwort ändern</button>';
				$tableString .= '<span class="mx-2"></span>';
				$tableString .= '<button type="button" class="btn btn-danger" data-toggle="modal" data-user="'.$row->Name.'" data-target="#modalDeleteUser">Benutzer löschen</button>';
			$tableString .= "</td>";
		$tableString .= "</tr>";
	}
}


CORE::addReplacement('main', 'FILE', <<<EOF
<div class="container" id="users_page">
						<div class="row">
							<div class="col-md-12">
								<h2>Benutzerverwaltung</h2>

								<table class="mb-md-2">
									<thead>
										<tr>
											<th> ID </th>
											<th> Benutzername </th>
											<th> Gebannt </th>
											<th> Aktionen </th>
										</tr>
									</thead>
									<tbody>
										$tableString
									</tbody>
								</table>
								<div class="float-right">
									<button type="button" class="btn btn-info" data-toggle="modal" data-target="#modalAddUser">Benutzer hinzufügen</button>
								</div>

								<div class="modal fade" id="modalChangePassword" role="dialog">
									<div class="modal-dialog">
										<div class="modal-content">
											<div class="modal-header">
												<h4 class="modal-title">Passwort ändern</h4>
												<button type="button" class="close" data-dismiss="modal">&times;</button>
											</div>
											<div class="modal-body">
												<p>
													Möchtest du wirklich das Passwort ändern für
													"<span class="m-cp-span-user"></span>"?
												</p>
												<form name="m_cp_form" id="m_cp_form">
													<div class="form-group">
														<label for="change_username" class="col-form-label">Benutzername</label>
														<input type="text" class="form-control" id="change_username" name="username" disabled>
													</div>

													<div class="form-group">
														<label for="password" class="col-form-label">Neues Passwort</label>
														<input type="text" class="form-control" id="password" name="password">
													</div>

													<div class="form-group">
														<button type="submit" class="btn btn-primary float-right">Abschicken</button>
													</div>
												</form>
											</div>
											<div class="modal-footer">
												<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
											</div>
										</div>
									</div>
								</div>

								<div class="modal fade" id="modalDeleteUser" role="dialog">
									<div class="modal-dialog">
										<div class="modal-content">
											<div class="modal-header">
												<h4 class="modal-title">Benutzer löschen</h4>
												<button type="button" class="close" data-dismiss="modal">&times;</button>
											</div>
											<div class="modal-body">
												<p>
													Möchtest du wirklich den Benutzer "<span id="m-du-span-user"></span>" löschen?
												</p>
												<form name="m_du_form" id="m_du_form">
													<div class="form-group">
														<label for="del_username" class="col-form-label">Benutzername</label>
														<input type="text" class="form-control" id="del_username" name="username" disabled>
													</div>
													<div class="form-group">
														<button type="submit" class="btn btn-primary float-right">Abschicken</button>
													</div>
												</form>
											</div>
											<div class="modal-footer">
												<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
											</div>
										</div>
									</div>
								</div>

								<div class="modal fade" id="modalAddUser" role="dialog">
									<div class="modal-dialog">
										<div class="modal-content">
											<div class="modal-header">
												<h4 class="modal-title">Benutzer hinzufügen</h4>
												<button type="button" class="close" data-dismiss="modal">&times;</button>
											</div>
											<div class="modal-body">
												<form name="m_au_form" id="m_au_form">
													<div class="form-group">
														<label for="add_username" class="col-form-label">Benutzername</label>
														<input type="text" class="form-control" id="add_username" name="username">
													</div>
													<div class="form-group">
														<label for="add_password" class="col-form-label">Passwort</label>
														<input type="password" class="form-control" id="add_password" name="password">
													</div>
													<div class="form-group">
														<button type="submit" class="btn btn-primary float-right">Abschicken</button>
													</div>
												</form>
											</div>
											<div class="modal-footer">
												<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
											</div>
										</div>
									</div>
								</div>
							</div>
						</div>
					</div>
EOF
);
?>
