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

{% 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="#api" data-toggle="tab" id="tab_api">{% trans "API access" %}</a></li>
</ul>

<div class="tab-content">
<div class="tab-pane active" id="users">
{% if object.access_control != object.ACCESS_CUSTOM %}
  <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 login" %}</th>
          {% for group in groups %}
            <th class="vertical-text">{{ group.short_name }}</th>
          {% endfor %}
        </tr>
      </thead>
      <tbody>
        {% for user in all_users %}
          <tr>
            <td><a href="{{ user.get_absolute_url }}">{{ user.username }}</a></td>
            <td>{{ user.full_name }}</td>
            {% if can_edit_user %}
              <td>{{ user.email }}</td>
            {% endif %}
            <td>{% if user.last_login %}{{ user.last_login|naturaltime }}{% else %}{% trans "Not yet signed in" %}{% endif %}</td>
            {% user_permissions user groups %}
            <td>
              <a href="#" data-toggle="modal" data-target="#delete_user_{{ user.id }}" class="btn btn-link btn-xs red" title="{% trans "Remove" %}">
                {% icon 'delete.svg' %}
              </a>
              <form action="{% url "delete-user" project=object.slug %}" 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" value="{% trans "Remove" %}" />
                      </div>
                    </div><!-- /.modal-content -->
                  </div><!-- /.modal-dialog -->
                </div><!-- /.modal -->
              </form>

              {% if not user.last_login %}
                <form action="{% url "resend_invitation" project=object.slug %}" method="post" class="inlineform">
                  {% csrf_token %}
                  <input type="hidden" name="user" value="{{ user.username }}" />
                  <button type="submit" class="btn btn-link btn-xs" title="{% trans "Resend invitation" %}">
                  {% icon 'refresh.svg' %}
                  </button>
                </form>
              {% endif %}
            </td>
          </tr>
        {% 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 %}{% trans "Not yet signed in" %}{% endif %}</td>
            <td colspan="{{groups|length}}">
              <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 %}
      </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>

  <form action="{% url "set-groups" project=object.slug %}" method="post" id="set_groups_form">
    {% csrf_token %}
  </form>

  <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">
            {{ add_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>
    <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_user_form|crispy }}
          </div>
          <div class="panel-footer">
          <input type="submit" class="btn btn-primary" value="{% trans "Invite" %}" />
          </div>
        </div>
      </form>
    </div>
  </div>
{% else %}
  <div class="panel panel-default">
    <div class="panel-heading">
      <h4 class="panel-title">
        {% documentation_icon 'admin/access' 'groups' right=True %}
        {% trans "Users" %}
      </h4>
    </div>
    <div class="panel-body">
      <p class="help-block">{% trans "Weblate user management is turned off, you can change permissions in the Django admin interface." %}</p>
    </div>
  </div>
{% endif %}
</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">
      <tr>
        <th>{% trans "Your personal API key:" %}</th>
        <td>
            {{ user.auth_token.key }}
            <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>
  {% if ssh_key %}
  <div class="panel panel-default">
    <div class="panel-heading">
      <h4 class="panel-title">
        {% documentation_icon 'vcs' right=True %}
        {% trans "SSH key" %}
      </h4>
    </div>
    <div class="panel-body">
      {% include "snippets/ssh-key.html" %}
    </div>
  </div>
  {% endif %}
{% documentation_icon 'vcs' right=True %}
</div>
</div>

{% endblock %}
