{% extends "layouts/content.html" %}
{% load i18n %}
{% load utils %}

{% block styles %}
  <style>
    .modal-lg
    {
      width: 1100px;
    }
  </style>
{% endblock %}

{% block content %}
<h2>Authentication Tokens</h2>
<p>Authentication tokens allow scripts using <a href="{{ STATIC_URL }}docs/v2/lavacli.html">lavacli</a> and other
scripts based on XML-RPC to securely access LAVA resources. You can create and use any number of tokens
simultaneously. If you believe a token is compromised you can quickly remove it. Anyone using that token
will no longer be able to authenticate as You in the system.</p>

          <button class="btn btn-xs btn-primary" data-toggle="modal" data-target="#newTokenModal" title="Create new token">new&nbsp;<span
                  class="glyphicon glyphicon-plus-sign"></span></button>
          <div class="modal fade" id="newTokenModal" tabindex="-1" role="dialog" aria-labelledby="newTokenModalLabel" aria-hidden="true">
            <div class="modal-dialog">
              <div class="modal-content">
                <div class="modal-header">
                  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                  <h4 class="modal-title" id="newTokenModalLabel">Description of new token</h4>
                </div>
                  <div class="modal-body">
                    <form method="post" action="{% url 'linaro_django_xmlrpc.views.create_token' %}">
                      <div id="description-container" class="form-group">
                      <p>The token description is arbitrary text
                      which can help you to associate tokens with an intended purpose.
                      </p>
                        <input id="description" name="description"/>
                        {% csrf_token %}
                      </div>
                      <dl class="dl-horizontal">
                        <div class="form-group">
                          <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
                          <button type="submit" class="btn btn-success">Create</button>
                        </div>
                      </dl>
                    </form>
                  </div>
                </div>
            </div>
          </div>

{% if token_list %}
  {% if token_list|length > 1 %}
     <h2>You have {{ token_list|length }} tokens.</h2>
  {% else %}
      <h2>You have {{ token_list|length }} token.</h2>
  {% endif %}

  <h3>Most recently created tokens shown first</h3>
    {% if unused %}
    <button class="btn btn-sm btn-danger" data-toggle="modal"
            data-target="#deleteUnusedTokensModal"
            {% if unused == 1 %}
            title="Delete the unused token">Delete the {{ unused }} unused token
            {% else %}
            title="Delete all unused tokens">Delete all {{ unused }} unused tokens
            {% endif %}
        <span class="glyphicon glyphicon-trash"></span></button>
    <div class="modal fade" id="deleteUnusedTokensModal" tabindex="-1" role="dialog"
         aria-labelledby="deleteUnusedTokensModalLabel" aria-hidden="true">
    <div class="modal-dialog">
    <div class="modal-content">
    <div class="modal-header">
      <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
        {% if unused == 1 %}
      <h3 class="modal-title" id="deleteUnusedTokensModalLabel">Delete the {{ unused }} unused token</h3>
        {% else %}
      <h3 class="modal-title" id="deleteUnusedTokensModalLabel">Delete <b>ALL</b> {{ unused }} unused tokens</h3>
        {% endif %}
    </div>
    <div class="modal-body">
    {% if unused == 1 %}
      <p>Are you sure that you want to delete <b>the {{ unused }} unused token</b>?</p>
    {% else %}
      <p>Are you sure that you want to delete <b>all {{ unused }} unused tokens</b>?</p>
    {% endif %}
    </div>
    <div class="modal-footer">
        <form method="post" action="{% url 'linaro_django_xmlrpc.views.delete_unused_tokens' %}">
            <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
            <input class="btn btn-danger" type="submit" value="Delete"/>
            {% csrf_token %}
         </form>
    </div>
    </div>
    </div>
    </div>
    {% endif %}

<div class="row">
  <div class="col-md-12">
    <table class="table table-striped table-hover">
      <thead>
        <th>No.</th>
        <th>Description</th>
        <th>Created on</th>
        <th>Last used</th>
        <th>Actions</th>
      </thead>
      <tbody>
        {% for token in token_list|dictsortreversed:"created_on" %}
        <tr>
          <td>{{ token.pk }}</td>
          <td>{{ token.description|default:"empty" }}</td>
          <td>{{ token.created_on|date }}</td>
          {% if token.last_used_on %}
          <td>{{ token.last_used_on|timesince }}</td>
          {% else %}
          <td><em>It was not used yet</em></td>
          {% endif %}
          <td><button class="btn btn-xs btn-success" data-toggle="modal" data-target="#hashModal{{ token.pk }}" title="View token hash"><span class="glyphicon glyphicon-eye-open"></span></button>
          <div class="modal fade" id="hashModal{{ token.pk }}" tabindex="-1" role="dialog" aria-labelledby="hashModal{{ token.pk }}Label" aria-hidden="true">
            <div class="modal-dialog modal-lg">
              <div class="modal-content">
                <div class="modal-header">
                  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                  <h3 class="modal-title" id="hashModal{{ token.pk }}Label">Hash for {{ token }}</h3>
                </div>
                <div class="modal-body">
                    <p>{{ token.description|default:"empty description" }}</p>
                  <code>{{ token.secret }}</code>
                </div>
              </div>
            </div>
          </div>

          <div class="btn-group">
            <button class="btn btn-xs btn-primary" data-toggle="modal" data-target="#editTokenModal{{ token.pk }}" title="Edit token description"><span class="glyphicon glyphicon-pencil"></span></button>
            <div class="modal fade" id="editTokenModal{{ token.pk }}" tabindex="-1" role="dialog" aria-labelledby="editTokenModal{{ token.pk }}label" aria-hidden="true">
              <div class="modal-dialog">
                <div class="modal-content">
                  <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                    <h4 class="modal-title" id="editTokenModal{{ token.id }}">Edit description of {{ token }}</h4>
                  </div>
                  <div class="modal-body">
                    <form method="post" action="{% url 'linaro_django_xmlrpc.views.edit_token' token.pk %}">
                      <div id="description-container" class="form-group">
                        <p>An empty description will clear the existing content.</p>
                        <input id="description" name="description" value="{{ token.description }}"/>
                        {% csrf_token %}
                      </div>
                      <dl class="dl-horizontal">
                        <div class="form-group">
                          <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
                          <button type="submit" class="btn btn-success">Set</button>
                        </div>
                      </dl>
                    </form>
                  </div>
                </div>
              </div>
            </div>
          </div>

          <button class="btn btn-xs btn-danger" data-toggle="modal" data-target="#deleteTokenModal{{ token.pk }}" title="Delete token"><span class="glyphicon glyphicon-trash"></span></button>
          <div class="modal fade" id="deleteTokenModal{{ token.pk }}" tabindex="-1" role="dialog" aria-labelledby="deleteTokenModal{{ token.pk }}Label" aria-hidden="true">
            <div class="modal-dialog">
              <div class="modal-content">
                <div class="modal-header">
                  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                  <h3 class="modal-title" id="deleteTokenModal{{ token.pk }}Label">Deleting token</h3>
                </div>
                <div class="modal-body">
                  <p>Are you sure that you want to delete {{ token }}
                    {% if token.description %}
                     with description &ldquo;{{ token.description }}&rdquo;?
                    {% else %}
                     with no description?
                    {% endif %}
                  </p>
                </div>
                <div class="modal-footer">
                    <form method="post" action="{% url 'linaro_django_xmlrpc.views.delete_token' token.pk %}">
                        <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
                        <input class="btn btn-danger" type="submit" value="Delete"/>
                        {% csrf_token %}
                     </form>
                </div>
              </div>
            </div>
          </div>
          </td>
          </tr>
        {% endfor %}
      </tbody>
    </table>
  </div>
</div>

{% else %}
<p>You don't have any tokens yet.</p>
{% endif %}

{% endblock %}
