{% extends 'users/base.html' %}
{% load helpers %}

{% block title %}API Tokens{% endblock %}

{% block usercontent %}
    <div class="row">
        <div class="col-md-12">
            {% for token in tokens %}
                <div class="panel panel-{% if token.is_expired %}danger{% else %}default{% endif %}">
                    <div class="panel-heading">
                        <div class="pull-right noprint">
                            <a class="btn btn-xs btn-success copy-token" data-clipboard-target="#token_{{ token.pk }}">Copy</a>
                            {% if perms.users.change_token %}
                                <a href="{% url 'user:token_edit' pk=token.pk %}" class="btn btn-xs btn-warning">Edit</a>
                            {% endif %}
                            {% if perms.users.delete_token %}
                                <a href="{% url 'user:token_delete' pk=token.pk %}" class="btn btn-xs btn-danger">Delete</a>
                            {% endif %}
                        </div>
                        <i class="fa fa-key"></i>
                        <samp><span id="token_{{ token.pk }}">{{ token.key }}</span></samp>
                        {% if token.is_expired %}
                            <span class="label label-danger">Expired</span>
                        {% endif %}
                    </div>
                    <div class="panel-body">
                        <div class="row">
                            <div class="col-md-4">
                                <small class="text-muted">Created</small><br />
                                <span title="{{ token.created }}">{{ token.created|date }}</span>
                            </div>
                            <div class="col-md-4">
                                <small class="text-muted">Expires</small><br />
                                {% if token.expires %}
                                    <span title="{{ token.expires }}">{{ token.expires|date }}</span>
                                {% else %}
                                    <span>Never</span>
                                {% endif %}
                            </div>
                            <div class="col-md-4">
                                <small class="text-muted">Create/edit/delete operations</small><br />
                                {% if token.write_enabled %}
                                    <span class="label label-success">Enabled</span>
                                {% else %}
                                    <span class="label label-danger">Disabled</span>
                                {% endif %}
                            </div>
                        </div>
                        {% if token.description %}
                            <br /><span>{{ token.description }}</span>
                        {% endif %}
                    </div>
                </div>
            {% empty %}
                <p>You do not have any API tokens.</p>
            {% endfor %}
            {% if perms.users.add_token %}
                <a href="{% url 'user:token_add' %}" class="btn btn-primary">
                    <span class="fa fa-plus" aria-hidden="true"></span>
                    Add a token
                </a>
            {% else %}
                <div class="alert alert-info text-center" role="alert">
                    You do not have permission to create new API tokens. If needed, ask an administrator to enable token creation for your account or an assigned group.
                </div>
            {% endif %}
        </div>
    </div>
{% endblock %}

{% block javascript %}
<script type="text/javascript">
new ClipboardJS('.copy-token');
</script>
{% endblock %}
