{% extends 'base.html' %}
{% load i18n %}
{% load static %}
{% load bootstrap3 %}
{% block custom_head_css_js %}
    <link href="{% static 'css/plugins/select2/select2.min.css' %}" rel="stylesheet">
    <script src="{% static 'js/plugins/select2/select2.full.min.js' %}"></script>
{% endblock %}

{% block content %}
    <div class="wrapper wrapper-content animated fadeInRight">
        <div class="row">
            <div class="col-sm-12">
                <div class="ibox float-e-margins">
                    <div class="ibox-title">
                        <h5>{{ action }}</h5>
                        <div class="ibox-tools">
                            <a class="collapse-link">
                                <i class="fa fa-chevron-up"></i>
                            </a>
                            <a class="dropdown-toggle" data-toggle="dropdown" href="#">
                                <i class="fa fa-wrench"></i>
                            </a>
                            <a class="close-link">
                                <i class="fa fa-times"></i>
                            </a>
                        </div>
                    </div>
                    <div class="ibox-content">
                        <form enctype="multipart/form-data" method="post" class="form-horizontal" action="" >
                            {% csrf_token %}
                            {% if form.non_field_errors %}
                                <div class="alert alert-danger">
                                    {{ form.non_field_errors }}
                                </div>
                            {% endif %}
                            <h3>{% trans 'Basic' %}</h3>
                            {% bootstrap_field form.name layout="horizontal" %}
                            {% bootstrap_field form.ip layout="horizontal" %}
                            {% bootstrap_field form.port layout="horizontal" %}
                            {% bootstrap_field form.protocol layout="horizontal" %}
                            {% bootstrap_field form.domain layout="horizontal" %}

                            {% block auth %}
                            <h3 id="auth_title">{% trans 'Auth' %}</h3>
                            <div class="auth-fields">
                                {% bootstrap_field form.username layout="horizontal" %}
                                {% bootstrap_field form.password layout="horizontal" %}
                                {% bootstrap_field form.private_key layout="horizontal" %}
                            </div>
                            {% endblock %}

                            <h3>{% trans 'Other' %}</h3>
                            {% bootstrap_field form.is_active layout="horizontal" %}
                            {% bootstrap_field form.comment layout="horizontal" %}
                            <div class="form-group">
                                <div class="col-sm-4 col-sm-offset-2">
                                    <button class="btn btn-white" type="reset">{% trans 'Reset' %}</button>
                                    <button id="submit_button" class="btn btn-primary" type="submit">{% trans 'Submit' %}</button>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
{% endblock %}

{% block custom_foot_js %}
<script>
var protocol_id = '#' + '{{ form.protocol.id_for_label }}';
var private_key_id = '#' + '{{ form.private_key.id_for_label }}';
var port = '#' + '{{ form.port.id_for_label }}';
var username = '#' + '{{ form.username.id_for_label }}';
var password = '#' + '{{ form.password.id_for_label }}';
var auth_title = '#auth_title';

function protocolChange() {
    if ($(protocol_id + " option:selected").text() === 'rdp') {
        {#$(port).val(3389);#}
        $(private_key_id).closest('.form-group').addClass('hidden');
        $(username).closest('.form-group').addClass('hidden');
        $(password).closest('.form-group').addClass('hidden');
        $(auth_title).addClass('hidden');
    } else {
        {#$(port).val(22);#}
        $(private_key_id).closest('.form-group').removeClass('hidden');
        $(username).closest('.form-group').removeClass('hidden');
        $(password).closest('.form-group').removeClass('hidden');
        $(auth_title).removeClass('hidden');
    }
}

$(document).ready(function(){
    protocolChange();
})
.on("submit", "form", function (evt) {
    evt.preventDefault();
    var form = $("form");
    var data = form.serializeObject();
    data["private_key"] = $("#id_private_key").data('file');
    var method = "POST";
    var the_url = '{% url "api-assets:gateway-list" %}';
    var redirect_to = '{% url "assets:domain-gateway-list" pk=DEFAULT_PK %}'.replace("{{ DEFAULT_PK }}", data.domain);
    {% if type == "update" %}
        the_url = '{% url 'api-assets:gateway-detail' pk=object.id %}';
        method = "PUT";
    {% endif %}
    var props = {
        url:the_url,
        data:data,
        method:method,
        form:form,
        redirect_to:redirect_to
     };
    formSubmit(props);
})
.on('change', '#id_private_key', function () {
    readFile($(this)).on("onload", function (evt, data) {
         $(this).data("file", data)
    })
})
.on('change', protocol_id, function(){
    protocolChange();
});
</script>
{% endblock %}