{% extends 'generic/object_edit.html' %}
{% load form_helpers %}
{% load plugins %}
{% load static %}

{% block content-wrapper %}
    <div class="tab-content">
        <div class="tab-pane show active" id="edit-form" role="tabpanel" aria-labelledby="object-list-tab">
            {# Link to model documentation #}
            {% if settings.DOCS_ROOT and object.docs_url %}
                <div class="float-end">
                    <a href="{{ object.docs_url }}" target="_blank" class="btn btn-sm btn-outline-secondary"
                       title="View model documentation">
                        <i class="mdi mdi-help-circle"></i> Help
                    </a>
                </div>
            {% endif %}

            <form action="." method="post" class="form form-object-edit mt-5">
                {% csrf_token %}

                {% block form %}
                    {% if object.is_active %}
                        <div class="alert alert-danger" role="alert">
                            <strong>Warning:</strong> Changing your public key will require your user key to be
                            re-activated
                            by another user. You will be unable to retrieve any secrets until your key has been
                            reactivated.
                        </div>
                    {% endif %}
                    <div class="field-group mb-5">
                        {% render_field form.public_key %}
                    </div>
                {% endblock %}

                <div class="text-end my-3">
                    {% block buttons %}
                        <button
                                type="button"
                                class="btn btn-warning"
                                id="generate_keypair"
                                data-bs-toggle="modal"
                                data-bs-target="#new_keypair_modal">
                            <i class="mdi mdi-key"></i> Generate a New Key Pair
                        </button>
                        {% if object.pk %}
                            <button type="submit" name="_update" class="btn btn-primary">
                                Save
                            </button>
                        {% else %}
                            <button type="submit" name="_create" class="btn btn-primary">
                                Create
                            </button>
                        {% endif %}
                        <a class="btn btn-outline-danger" href="{{ return_url }}">Cancel</a>
                    {% endblock %}
                </div>
            </form>
            <div class="modal fade" id="new_keypair_modal" tabindex="-1" role="dialog">
                <div class="modal-dialog modal-lg" role="document">
                    <div class="modal-content">
                        <div class="modal-header">
                            <h4 class="modal-title" id="new_keypair_modal_title">
                                New RSA Key Pair
                            </h4>
                            <button type="button" class="btn btn-close" data-bs-dismiss="modal"
                                    aria-label="Close"></button>
                        </div>
                        <div class="modal-body">
                            <div class="field-group">
                                <h5>New Public Key</h5>
                                <textarea class="form-control" rows="10" id="new_pubkey"
                                          style="height: 250px;font-family:var(--bs-font-monospace);"></textarea>
                            </div>

                            <div class="field-group">
                                <h5>New Private Key</h5>
                                <textarea class="form-control" rows="10" id="new_privkey"
                                          style="height: 250px;font-family:var(--bs-font-monospace);"></textarea>
                            </div>
                        </div>
                        <div class="modal-footer text-center">
                            <button type="button" class="btn btn-danger" id="use_new_pubkey" data-bs-dismiss="modal">
                                I Saved My New Private Key
                            </button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
{% endblock %}

{% block javascript %}
    {# Javascript #}
    <script
            type="text/javascript"
            src="{% static 'netbox_secrets/secrets.js' %}"
            onerror="window.location='{% url 'media_failure' %}?filename=netbox_secrets/secrets.js'">
    </script>
{% endblock %}
