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

{% block form %}
    <form id="RemoteAppForm" method="post" class="form-horizontal">
        {% bootstrap_form form layout="horizontal" %}
        <div class="hr-line-dashed"></div>
        <div class="form-group">
            <div class="col-sm-4 col-sm-offset-2">
                <button class="btn btn-default" type="reset"> {% trans 'Reset' %}</button>

                <button id="submit_button" class="btn btn-primary" type="submit">{% trans 'Submit' %}</button>
            </div>
        </div>
    </form>
{% endblock %}

{% block custom_foot_js %}
<script type="text/javascript">
var app_type_id = '#' + '{{ form.type.id_for_label }}';

function getFormDataType(){
    return $(app_type_id+ " option:selected").val();
}
function constructFormDataParams(data){
    var params = {};
    var type =data.type;
    for (var k in data){
        if (k.startsWith(type)){
            params[k] = data[k];
            delete data[k]
        }
    }
    return params
}
function getFormData(form){
    var data = form.serializeObject();
    data['type'] = getFormDataType();
    data['params'] = constructFormDataParams(data);
    return data
}

$(document).ready(function () {
    $('.select2').select2({
        closeOnSelect: true
    });
}).on("submit", "form", function (evt) {
    evt.preventDefault();
    var the_url = '{% url "api-applications:remote-app-list" %}';
    var redirect_to = '{% url "applications:remote-app-list" %}';
    var method = "POST";
    {% if api_action == "update" %}
        the_url = '{% url "api-applications:remote-app-detail" object.id %}';
        method = "PUT";
    {% endif %}
    var form = $("form");
    var data = getFormData(form);
    var props = {
        url: the_url,
        data: data,
        method: method,
        form: form,
        redirect_to: redirect_to
     };
    formSubmit(props);
 })
;
</script>
{% endblock %}