{% extends "base.html" %}
{% load i18n %}
{% load translations %}
{% load permissions %}
{% load crispy_forms_tags %}
{% load humanize %}
{% load icons %}

{% block breadcrumbs %}
{% if object.defining_project %}
  <li><a href="{{ object.defining_project.get_absolute_url }}">{{ object.defining_project }}</a></li>
  <li><a href="{% url 'manage-access' project=object.defining_project.slug %}#teams">{% trans "Access control" %}</a></li>
{% else %}
  <li><a href="{% url 'manage' %}">{% trans "Manage" %}</a></li>
  <li><a href="{% url 'manage-teams' %}">{% trans "Teams" %}</a></li>
{% endif %}
<li><a href="{% url 'team' pk=object.pk %}">{{ object.name }}</a></li>
{% endblock %}

{% block content %}
{% perm "meta:team.users" object as user_can_edit_team_users %}

<form method="POST">
{% csrf_token %}
<ul class="nav nav-pills">
  <li role="presentation" class="active"><a href="#basic" role="tab" data-toggle="tab">{% trans "Settings" %}</a></li>
{% if form %}
  <li role="presentation"><a href="#auto" role="tab" data-toggle="tab">{% trans "Automatic assignments" %}</a></li>
{% endif %}
{% if user_can_edit_team_users %}
  <li role="presentation"><a href="#users" role="tab" data-toggle="tab">{% trans "Members" %}</a></li>
{% endif %}
</ul>

<div class="tab-content">
  <div class="tab-pane active" id="basic">
    {% if form %}
      {{ form | crispy }}
    {% else %}

      <table class="table">
        <tr>
          <th>{% trans "Roles" %}</th>
          <td>{% include "auth/teams-roles.html" with group=object %}</td>
        </tr>
        <tr>
          <th>{% trans "Languages" %}</th>
          <td>{% include "auth/teams-languages.html" with group=object %}</td>
        </tr>
        <tr>
          <th>{% trans "Projects" %}</th>
          <td>{% include "auth/teams-projects.html" with group=object %}</td>
        </tr>
        <tr>
          <th>{% trans "Components" %}</th>
          <td>{% include "auth/teams-components.html" with group=object %}</td>
        </tr>
      </table>
    {% endif %}
  </div>
  {% if form %}
    <div class="tab-pane" id="auto">
      {{ auto_formset | crispy }}
      <div class="multiFieldEmpty hidden">
        {{ auto_formset.empty_form | crispy }}
      </div>
      <button class="btn btn-info add-multifield" data-prefix="autogroup_set">{% trans "Add automatic assignment" %}</button>
      <br/>
      <br/>
      <br/>
    </div>
  {% endif %}
  {% if user_can_edit_team_users %}
    <div class="tab-pane" id="users">
      <div class="panel panel-default">
        <div class="panel-heading">
          <h4 class="panel-title">
            {% documentation_icon 'admin/access' 'groups' right=True %}
            {% trans "Users" %}
          </h4>
        </div>
        <table class="table table-striped">
          <thead>
            <tr>
              <th>{% trans "Username" %}</th>
              <th>{% trans "Full name" %}</th>
              <th>{% trans "Last sign in" %}</th>
              <th></th>
            </tr>
          </thead>
          <tbody>
            {% for user in users %}
              <tr>
                <td>
                  <a href="{{ user.get_absolute_url }}">{{ user.username }}</a>
                  {% if user in admins %}
                    <span class="badge">{% trans "Team administrator" %}</span>
                  {% endif %}
                </td>
                <td>
                  {{ user.full_name }}
                </td>
                <td>
                  {% if user.last_login %}
                    {{ user.last_login|naturaltime }}
                  {% else %}
                    {% trans "Not yet signed in" %}
                  {% endif %}
                </td>
                <td class="number">
                  {% if user in admins %}
                    <a href="#" class="btn btn-link btn-xs link-post" data-action="{{ object.get_absolute_url }}" data-params='{"add_user": 1, "user": "{{ user.username|escapejs }}"}'>
                      {% icon 'account-supervisor.svg' %}
                      {% trans "Revoke administration" %}
                    </a>
                  {% else %}
                    <a href="#" class="btn btn-link btn-xs link-post" data-action="{{ object.get_absolute_url }}" data-params='{"add_user": 1, "make_admin": 1, "user": "{{ user.username|escapejs }}"}'>
                      {% icon 'account-supervisor.svg' %}
                      {% trans "Grant administration" %}
                    </a>
                  {% endif %}
                  <a href="#" data-toggle="modal" data-target="#delete_user_{{ user.id }}" class="btn btn-link btn-xs red" title="{% trans "Remove" %}">
                    {% icon 'delete.svg' %} {% trans "Remove" %}
                  </a>
                </td>
              </tr>
            {% endfor %}
          </tbody>
        </table>
        {% if users.paginator.num_pages > 1 %}
          <div class="panel-footer">
            {% include "paginator.html" with page_obj=users anchor="users" %}
          </div>
        {% endif %}
        <div class="panel-footer">
          <a href="#" data-toggle="modal" data-target="#add_user_popup" class="btn btn-primary">{% trans "Add a user" %}</a>
        </div>
      </div>
    </div>
  {% endif %}
</div>

{% if form %}
  <input type="submit" value="{% trans "Save" %}" class="btn btn-primary" />
{% endif %}
</form>

{% for user in users %}
    <form  method="post" class="inlineform">
      {% csrf_token %}
      <input type="hidden" name="user" value="{{ user.username }}" />
      <div class="modal fade" tabindex="-1" role="dialog" id="delete_user_{{ user.id }}">
        <div class="modal-dialog" role="document">
          <div class="modal-content">
            <div class="modal-header">
              <button type="button" class="close" data-dismiss="modal" aria-label="{% trans "Close" %}"><span aria-hidden="true">&times;</span></button>
              <h4 class="modal-title">{% trans "Are you absolutely sure?" %}</h4>
            </div>
            <div class="modal-body">
              {% blocktrans with user=user.username %}This will remove <b>{{ user }}</b> access to this project.{% endblocktrans %}
            </div>
            <div class="modal-footer">
              <input type="submit" class="btn btn-danger" name="remove_user" value="{% trans "Remove" %}" />
            </div>
          </div><!-- /.modal-content -->
        </div><!-- /.modal-dialog -->
      </div><!-- /.modal -->
    </form>
{% endfor %}

  <form  method="post" class="inlineform">
    {% csrf_token %}
    <div class="modal fade" tabindex="-1" role="dialog" id="add_user_popup">
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="{% trans "Close" %}"><span aria-hidden="true">&times;</span></button>
            {% trans "Add a user" %}
            {% documentation_icon 'admin/access' 'invite-user' %}
          </div>
          <div class="modal-body">
            {{ add_user_form|crispy }}
          </div>
          <div class="modal-footer">
            <input type="submit" class="btn btn-primary" name="add_user" value="{% trans "Add" %}" />
          </div>
        </div><!-- /.modal-content -->
      </div><!-- /.modal-dialog -->
    </div><!-- /.modal -->
  </form>
{% endblock %}
