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

{% block breadcrumbs %}
<li><a href="{{ object.get_absolute_url }}">{{ object }}</a></li>
<li><a href="{% url 'manage-access' project=object.slug %}">{% trans "Access control" %}</a></li>
{% endblock %}

{% block content %}

{% perm 'user.edit' as can_edit_user %}

<ul class="nav nav-pills">
  <li class="active"><a href="#users" data-toggle="tab" id="tab_users">{% trans "Users" %}</a></li>
  <li><a href="#teams" data-toggle="tab" id="tab_teams">{% trans "Teams" %}</a></li>
  <li><a href="#api" data-toggle="tab" id="tab_api">{% trans "API access" %}</a></li>
</ul>

<div class="tab-content">
<div class="tab-pane active" 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>
          {% if can_edit_user %}
            <th>{% trans "E-mail" %}</th>
          {% endif %}
          <th>{% trans "Last sign in" %}</th>
          <th>{% trans "Teams" %}</th>
          <th></th>
        </tr>
      </thead>
      <tbody>
        {% for user in all_users %}
          {% include "trans/project-access-row.html" %}
        {% endfor %}
        {% for userblock in blocked_users %}
          <tr>
            <td><a href="{{ userblock.user.get_absolute_url }}">{{ userblock.user.username }}</a></td>
            <td>{{ userblock.user.full_name }}</td>
            <td>{{ userblock.user.email }}</td>
            <td>
              {% if userblock.user.last_login %}
                {{ userblock.user.last_login|naturaltime }}
              {% else %}
                <em>{% trans "Not yet signed in" %}</em>
              {% endif %}
            </td>
            <td>
              <strong>
                {% if userblock.expiry %}
                  {% blocktrans with expiry=userblock.expiry|date:"DATE_FORMAT" %}Blocked until {{ expiry }}{% endblocktrans %}
                {% else %}
                  {% trans "Blocked permanently" %}
                {% endif %}
              <strong>
            </td>
            <td>
              <form action="{% url "unblock-user" project=object.slug %}" method="post" class="inlineform">
                {% csrf_token %}
                <input type="hidden" name="user" value="{{ userblock.user.username }}" />
                <button type="submit" class="btn btn-link btn-xs" title="{% trans "Unblock user" %}">
                {% icon 'close.svg' %}
                </button>
              </form>
            </td>
          </tr>
        {% endfor %}
        {% for invitation in invitations %}
          <tr>
            {% if invitation.user %}
              <td><a href="{{ invitation.user.get_absolute_url }}">{{ invitation.user.username }}</a></td>
              <td>{{ invitation.user.full_name }}</td>
              {% if can_edit_user %}
                <td>{{ invitation.user.email }}</td>
              {% endif %}
            {% else %}
              <td colspan="{% if can_edit_user %}3{% else %}2{% endif %}">
                <em>{% blocktrans with email=invitation.email %}Invitation for {{ email }}{% endblocktrans %}</em>
              </td>
            {% endif %}
            <td><em>{% trans "Pending invitation" %}</em></td>
            <td>
              <span class="badge">{{ invitation.group }}</span>
            </td>
            <td>
              <form action="{{ invitation.get_absolute_url }}" method="post" class="inlineform">
                {% csrf_token %}
                <input type="hidden" name="action" value="resend" />
                <button type="submit" class="btn btn-link btn-xs" title="{% trans "Resend invitation" %}">
                {% icon 'refresh.svg' %}
                </button>
              </form>
              <button type="button" class="btn btn-link btn-xs" data-clipboard-text="{{ site_url }}{{ invitation.get_absolute_url }}" data-clipboard-message="{% trans "Invitation link copied to clipboard." %}" title="{% trans "Copy to clipboard" %}">{% icon "copy.svg" %}</button>
              <form action="{{ invitation.get_absolute_url }}" method="post" class="inlineform">
                {% csrf_token %}
                <input type="hidden" name="action" value="remove" />
                <button type="submit" class="btn btn-link btn-xs red" title="{% trans "Remove invitation" %}">
                {% icon 'delete.svg' %}
                </button>
              </form>
            </td>
          </tr>
        {% endfor %}
      </tbody>
    </table>
    <div class="panel-body">
      <p class="help-block">{% trans "Once all its permissions are removed, the user will be removed from the project." %}</p>
    </div>
  </div>

  <div class="row">
    <div class="col-sm-6">
      <form action="{% url "add-user" project=object.slug %}" method="post">
        {% csrf_token %}
        <div class="panel panel-default">
          <div class="panel-heading"><h4 class="panel-title">
            {% documentation_icon 'admin/access' 'invite-user' right=True %}
            {% trans "Add a user" %}
          </h4></div>
          <div class="panel-body">
            {{ invite_user_form|crispy }}
          </div>
          <div class="panel-footer">
          <input type="submit" class="btn btn-primary" value="{% trans "Add" %}" />
          </div>
        </div>
      </form>
      <form action="{% url "block-user" project=object.slug %}" method="post" id="block-user">
        {% csrf_token %}
        <div class="panel panel-default">
          <div class="panel-heading"><h4 class="panel-title">
            {% documentation_icon 'admin/access' 'block-user' right=True %}
            {% trans "Block user" %}
          </h4></div>
          <div class="panel-body">
            {{ block_user_form|crispy }}
          </div>
          <div class="panel-footer">
          <input type="submit" class="btn btn-danger" value="{% trans "Block" %}" />
          </div>
        </div>
      </form>
    </div>
    {% if invite_email_form %}
    <div class="col-sm-6">
      <form action="{% url "invite-user" project=object.slug %}" method="post">
        {% csrf_token %}
        <div class="panel panel-default">
          <div class="panel-heading"><h4 class="panel-title">
            {% documentation_icon 'admin/access' 'invite-user' right=True %}
            {% trans "Invite new user" %}
          </h4></div>
          <div class="panel-body">
            {{ invite_email_form|crispy }}
          </div>
          <div class="panel-footer">
          <input type="submit" class="btn btn-primary" value="{% trans "Invite" %}" />
          </div>
        </div>
      </form>
    </div>
    {% endif %}
  </div>
</div>

<div class="tab-pane" id="teams">
  <div class="panel panel-default">
    <div class="panel-heading">
      <h4 class="panel-title">
        {% documentation_icon 'admin/access' 'groups' right=True %}
        {% trans "Teams" %}
      </h4>
    </div>
    <table class="table table-striped">
      <thead>
        <tr>
          <th>{% trans "Name" %}</th>
          <th>{% trans "Roles" %}</th>
          <th>{% trans "Languages" %}</th>
          <th>{% trans "Components" %}</th>
          <th>{% trans "Members" %}</th>
          <th></th>
        </tr>
      </thead>
      <tbody>
        {% for group in groups %}
        <tr>
          <td>
            {{ group.name }}
          </td>
          <td>
            {% include "auth/teams-roles.html" %}
          </td>
          <td>
            {% include "auth/teams-languages.html" %}
          </td>
          <td>
            {% include "auth/teams-components.html" %}
          </td>
          <td class="number">
            {% include "auth/teams-count.html" %}
          </td>
          <td>
            <a href="{{ group.get_absolute_url }}" class="btn btn-link btn-xs" title="{% trans "Edit" %}">
              {% icon 'pencil.svg' %}
            </a>
            <a href="#" data-toggle="modal" data-target="#delete_group_{{ group.id }}" class="btn btn-link btn-xs red" title="{% trans "Delete" %}">
              {% icon 'delete.svg' %}
            </a>
            <form action="{{ group.get_absolute_url }}" method="post" class="inlineform">
              {% csrf_token %}
              <input type="hidden" name="next" value="{{request.get_full_path}}" />
              <div class="modal fade" tabindex="-1" role="dialog" aria-labelledby="delete_group_title_{{ group.id }}" aria-describedby="delete_group_body_{{ group.id }}" id="delete_group_{{ group.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" id="delete_group_title_{{ group.id }}">{% trans "Are you absolutely sure?" %}</h4>
                    </div>
                    <div class="modal-body" id="delete_group_body_{{ group.id }}">
                      {% blocktrans with name=group.name %}This will delete the team <b>{{ name }}</b> from this project.{% endblocktrans %}
                      {% if group.user__count %}
                        {% blocktrans count count=group.user__count %}This team contains {{count}} member. Deleting the team might affect their access to the project.{% plural %}This team contains {{count}} members. Deleting the team might affect their access to the project.{% endblocktrans %}
                      {% endif %}
                    </div>
                    <div class="modal-footer">
                      <input type="submit" class="btn btn-danger" name="delete" value="{% trans "Delete" %}" />
                    </div>
                  </div><!-- /.modal-content -->
                </div><!-- /.modal-dialog -->
              </div><!-- /.modal -->
            </form>
          </td>
        </tr>
        {% endfor %}
      <tbody>
    </table>
  </div>
  <form action="{% url "create-project-group" project=object.slug %}" method="post">
    <div class="panel panel-default">
      <div class="panel-heading">
        <h4 class="panel-title">
          {% documentation_icon 'admin/access' 'groups' right=True %}
          {% trans "Create new team" %}
        </h4>
      </div>
      <div class="panel-body">
        {% crispy create_team_form %}
      </div>
      <div class="panel-footer">
        <input type="submit" class="btn btn-primary" value="{% trans "Add" %}" />
      </div>
    </div>
  </form>
</div>

<div class="tab-pane" id="api">
  <div class="panel panel-default">
    <div class="panel-heading">
      <h4 class="panel-title">
        {% documentation_icon 'api' right=True %}
        {% trans "API access" %}
      </h4>
    </div>
    <table class="table table-striped">
      <thead>
        <tr>
          <th>{% trans "Name" %}</th>
          <th>{% trans "Expires" %}</th>
          <th>{% trans "Teams" %}</th>
          <th></th>
        </tr>
      </thead>
      <tbody>
        {% for user in project_tokens %}
          {% include "trans/project-access-row.html" %}
        {% endfor %}
      <tbody>
    </table>
  </div>

  <div class="row">
    <div class="col-sm-6">
      <form action="{% url "create-project-token" project=object.slug %}" method="post">
        {% csrf_token %}
        <div class="panel panel-default">
          <div class="panel-heading"><h4 class="panel-title">
            {% documentation_icon 'api' right=True %}
            {% trans "Create a project token" %}
          </h4></div>
          <div class="panel-body">
            {{ create_project_token_form|crispy }}
          </div>
          <div class="panel-footer">
          <input type="submit" class="btn btn-primary" value="{% trans "Create" %}" />
          </div>
        </div>
      </form>
    </div>
    <div class="col-sm-6">
      <div class="panel panel-default">
        <div class="panel-heading">
          <h4 class="panel-title">
            {% documentation_icon 'api' right=True %}
            {% trans "Personal API key" %}
          </h4>
        </div>
        <table class="table table-striped">
          <tr>
            <th>{% trans "Your personal API key:" %}</th>
            <td>
                <span class="auth-token">{{ user.auth_token.key }}</span>
                <button type="button" class="btn btn-link btn-xs" data-visibility=".auth-token" title="{% trans "Toggle API key visibility" %}">{% icon "eye.svg" %}</button>
                <button type="button" class="btn btn-link btn-xs" data-clipboard-text="{{ user.auth_token.key }}" data-clipboard-message="{% trans "API key copied to clipboard." %}" title="{% trans "Copy to clipboard" %}">{% icon "copy.svg" %}</button>
            </td>
          </tr>
        </table>
        <div class="panel-footer">
          <p class="help-block">{% trans "This is your personal API key, do not share it with anybody else." %}</p>
        </div>
      </div>
    </div>
  </div>
  {% include "snippets/ssh-key.html" %}
{% documentation_icon 'vcs' right=True %}
</div>
</div>

{% endblock %}
