{% extends 'base.html' %}
{% load static %}
{% load form_helpers %}
{% load secret_helpers %}

{% block content %}
<form action="." method="post" class="form form-horizontal">
    {% csrf_token %}
    {{ form.private_key }}
    <div class="row">
        <div class="col-md-6 col-md-offset-3">
            <h3>{% block title %}{% if obj.pk %}Editing {{ obj }}{% else %}Add a Secret{% endif %}{% endblock %}</h3>
            {% if form.non_field_errors %}
                <div class="panel panel-danger">
                    <div class="panel-heading"><strong>Errors</strong></div>
                    <div class="panel-body">
                        {{ form.non_field_errors }}
                    </div>
                </div>
            {% endif %}
            <div class="panel panel-default">
                <div class="panel-heading"><strong>Secret Attributes</strong></div>
                <div class="panel-body">
                    {% render_field form.device %}
                    {% render_field form.role %}
                    {% render_field form.name %}
                    {% render_field form.userkeys %}
                </div>
            </div>
            <div class="panel panel-default">
                <div class="panel-heading"><strong>Secret Data</strong></div>
                <div class="panel-body">
                    {% if obj.pk and obj|decryptable_by:request.user %}
                        <div class="form-group">
                            <label class="col-md-3 control-label required">Current Plaintext</label>
                            <div class="col-md-7">
                                <p class="form-control-static" id="secret_{{ obj.pk }}">********</p>
                            </div>
                            <div class="col-md-2 text-right">
                                <button class="btn btn-xs btn-success unlock-secret" secret-id="{{ obj.pk }}">
                                    <i class="fa fa-lock"></i> Unlock
                                </button>
                                <button class="btn btn-xs btn-danger lock-secret collapse" secret-id="{{ obj.pk }}">
                                    <i class="fa fa-unlock-alt"></i> Lock
                                </button>
                            </div>
                        </div>
                    {% endif %}
                    {% render_field form.plaintext %}
                    {% render_field form.plaintext2 %}
                </div>
            </div>
            {% if form.custom_fields %}
                <div class="panel panel-default">
                    <div class="panel-heading"><strong>Custom Fields</strong></div>
                    <div class="panel-body">
                        {% render_custom_fields form %}
                    </div>
                </div>
            {% endif %}
            <div class="panel panel-default">
                <div class="panel-heading"><strong>Tags</strong></div>
                <div class="panel-body">
                    {% render_field form.tags %}
                </div>
            </div>
        </div>
    </div>
    <div class="row">
        <div class="form-group">
            <div class="col-md-12 text-center">
                {% if obj.pk %}
                    <button type="submit" name="_update" class="btn btn-primary">Update</button>
                    <a href="{% url 'secrets:secret' pk=obj.pk %}" class="btn btn-default">Cancel</a>
                {% else %}
                    <button type="submit" name="_create" class="btn btn-primary">Create</button>
                    <button type="submit" name="_addanother" class="btn btn-primary">Create and Add Another</button>
                    <a href="{{ return_url }}" class="btn btn-default">Cancel</a>
                {% endif %}
		    </div>
        </div>
    </div>
</form>

{% include 'secrets/inc/private_key_modal.html' %}
{% endblock %}

{% block javascript %}
<script src="{% static 'js/secrets.js' %}?v{{ settings.VERSION }}"></script>
{% endblock %}
