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

{% block form %}
<form id="platformForm" method="post" class="form-horizontal">
    {% csrf_token %}
    {% bootstrap_field form.name layout="horizontal" %}
    {% bootstrap_field form.base layout="horizontal" %}
    <div class="meta-config">
        <div hidden class="windows-config">
        {% bootstrap_field meta_form.security layout="horizontal" %}
        {% bootstrap_field meta_form.console layout="horizontal" %}
        </div>
    </div>
    {% bootstrap_field form.comment 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">
function toggleWindowConfig() {
    var baseRef = $("#id_base");
    var windowConfigRef = $(".windows-config");
    var windowConfigInputRef = windowConfigRef.find(".form-control");
    if (baseRef.val().toLowerCase() === 'windows') {
        windowConfigInputRef.attr('disabled', false);
        windowConfigRef.show();
    } else {
        windowConfigInputRef.attr('disabled', true);
        windowConfigRef.hide();
    }
}

$(document).ready(function () {
    toggleWindowConfig()
})
.on("change", "#id_base", function () {
    toggleWindowConfig()
})
.on("submit", "form", function (evt) {
    evt.preventDefault();
    var form = $("form");
    var data = form.serializeObject();
    var method = "POST";
    var theUrl = '{% url "api-assets:platform-list" %}';
    var redirectTo = '{% url "assets:platform-list" %}';
    {% if type == "update" %}
        theUrl = '{% url 'api-assets:platform-detail' pk=object.id %}';
        method = "PUT";
    {% endif %}

    var metaData = $(".meta-config .form-control").serializeObject();
    objectAttrsIsBool(metaData, ['console']);
    var metaKeys = Object.keys(metaData);
    metaKeys.forEach(function (k, v) {
        delete data[k]
    });
    data.meta = metaData;
    console.log(data);
    var props = {
        url: theUrl,
        data: data,
        method: method,
        form: form,
        redirect_to: redirectTo
     };
    formSubmit(props);
 })
</script>
{% endblock %}

