{% extends 'base.html' %}
{% load i18n %}
{% load static %}
{% load bootstrap3 %}

{% 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.login_mode layout="horizontal" %}
                            {% bootstrap_field form.username layout="horizontal" %}
                            {% bootstrap_field form.priority layout="horizontal" %}
                            {% bootstrap_field form.protocol layout="horizontal" %}

                            <h3 id="auth_title_id">{% trans 'Auth' %}</h3>
                            {% block auth %}
                            <div class="auto-generate">
                                <div class="form-group">
                                    <label for="{{ form.auto_generate_key.id_for_label }}" class="col-sm-2 control-label">{% trans 'Auto generate key' %}</label>
                                    <div class="col-sm-8">
                                        {{ form.auto_generate_key}}
                                    </div>
                                </div>
                            </div>
                            <div class="auth-fields">
                                {% bootstrap_field form.password layout="horizontal" %}
                                {% bootstrap_field form.private_key layout="horizontal" %}
                            </div>
                            <div class="form-group">
                                <label for="{{ form.auto_push.id_for_label }}" class="col-sm-2 control-label">{% trans 'Auto push' %}</label>
                                <div class="col-sm-8">
                                    {{ form.auto_push}}
                                </div>
                            </div>
                            {% endblock %}
                            <div id="command-filter-block">
                                <h3>{% trans 'Command filter' %}</h3>
                                {% bootstrap_field form.cmd_filters layout="horizontal" %}
                            </div>
                            <h3>{% trans 'Other' %}</h3>
                            {% bootstrap_field form.sudo layout="horizontal" %}
                            {% bootstrap_field form.shell 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 login_mode_id = '#' + '{{ form.login_mode.id_for_label }}';

var auto_generate_key = '#'+'{{ form.auto_generate_key.id_for_label }}';
var password_id = '#' + '{{ form.password.id_for_label }}';
var private_key_id = '#' + '{{ form.private_key.id_for_label }}';
var auto_push_id = '#' + '{{ form.auto_push.id_for_label }}';
var sudo_id = '#' + '{{ form.sudo.id_for_label }}';
var shell_id = '#' + '{{ form.shell.id_for_label }}';

function autoLoginModeProtocol() {
    // 协议+自动登录模式字段控制
    $('#auth_title_id').removeClass('hidden');
    var protocol = $(protocol_id + " option:selected").text();
    if (protocol === 'rdp') {
        authFieldsDisplay();
        $(auto_generate_key).closest('.form-group').removeClass('hidden');
        $(private_key_id).closest('.form-group').addClass('hidden');
        $(password_id).closest('.form-group').removeClass('hidden');
        $(auto_push_id).closest('.form-group').removeClass('hidden');
        $('#command-filter-block').addClass('hidden');
        $(sudo_id).closest('.form-group').addClass('hidden');
        $(shell_id).closest('.form-group').addClass('hidden');
    }
    else if (protocol === 'vnc') {
        $('.auth-fields').removeClass('hidden');
        $(auto_generate_key).closest('.form-group').addClass('hidden');
        $(private_key_id).closest('.form-group').addClass('hidden');
        $(password_id).closest('.form-group').removeClass('hidden');
        $(auto_push_id).closest('.form-group').addClass('hidden');
        $('#command-filter-block').addClass('hidden');
        $(sudo_id).closest('.form-group').addClass('hidden');
        $(shell_id).closest('.form-group').addClass('hidden');
    }
    else if (protocol === 'telnet') {
        $('.auth-fields').removeClass('hidden');
        $(auto_generate_key).closest('.form-group').addClass('hidden');
        $(private_key_id).closest('.form-group').addClass('hidden');
        $(password_id).closest('.form-group').removeClass('hidden');
        $(auto_push_id).closest('.form-group').addClass('hidden');
        $('#command-filter-block').removeClass('hidden');
        $(sudo_id).closest('.form-group').addClass('hidden');
        $(shell_id).closest('.form-group').addClass('hidden');
    }
    else {
        authFieldsDisplay();
        $(auto_generate_key).closest('.form-group').removeClass('hidden');
        $(private_key_id).closest('.form-group').removeClass('hidden');
        $(password_id).closest('.form-group').removeClass('hidden');
        $(auto_push_id).closest('.form-group').removeClass('hidden');
        $('#command-filter-block').removeClass('hidden');
        $(sudo_id).closest('.form-group').removeClass('hidden');
        $(shell_id).closest('.form-group').removeClass('hidden');
    }
}

function manualLoginModeProtocol() {
    // 协议+手动登录模式字段控制
    $('#auth_title_id').addClass('hidden');
    var protocol = $(protocol_id + " option:selected").text();
    if (protocol === 'rdp') {
        $('.auth-fields').addClass('hidden');
        $(auto_generate_key).closest('.form-group').addClass('hidden');
        $(password_id).closest('.form-group').addClass('hidden');
        $(private_key_id).closest('.form-group').addClass('hidden');
        $(auto_push_id).closest('.form-group').addClass('hidden');
        $('#command-filter-block').addClass('hidden');
        $(sudo_id).closest('.form-group').addClass('hidden');
        $(shell_id).closest('.form-group').addClass('hidden');
    }
    else if (protocol === 'vnc') {
        $('.auth-fields').addClass('hidden');
        $(auto_generate_key).closest('.form-group').addClass('hidden');
        $(password_id).closest('.form-group').addClass('hidden');
        $(private_key_id).closest('.form-group').addClass('hidden');
        $(auto_push_id).closest('.form-group').addClass('hidden');
        $('#command-filter-block').addClass('hidden');
        $(sudo_id).closest('.form-group').addClass('hidden');
        $(shell_id).closest('.form-group').addClass('hidden');
    }
    else if (protocol === 'telnet') {
        $('.auth-fields').addClass('hidden');
        $(auto_generate_key).closest('.form-group').addClass('hidden');
        $(password_id).closest('.form-group').addClass('hidden');
        $(private_key_id).closest('.form-group').addClass('hidden');
        $(auto_push_id).closest('.form-group').addClass('hidden');
        $('#command-filter-block').removeClass('hidden');
        $(sudo_id).closest('.form-group').addClass('hidden');
        $(shell_id).closest('.form-group').addClass('hidden');
    }
    else {
        $('.auth-fields').addClass('hidden');
        $(auto_generate_key).closest('.form-group').addClass('hidden');
        $(password_id).closest('.form-group').addClass('hidden');
        $(private_key_id).closest('.form-group').addClass('hidden');
        $(auto_push_id).closest('.form-group').addClass('hidden');
        $('#command-filter-block').removeClass('hidden');
        $(sudo_id).closest('.form-group').removeClass('hidden');
        $(shell_id).closest('.form-group').removeClass('hidden');
    }

}

function authFieldsDisplay() {
    if ($(auto_generate_key).prop('checked')) {
        $('.auth-fields').addClass('hidden');
    } else {
        $('.auth-fields').removeClass('hidden');
    }
}
function fieldDisplay(){
    var login_mode = $(login_mode_id).val();
    if (login_mode === 'manual'){
        manualLoginModeProtocol();
    }
    else if(login_mode === 'auto'){
        autoLoginModeProtocol();
    }
}

$(document).ready(function () {
    $('.select2').select2();
    authFieldsDisplay();
    fieldDisplay();
})
.on('change', auto_generate_key, function(){
    authFieldsDisplay();
})
.on('change', login_mode_id, function(){
    fieldDisplay();
})
.on('change', protocol_id, function(){
    fieldDisplay();
}).on("submit", "form", function (evt) {
    evt.preventDefault();
    {% block formUrl %}
    var the_url = '{% url 'api-assets:system-user-list' %}';
    var redirect_to = '{% url "assets:system-user-list" %}';
    var method = "POST";
    {% endblock %}
    var form = $("form");
    var data = form.serializeObject();

    objectAttrsIsList(data, ['cmd_filters']);
    objectAttrsIsBool(data, ["auto_generate_key", "auto_push"]);
    data["private_key"] = $("#id_private_key").data('file');

    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)
    })
})

</script>
{% endblock %}
