{% import "includes/macros" as macros %}
{% extends "base" %}
{% block content %}
    <div class="max-w-screen-lg w-full">
        <h1 class="h1">Mitglieder</h1>
        {% if allowed_to_edit %}
            <details class="mt-5 bg-gray-200 dark:bg-primary-600 p-3 rounded-md">
                <summary class="px-3 cursor-pointer text-md font-bold text-primary-950 dark:text-white">Neue Person hinzufügen</summary>
                <form action="/admin/user/new"
		  onsubmit="return confirm('Willst du wirklich einen neuen Benutzer anlegen?');"
                  method="post"
                  class="flex mt-4 rounded-md sm:flex items-end justify-between">
                <div class="w-full">
                    <div class="grid md:grid-cols-3">
                        <div>
                            <label for="name" class="sr-only">Name</label>
                            <input type="text"
                                   name="name"
                                   class="relative block rounded-md border-0 py-1.5 px-2 text-gray-900 ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:ring-2 focus:ring-inset focus:ring-primary-600 sm:text-sm sm:leading-6 mb-2 md:mb-0"
                                   placeholder="Name" />
                        </div>
                    </div>
                </div>
                <div class="text-right ml-3">
                    <input value="Hinzufügen"
                           type="submit"
                           class="w-28 mt-2 sm:mt-0 rounded-md bg-primary-500 px-3 py-2 text-sm font-semibold text-white hover:bg-primary-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary-600 cursor-pointer" />
                </div>
            </form>
            </details>
            
        {% endif %}
        <!-- START filterBar -->
        <div class="search-wrapper flex">
            <label for="name" class="sr-only">Suche</label>
            <input type="search"
                   name="name"
                   id="filter-js"
                   class="search-bar"
                   placeholder="Suchen nach (Name, [yes|no]-role:<name>, has-[no-]membership-pdf)" />

            <div class="relative">
              <button id="dropdownbtn" data-dropdown="dropdown" class="btn btn-dark ml-3" type="button">
                Sortieren 
              </button>

              <!-- Dropdown menu -->
              <div id="dropdown" class="z-10 hidden bg-white divide-y divide-gray-100 text-secondary-900 rounded-lg shadow-sm w-44 absolute right-0">
                  <ul class="py-2 text-sm" aria-labelledby="dropdownbtn">
                    <li>
                      <a href="./user" class="block px-4 py-2 hover:bg-gray-100 hover:text-secondary-950">Zuletzt eingeloggt</a>
                    </li>
                    <li>
                      <a href="?sort=name&asc" class="block px-4 py-2 hover:bg-gray-100 hover:text-secondary-950">Name A-Z</a>
                    </li>
                    <li>
                      <a href="?sort=name" class="block px-4 py-2 hover:bg-gray-100 hover:text-secondary-950">Name Z-A</a>
                    </li>
                  </ul>
              </div> 
            </div>
        </div>
        <!-- END filterBar -->
        <div id="filter-result-js" class="search-result"></div>
        {% for user in users %}
            <div data-filterable="true"
                 data-filter="{{ user.name }} {% for role in roles %} {% if role.name in user.roles %} yes-role:{{ macros::fancy_role_name(name=role.name) }} {% else %} no-role:{{ role.name }} {% endif %} role-{{ role }} {% endfor %}"
                 class="border-t bg-white dark:bg-primary-900 py-3 px-4 relative">
                <details class="block dark:text-white w-full">
                    <summary>
                        <span class="text-black dark:text-white cursor-pointer">
                            <span class="font-bold">
                                {{ user.name }}
                                {% if not user.last_access and allowed_to_edit and user.mail %}
                                    <form action="/admin/user"
                                          method="post"
                                          enctype="multipart/form-data"
                                          class="inline">
                                    </form>
                                {% endif %}
                                {% if user.last_access %}&bullet; Zuletzt eingeloggt: &nbsp;{{ user.last_access | date() }}{% endif %}
                            </span>
                            <small class="block text-gray-600 dark:text-gray-100">
                                {% for role in user.roles -%}
                                    {{ macros::fancy_role_name(name=role) }}
                                    {%- if not loop.last %},
                                    {% endif -%}
                                {% endfor %}
                            </small>
                        </span>
                    </summary>
                    <form action="/admin/user"
                          method="post"
                          enctype="multipart/form-data"
                          class="w-full mt-2">
                        {% if user.pw %}
                            <a class="block my-1 font-normal text-[#f43f5e] dark:text-primary-200 hover:text-primary-900 dark:hover:text-primary-300 underline"
                               href="/admin/user/{{ user.id }}/reset-pw"
                               onclick="return confirm('Willst du wirklich das Passwort von \'{{ user.name }}\'zurücksetzen?');">Passwort zurücksetzen</a>
                        {% endif %}
                        <div class="w-full grid gap-3 mt-3">
                            <input type="hidden" name="id" value="{{ user.id }}" />
                            <div class="grid sm:grid-cols-2 lg:grid-cols-4 gap-3">
                                {% for role in roles %}
                                    {% if not role.cluster %}
					{% if role.name == "admin" %}
                                        {{ macros::checkbox(label=role.name, name="roles[" ~ role.id ~ "]", id=loop.index , checked=role.name in user.roles, disabled=allowed_to_edit == false, help="Admins können Mitglieder (auf dieser Seite) verwalten") }}
					{% elif role.name == "scheckbuch" %}
                                        {{ macros::checkbox(label=role.name, name="roles[" ~ role.id ~ "]", id=loop.index , checked=role.name in user.roles, disabled=allowed_to_edit == false, help="Anfänger sehen nur Ausfahrten/Events, die explizit für sie ausgeschrieben wurden") }}
					{% elif role.name == "cox" %}
                                        {{ macros::checkbox(label=role.name, name="roles[" ~ role.id ~ "]", id=loop.index , checked=role.name in user.roles, disabled=allowed_to_edit == false, help="Steuerpersonen können selbstständig Ausfahrten ausschreiben und sich bei Events zum steuern anmelden") }}
					{% elif role.name == "manage_events" %}
                                        {{ macros::checkbox(label=role.name, name="roles[" ~ role.id ~ "]", id=loop.index , checked=role.name in user.roles, disabled=allowed_to_edit == false, help="Eventmanager können Events ausschreiben und bearbeiten") }}
					{% else %}
                                        {{ macros::checkbox(label=role.name, name="roles[" ~ role.id ~ "]", id=loop.index , checked=role.name in user.roles, disabled=allowed_to_edit == false) }}

					{% endif %}
                                    {% endif %}
                                {% endfor %}
                                <hr class="sm:col-span-2 lg:col-span-4 my-3" />
                                {{ macros::input(label='Name', name='name', id=loop.index, type="text", value=user.name) }}
                            </div>
                        </div>
                        {% if allowed_to_edit %}
                            <div class="mt-3 text-right">
                                <a href="/admin/user/{{ user.id }}/delete"
                                   class="w-28 btn btn-alert"
                                   onclick="return confirm('Wirklich löschen?');">
                                    {% include "includes/delete-icon" %}
                                    Löschen
                                </a>
                                <input value="Ändern" type="submit" class="w-28 btn btn-primary ml-1" />
                            </div>
                        {% endif %}
                    </form>
                </details>
            </div>
        {% endfor %}
    </div>
{% endblock content %}