{% extends "base.html" %}
{% load i18n %}
{% load humanize %}
{% block title %}
    {% trans "Revisions for" %} {{ secret.name }}
{% endblock %}
{% block content %}
    <div class="container">
        <h2 class="mb-3">
            {% trans "Revisions for" %} <a href="{{ secret.get_absolute_url }}">{{ secret.name }}</a>
        </h2>
        <div class="card">
            <div class="card-body table-responsive">
                <table class="table table-hover">
                    <thead>
                        <tr>
                            <th>{% trans "Date" %}</th>
                            <th>{% trans "Changed by" %}</th>
                            <th>{% trans "Name" %}</th>
                            <th>{% trans "Details" %}</th>
                            <th>{% trans "View" %}</th>
                        </tr>
                    </thead>
                    <tbody>
                      {% for r in rows %}
                        <tr>
                          <td>
                            <span data-bs-toggle="tooltip" 
                                  data-bs-placement="right"
                                  title="{{ r.ts|date:'Y-m-d H:i:s' }}">
                              {{ r.ts|naturaltime }}
                            </span>
                          </td>
                          <td>
                            {% if r.kind == "payload" %}
                              <i class="fa fa-key text-primary ms-1" data-bs-toggle="tooltip"
                                 title="{% trans 'Secret value changed' %}"></i>
                            {% elif r.kind == "restore" %}
                              <i class="fa fa-undo text-danger me-1"
                                 data-bs-toggle="tooltip"
                                 title="{% trans 'Secret was restored to this version' %}"></i>
                            {% else %}
                              <i class="fa fa-pencil text-muted ms-1" data-bs-toggle="tooltip"
                                 title="{% trans 'Metadata changed' %}"></i>

                            {% endif %}
                            {{ r.user }}
                          </td>

                          <td>
                            {{ r.name }}
                            {% if r.needs_changing %}
                              <span class="badge text-bg-danger ms-1"
                                    data-bs-toggle="tooltip"
                                    title="{% trans 'This revision was marked as "needs changing"' %}">
                                needs-changing
                              </span>
                            {% endif %}
                          </td>

                          <td>
                            {% if r.changes %}
                              {% for c in r.changes %}
                                <strong>{{ c.label }}:</strong>
                                <code>{{ c.old }}</code> → <code>{{ c.new }}</code>
                                {% if not forloop.last %}<br>{% endif %}
                              {% endfor %}
                              {% if r.restored_from %}<br>
                                <strong>Restored from:</strong>
                                <code>
                                  <a href="{% url 'secrets.revision-detail' revision_hashid=r.restored_from %}">{{ r.restored_from }}</a>
                                </code>
                              {% endif %}
                            {% else %}
                              —
                            {% endif %}
                          </td>

                          <td>
                              <a href="{{ r.link }}"
                                 class="btn btn-sm btn-light"><i class="fa fa-eye"></i></a>
                              {% if request.user.is_superuser %}
                                <form method="post"
                                      action="{% url 'secrets.secret-change-delete' hashid=secret.hashid change_hashid=r.change_hash %}"
                                      class="d-inline">
                                    {% csrf_token %}
                                    <button type="submit"
                                            class="btn btn-sm btn-outline-danger ms-1"
                                            data-bs-toggle="tooltip"
                                            title="{% trans 'Scrub metadata for this change' %}"
                                            onclick="return confirm('{% trans "Scrub metadata snapshot for this change? This cannot be undone." %}')">
                                      <i class="fa fa-eraser"></i>
                                    </button>
                                  </form>
                              {% endif %}
                              {% if r.current %}
                                  {% if r.kind == "payload" %}
                                    <span data-bs-toggle="tooltip"
                                          title="{% trans 'Current secret revision' %}">
                                      <i class="fa fa-star text-warning"></i>
                                    </span>
                                  {% elif r.kind == "meta" %}
                                    <span data-bs-toggle="tooltip"
                                          title="{% trans 'Current metadata' %}">
                                      <i class="fa fa-star-half-stroke text-warning"></i>
                                    </span>
                                  {% endif %}
                              {% endif %}
                          </td>
                        </tr>
                      {% endfor %}
                    </tbody>
              </table>
            </div>
        </div>
    </div>
{% endblock %}
