
{% extends "base.html.twig" %}

{% block title %}Użytkownicy - {{ setting('title') }}{% endblock %}

{% block content %}
<hgroup class="page-header">
	<h1><i class="fas fa-user"></i> Użytkownicy</h1>
	<a href="{{ path('user_add') }}" class="btn btn-primary" title="Dodaj użytkownika">Dodaj użytkownika</a>
</hgroup>

{% include 'partial/alert.html.twig' %}

<form method="get" role="form" class="row g-2 mb-5">
	<div class="col-xl-5 col-sm-6">
		<label for="username">Login:</label>
		<input type="text" name="username" title="Login" placeholder="Login" value="{{ findBy.username }}" class="form-control">
	</div>
	<div class="col-xl-5 col-sm-6">
		<label for="email">Adres email:</label>
		<input type="text" name="email" title="Adres email" placeholder="Adres email" value="{{ findBy.email }}" class="form-control">
	</div>
	<div class="col-xl-2 col-sm-6">
		<label for="role">Rola:</label>
		<select name="role" title="Rola" class="form-select">
			<option value="">Wszyscy</option>
			<option value="admin" {% if findBy.role=='admin' %}selected{% endif %}>Admin</option>
		</select>
	</div>
	<div class="col-xl-6 col-sm-6">
		<label for="date">Data rejestracji:</label>
		<div class="input-group">
			<input name="date_from" title="Data początkowa" type="date" max="{{ 'now'|date('Y-m-d') }}" value="{{ findBy.date_from }}" class="form-control">
			<div class="input-group-append input-group-prepend">
				<span class="input-group-text"> - </span>
			</div>
			<input name="date_to" title="Data końcowa" type="date" max="{{ 'now'|date('Y-m-d') }}" value="{{ findBy.date_to }}" class="form-control">
		</div>
	</div>
	<div class="col-xl-3 col-sm-6">
		<label for="ip">IP rejestracji:</label>
		<input type="text" name="ip" title="Adres IP" placeholder="Adres IP" value="{{ findBy.ip }}" class="form-control">
	</div>
	<div class="col-xl-3 col-sm-6">
		<label>&nbsp;</label><br>
		<input type="submit" value="Szukaj" class="btn btn-primary text-uppercase w-100">
	</div>
</form>

{% if users|length %}
	<div class="table-responsive">
		<table class="table table-striped table-bordered table-sm select-all-checkbox-parent">
			<tr>
				<th scope="col" class="text-center"><input type="checkbox" class="select-all-checkbox" title="Wybierz wszystkich"></th>
				<th scope="col" class="text-center{% if users.isSorted('u.id') %} table-active{% endif %}">
          {{ knp_pagination_sortable(users, 'ID', 'u.id') }}
        </th>
				<th scope="col" class="text-center{% if users.isSorted('u.username') %} table-active{% endif %}">
          {{ knp_pagination_sortable(users, 'Login', 'u.username') }}
        </th>
				<th scope="col" class="text-center{% if users.isSorted('u.email') %} table-active{% endif %}">
          {{ knp_pagination_sortable(users, 'Adres email', 'u.email') }}
        </th>
				<th scope="col">Rola</th>
				<th scope="col">Data rejestracji</th>
				<th scope="col">Ostatnie logowanie</th>
				<th scope="col" class="text-center">Edytuj</th>
				<th scope="col" class="text-center">Usuń</th>
			</tr>
			{% for user in users %}
				<tr>
					<td class="text-center">
						<input type="checkbox" name="checkbox_multiple[ids][]" value="{{ user.id }}" form="form_selected_users" class="select-checkbox">
					</td>
					<th scope="row" class="text-center">{{ user.id }}</th>
					<td>
						{% if user.username %}
							<b>{{ user.username }}</b>
						{% endif %}
					</td>
					<td>
						<a href="mailto:{{ user.email }}" title="{{ user.email }}">{{ user.email }}</a>
					</td>
					<td>
						{{ user.roles|join('<br>')|raw }}
					</td>
					<td class="text-nowrap">
						{{ user.date|date('Y-m-d H:i') }}<br>
						{{ user.ip }}
					</td>
					<td class="text-nowrap">
						{% if user.logs|length %}
							{{ (user.logs|last).date|date('Y-m-d H:i') }}<br>
							{{ (user.logs|last).ip }}
						{% endif %}
					</td>
					<td class="text-center"><a href="{{ path('user_edit',{'id':user.id}) }}" class="btn btn-link" title="Edytuj"><i class="fas fa-edit"></i></a></td>
					<td class="text-center">
						{% if user.id != app.user.id %}
							<button class="btn btn-link text-danger" data-bs-toggle="modal" data-bs-target="#remove_{{ user.id }}" aria-label="Usuń" title="Usuń"><i class="fas fa-trash-alt"></i></button>
						{% endif %}
					</td>
				</tr>
			{% endfor %}
		</table>
	</div>

  {% for user in users %}
    {% if user.id != app.user.id %}
      <div class="modal fade" id="remove_{{ user.id }}" tabindex="-1" role="dialog" aria-labelledby="titleModal{{ user.id }}">
        <div class="modal-dialog" role="document">
          <div class="modal-content">
            <div class="modal-header">
              <h5 class="modal-title" id="titleModal{{ user.id }}">Usuń {{ user.username }}</h5>
              <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Anuluj"></button>
            </div>
            <form method="post" action="{{ path('user_delete', { id: user.id }) }}">
              <input type="hidden" name="token" value="{{ csrf_token('delete-user') }}"/>
              <div class="modal-body">
                <p class="mb-1">Na pewno chcesz usunąć: {{ user.username }}?</p>
              </div>
              <div class="modal-footer">
                <input type="button" class="btn btn-secondary" data-bs-dismiss="modal" value="Anuluj">
                <input type="submit" class="btn btn-danger" value="Usuń">
              </div>
            </form>
          </div>
        </div>
      </div>
    {% endif %}
  {% endfor %}

	{{ form_start(form,  {'attr': {'id': 'form_selected_users', 'class': 'd-flex d-flex align-items-center mt-3 mb-3'}}) }}
		<div class="d-flex align-items-center g-2 mb-3">
			{{ form_label(form.type) }}
			{{ form_widget(form.type, {'attr': {'class': 'mx-2'}}) }}
		</div>
		{{ form_row(form.submit, {'attr': {'class': 'btn-danger'}}) }}
	{{ form_end(form) }}

	<p>Pokazuje {{ users|length }} z {{ users.getTotalItemCount }} rekordów</p>
	{{ knp_pagination_render(users) }}
{% else %}
	<h5 class="text-danger">Nic nie znaleziono</h5>
{% endif %}

{% endblock %}
