{% 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 "Manage users" %}</a></li>
{% endblock %}

{% block content %}

{% 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>
  <th>{% trans "E-mail" %}</th>
  <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>{{ user.username }}</td>
  <td>{{ user.full_name }}</td>
  <td>{{ user.email }}</td>
  <td>{% if user.last_login %}{{ user.last_login|naturaltime }}{% else %}{% trans "Not yet logged in" %}{% endif %}</td>
  {% user_permissions user groups %}
  <td>
<form action="{% url "delete-user" project=object.slug %}" method="post">
{% csrf_token %}
<input type="hidden" name="user" value="{{ user.username }}" />
<button type="submit" class="btn btn-link btn-xs red" title="{% trans "Remove" %}">
{% icon 'delete.svg' %}
</button>
</form>
  </td>
  </tr>
{% endfor %}
  </tbody>
  </table>
  <div class="panel-body">
  <p class="helptext">{% 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">
  {% 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>
</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">
  {% 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="helptext">{% trans "Weblate user management is turned off, you can change permissions in the Django admin interface." %}</p>
  </div>
</div>
{% endif %}

{% perm 'billing:project.permissions' object as user_can_edit_access_control %}

<form action="{% url "change-access" project=object.slug %}" method="post">
{% csrf_token %}
<div class="panel panel-default">
  <div class="panel-heading"><h4 class="panel-title">
    {% documentation_icon 'admin/access' 'acl' right=True %}
  {% trans "Project access control" %}
  </h4></div>
  <div class="panel-body">
  {% crispy access_form %}
  </div>
  <div class="panel-footer">
{% if user_can_edit_access_control %}
  <input type="submit" class="btn btn-primary" value="{% trans "Change" %}" />
{% else %}
{% if has_billing %}
  <a class="btn btn-primary pull-right flip" href="{% url 'billing' %}">{% trans "Check your billing status" %}</a>
{% endif %}
    <p class="helptext">{% trans "You do not have permission to change project access control." %}</p>
<div class="clearfix"></div>
{% endif %}
  </div>
</div>
</form>
{% endblock %}
