{% extends 'generic/object.html' %}
{% load perms %}

{% block breadcrumbs %}
{% endblock %}

{% block object_identifier %}
    {% if object %}
        {{ block.super }}
    {% endif %}
{% endblock %}

{% block title %}User Key{% endblock %}

{% block subtitle %}
    {% if object %}
        {{ block.super }}
    {% endif %}
{% endblock %}

{% block controls %}
    <div class="controls">
        <div class="control-group">
            {# Extra buttons #}
            {% block extra_controls %}{% endblock %}

            {% if object %}
                {% if request.user|can_change:object %}
                    <a href="{% url 'plugins:netbox_secrets:userkey_edit' %}?return_url={{ request.path }}"
                       class="btn btn-sm btn-warning" role="button">
                        <span class="mdi mdi-pencil" aria-hidden="true"></span>&nbsp;Edit
                    </a>
                {% endif %}
            {% endif %}
        </div>
    </div>
{% endblock %}

{% block tabs %}
    {% if object %}
        {{ block.super }}
    {% endif %}
{% endblock %}

{% block content %}
    <div class="row mb-3">
        <div class="col col-md-6">
            <div class="card">
                <h5 class="card-header">Overview</h5>
                <div class="card-body">
                    <table class="table table-hover">
                        <tr>
                            <th scope="row">Status</th>
                            <td>
                                {% if object.is_active %}
                                    <i class="mdi mdi-check-circle text-success"></i> Active
                                {% else %}
                                    <i class="mdi mdi-close-circle text-danger"></i>
                                    {% if not object.is_filled %}
                                        You don't have a user key on file.
                                    {% else %}
                                        Your user key is inactive. Ask an administrator to enable it for you.
                                    {% endif %}
                                {% endif %}
                            </td>
                        </tr>
                        {% if object.is_filled %}
                            <tr>
                                <th scope="row">Public Key</th>
                                <td>
                                    <pre class="copyable">{{ object.public_key }}</pre>
                                </td>
                            </tr>
                        {% endif %}
                    </table>
                    {% if not object %}
                        <div class="card-footer">
                            <div class="text-end noprint">
                                <a class="btn btn-sm btn-primary"
                                   href="{% url 'plugins:netbox_secrets:userkey_edit' %}">
                                    <i class="mdi mdi-plus-thick" aria-hidden="true"></i> Add User Key
                                </a>
                            </div>
                        </div>
                    {% endif %}
                </div>
            </div>
        </div>
        {% if object.is_filled and object.is_active %}
            <div class="col col-md-6">
                <div class="card">
                    <h5 class="card-header">Session Key</h5>
                    <div class="card-body">
                        <table class="table table-hover">
                            <tr>
                                <th scope="row">Status</th>
                                <td>
                                    {% if object.session_key %}
                                        <i class="mdi mdi-check-circle text-success"></i> Active
                                    {% else %}
                                        <i class="mdi mdi-close-circle text-danger"></i> Inactive
                                    {% endif %}
                                </td>
                            </tr>
                            <tr>
                                <th scope="row">Created</th>
                                <td>{{ object.session_key.created|placeholder }}</td>
                            </tr>
                        </table>
                    </div>
                    <div class="card-footer">
                        <div class="text-end noprint">
                            {% if object.session_key %}
                                <a class="btn btn-danger btn-sm" title="Delete Session Key"
                                   href="{% url 'plugins:netbox_secrets:sessionkey_delete' %}?return_url={{ request.path }}">
                                    <i class="mdi mdi-trash-can-outline" aria-hidden="true"></i> Delete
                                </a>
                            {% else %}
                                <button type="button" class="btn btn-sm btn-primary" id="add_session_key"
                                        data-bs-toggle="modal" data-bs-target="#privkey_modal">
                                    <i class="mdi mdi-plus-thick" aria-hidden="true"></i> Add Session Key
                                </button>
                                {% include 'netbox_secrets/inc/private_key_modal.html' %}
                            {% endif %}
                        </div>
                    </div>
                </div>
            </div>
        {% endif %}
    </div>
{% endblock content %}
