{% 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">
                        {% if form.non_field_errors %}
                            <div class="alert alert-danger">
                                {{ form.non_field_errors }}
                            </div>
                        {% endif %}
                        <form enctype="multipart/form-data" method="post" class="form-horizontal" action="" >
                            {% csrf_token %}
                            <h3>{% trans 'Basic' %}</h3>
                            {% bootstrap_field form.name layout="horizontal" %}
                            {% bootstrap_field form.login_mode layout="horizontal" %}
                            <h3>{% trans 'Database' %}</h3>
                            {% bootstrap_field form.type layout="horizontal" %}
                            {% bootstrap_field form.host layout="horizontal" %}
                            {% bootstrap_field form.port layout="horizontal" %}
                            {% bootstrap_field form.user layout="horizontal" %}
                            {% bootstrap_field form.password layout="horizontal" %}
                            {% bootstrap_field form.database layout="horizontal" %}
                            <h3>{% trans 'Other' %}</h3>
                            {% 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 login_mode_id = '#' + '{{ form.login_mode.id_for_label }}';
var password_id = '#' + '{{ form.password.id_for_label }}';

function fieldDisplay(){
    var login_mode = $(login_mode_id).val();
    if (login_mode === 'manual'){
        $(password_id).closest('.form-group').addClass('hidden');
    }
    else if(login_mode === 'auto'){
        $(password_id).closest('.form-group').removeClass('hidden');
    }
}

$(document).ready(function () {
    $('.select2').select2();
})
.on('change', login_mode_id, function(){
    fieldDisplay();
})
.on("submit", "form", function (evt) {
    evt.preventDefault();
    var the_url = '{% url 'api-applications:database-list' %}';
    var redirect_to = '{% url "applications:database-list" %}';
    var method = "POST";
    {% if type == "update" %}
        the_url = '{% url 'api-applications:database-detail' pk=object.id %}';
        redirect_to = '{% url "applications:database-list" %}';
        method = "PUT";
    {% endif %}
    var form = $("form");
    var data = form.serializeObject();
    var props = {
        url: the_url,
        data: data,
        method: method,
        form: form,
        redirect_to: redirect_to
    };
    formSubmit(props);
})
</script>
{% endblock %}
