{% extends 'base.html' %}
{% load i18n %}
{% block title %}{% trans "Data Processing" %}{% endblock %}

{% block page_header %}
    {% include "horizon/common/_page_header.html" with title=_("Node Group Templates") %}
{% endblock page_header %}

{% block main %}

    <div class="nodegroup_templates">
        {{ nodegroup_templates_table.render }}
    </div>

    <script type="text/javascript">
        addHorizonLoadEvent(function () {
            function get_service_tab(service) {
                return $("a").filter(function (idx, e) {
                    return $(e).attr("data-target") && $(e).attr("data-target").indexOf(service.toLowerCase()) != -1
                }).closest("li");
            }

            // replace form submit with ajax POST and trigger next workflow
            horizon.modals.addModalInitFunction(function (modal) {
                if ($(modal).find(".nav-tabs").find("li").size() == 1) {
                    // hide tab bar for plugin/version modal wizard
                   $('div#modal_wrapper ul.nav-tabs').hide();
                }

                if ($(modal).find(".hidden_create_field").length > 0) {
                    var form = $(".hidden_create_field").closest("form");
                    var successful = false;
                    form.submit(function (e) {
                        var oldHref = $(".configure-nodegrouptemplate-btn")[0].href;
                        var plugin = $("#id_plugin_name option:selected").val();
                        var version = $("#id_" + plugin + "_version option:selected").val();
                        form.find(".close").click();
                        $(".configure-nodegrouptemplate-btn")[0].href = oldHref +
                                "?plugin_name=" + encodeURIComponent(plugin) +
                                "&hadoop_version=" + encodeURIComponent(version);
                        $(".configure-nodegrouptemplate-btn").click();
                        $(".configure-nodegrouptemplate-btn")[0].href = oldHref;
                        return false;
                    });
                    $(".plugin_version_choice").closest(".form-group").hide();
                }

                //display version for selected plugin
                $(document).on('change', '.plugin_name_choice', switch_versions);
                function switch_versions() {
                    $(".plugin_version_choice").closest(".form-group").hide();
                    var plugin = $(this);
                    $("." + plugin.val() + "_version_choice").closest(".form-group").show();
                }

                $(".storage_field").change(switch_storage).change();

                function switch_storage() {
                    var show = $(".storage_field").val() == "cinder_volume";
                    if (show) {
                        $(".volume_per_node_field").closest(".form-group").show();
                        $(".volume_size_field").closest(".form-group").show();
                    } else {
                        $(".volume_per_node_field").closest(".form-group").hide();
                        $(".volume_size_field").closest(".form-group").hide();
                    }
                }

                $(".plugin_name_choice").change();

                //handle node processes change
                $("input").filter(function (idx, e) {
                    return $(e).attr("name") && $(e).attr("name").indexOf("processes") != -1
                })
                        .change(function () {
                            var process_service = $(this).val();
                            var service = $(this).val().split(":")[0];
                            var enabled = false;
                            $(this).closest("ul").find("input").each(function (idx, el) {
                                if ($(el).val().split(":")[0] != service) {
                                    return;
                                }
                                enabled |= $(el).is(':checked');
                            });
                            if (enabled) {
                                get_service_tab(service).show();
                            } else {
                                get_service_tab(service).hide();
                            }
                        }).change();
                //general tab should be active
                get_service_tab("generalconfigaction").find("a").click();
            });
        });
    </script>

{% endblock %}
