{% extends 'assets/asset_create.html' %}
{% load bootstrap3 %}
{% load i18n %}

{% block extra %}
    <div class="hr-line-dashed"></div>
    <h3>{% trans 'Configuration' %}</h3>
    {% bootstrap_field form.number layout="horizontal" %}
{% endblock %}

{% block form_submit %}
<script>
$(document).ready(function () {
})
.on("submit", "form", function (evt) {
    evt.preventDefault();
    var the_url = '{% url 'api-assets:asset-detail' pk=object.id %}';
    var redirect_to = '{% url "assets:asset-list" %}';
    var form = $("form");
    var protocols = {};
    var data = form.serializeObject();
    $.each(data, function (k, v) {
        if (k.startsWith("form")){
            delete data[k];
            var _k = k.split("-");
            var formName = _k.slice(0, 2).join("-");
            var key = _k[_k.length-1];
            if (!protocols[formName]) {
                protocols[formName] = {}
            }
            protocols[formName][key] = v
        }
    });
    protocols = $.map(protocols, function ( v) {
        return v
    });
    data["protocols"] = protocols;
    if (typeof data.labels === "string") {
            data["labels"] = [data["labels"]];
    }
    if (typeof data["nodes"] == "string") {
        data["nodes"] = [data["nodes"]]
    }
    var props = {
        url: the_url,
        data: data,
        method: "PUT",
        form: form,
        redirect_to: redirect_to
    };
    formSubmit(props);
});
</script>
{% endblock %}