{% extends 'realm/realm_detailed.jinja2' %}
{% import 'macros/form_macros.jinja2' as mform %}

{% block detail_content %}
    {% if user.user %}
        {% if user.deleted_user.deletion_date %}
            <h3 class="text-danger">{{ user.user.username }}
                <small>Nutzer wird vorraussichtlich am {{ user.deleted_user.deletion_date.strftime('%d.%m.%Y') }}
                    gelöscht
                </small>
            </h3>
        {% else %}
            <h3>{{ user.user.username }}</h3>
        {% endif %}

    {% else %}
        <h3>{{ user.username }}</h3>
    {% endif %}
    {% if not form %}
        <ul class="list-group list-group-flush w-100">
            <li class="list-group-item">Ldap Domain: {{ user.user.dn }}</li>
            <li class="list-group-item"> Anzeigename:
                {% if user.user.display_name %}
                    {{ user.user.display_name }}
                {% else %}
                    <span class="text-warning"> Noch nicht generiert </span>
                {% endif %}
            </li>
            <li class="list-group-item">Vorname: {{ user.user.first_name }}</li>
            <li class="list-group-item">Nachname: {{ user.user.last_name }}</li>
            <li class="list-group-item">Email: {{ user.user.email }}</li>
            <li class="list-group-item">Passwort: <a
                    href="{{ url('realm-user-password-reset', args = [realm.id, user.user.dn]) }}" class="float-right">Nutzerpasswort
                zurücksetzen</a></li>
            <li class="list-group-item">Telefon: {{ user.user.phone }}</li>
            <li class="list-group-item">Mobiltelefon: {{ user.user.mobile_phone }}</li>
            <li class="list-group-item">Gruppen:
                {% if groups %}
                    {% for group in groups %}
                        <a href="{{ url('realm-group-detail', args=[realm.id, group.dn]) }}"
                           data-toggle="tooltip"
                           data-placement="right"
                           title="{{ group.description }}"
                           class="badge badge-secondary p-1">{{ group.name }}</a>
                    {% endfor %}
                {% else %}
                    <span class="text-warning">Keine zugewiesen</span>
                {% endif %}
                {% if not user.deleted_user.deletion_date %}
                    <a href="{{ url('realm-user-group-update', args=[realm.id, user.user.dn]) }}" class="float-right">
                        Gruppen zuweisen</a>
                {% endif %}
            </li>
            <li class="list-group-item">Zuletzt eingeloggt:
                {% if user.user.last_login %}
                    {{ user.user.last_login.strftime('%d.%m.%Y') }}
                {% else %}
                    <i class="far fa-times-circle text-danger"></i><span class="d-none">+</span>
                {% endif %}</li>
            {% if user.deleted_user.deletion_date %}
                <li class="list-group-item text-danger">
                    Löschvorgang: {{ user.deleted_user.deletion_date.strftime('%d.%m.%Y') }}
                    <a href="{{ url('realm-user-delete-cancel', args=[realm.id, user.user.dn]) }}"
                       class="float-right">
                        Löschvorgang abbrechen</a>
                </li>
            {% endif %}
        </ul>
        <div class="d-flex mt-3">
            {% if not user.deleted_user.deletion_date %}
                <a href="{{ url('realm-user-update', args = [realm.id, user.user.dn]) }}"
                   class="btn btn-primary mr-auto p-2">
                    <i class="fas fa-user-cog"></i> Nutzer bearbeiten
                </a>

                {% if not user.user.last_login %}
                    <a href="{{ url('realm-user-resend-welcome-mail', args = [realm.id, user.user.dn]) }}"
                       class="btn btn-secondary p-2 mr-2">
                        <i class="fas fa-paper-plane"></i> Wilkommensmail erneut senden
                    </a>
                {% endif %}

                <a href="{{ url('realm-user-delete-confirm', args = [realm.id, user.user.dn]) }}"
                   class="btn btn-danger p-2">
                    <i class="fas fa-trash"></i> Nutzer löschen
                </a>
            {% endif %}
        </div>
    {% else %}
        <form method="post">
            <input type="hidden" name="csrfmiddlewaretoken" value="{{ csrf_token }}">
            {{ mform.text_input(form.username) }}
            {{ mform.email_input(form.email) }}
            {{ mform.text_input(form.first_name) }}
            {{ mform.text_input(form.last_name) }}
            <div class="d-flex mt-4">
                <button type="submit" class="btn btn-primary mr-auto p-2">Speichern</button>
                <a href="{{ url('realm-user-detail', args = [realm.id, user.dn]) }}"
                   class="btn btn-secondary p-2">Abbrechen</a>
            </div>
        </form>
    {% endif %}
{% endblock %}
