{% extends 'admin.html' %}
{% from 'include/datatables.html' import datatables %}
{% block title %}
  {% trans %}User management{% endtrans %}
{% endblock %}
{% set admin_nav_active="users" %}
{% block content %}
  {% from 'include/modal_dialog.html' import modal_dialog, button_confirm, modal_confirm %}
  <a class="btn btn-success mb-2"
     href="{{ url_for('admin','users', 'new')}}">{% trans %}Add user{% endtrans %}</a>
  <!-- Users View -->
  {% set filter_buttons = [
  (_('Admins'), 2, 'admin'),
  (_('LDAP'), 0, 'LDAP')] %}
  {% set role_class = {0:'danger', 5:'warning', 10:'info'} %}
  {% set role_choices = dict(form.role.choices) %}
  {% set mfa_choices = dict(form.mfa.choices) %}
  {% set report_time_range_choices = dict(form.report_time_range.choices) %}
  {% call datatables(_('User list'), filter_buttons)%}
    <thead>
      <tr>
        <th scope="col">{{ form.username.label.text  }}</th>
        <th scope="col">{{ form.fullname.label.text  }}</th>
        <th scope="col">{{ form.email.label.text  }}</th>
        <th scope="col">{{ form.role.label.text  }}</th>
        <th scope="col">{{ form.user_root.label.text  }}</th>
        <th scope="col">{{ form.mfa.label.text }}</th>
        <th scope="col">{{ form.report_time_range.label.text }}</th>
        <th scope="col" data-sortable="false"></th>
      </tr>
    </thead>
    <tbody>
      {% for user in users %}
        <tr>
          <td>
            <a href="{{ url_for('admin','users','edit', user.username)}}">{{ user.username }}</a>
            <span class="badge badge-secondary">#{{ user.userid }}</span>
            {% if ldap_enabled and user.is_ldap %}
              <span class="badge badge-secondary">{% trans %}LDAP{% endtrans %}</span>
            {% endif %}
            {% if user.username == username %}
              <span class="badge badge-success">{% trans %}It's you{% endtrans %}</span>
            {% endif %}
          </td>
          <td>{{ user.fullname }}</td>
          <td>{{ user.email }}</td>
          <td>
            <span class="badge badge-{{ role_class.get(user.role)}}">{{ role_choices.get(user.role) }}</span>
          </td>
          <td>
            {{ user.user_root }}
            {% if not user.valid_user_root() %}
              <span class="text-danger">
                <i class="fa fa-exclamation-triangle" aria-hidden="true"></i>
                {% trans %}Root directory not accessible!{% endtrans %}
              </span>
            {% endif %}
          </td>
          <td>{{ mfa_choices.get(user.mfa) }}</td>
          <td>{{ report_time_range_choices.get(user.report_time_range) }}</td>
          <td class="text-right">
            <div class="btn-hover">
              {# Dashboard #}
              <a class="btn btn-secondary"
                 href="{{ url_for('status', user.username, '/') }}">{% trans %}Overview{% endtrans %}</a>
              {# Edit button #}
              <a class="btn btn-secondary"
                 href="{{ url_for('admin','users','edit', user.username)}}">
                {% trans %}Edit{% endtrans %}
              </a>
              {# Delete button #}
              {% if user.username != username %}
                {{ button_confirm(label=_('Delete'), target="#delete-user-modal", username=user.username, url=url_for('admin', 'users', 'delete')) }}
              {% else %}
                <button type="button" class="btn btn-danger" disabled>{% trans %}Delete{% endtrans %}</button>
              {% endif %}
            </div>
          </td>
        </tr>
      {% endfor %}
    </tbody>
  {% endcall %}
  <!-- Delete User Modal -->
  {{ modal_confirm(
  id="delete-user-modal",
  title=_('Delete User'),
  message=_("Are you sure you want to delete this User?"),
  fields=['username'],
  submit=_('Delete')) }}
{% endblock content %}
