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

{% 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) {
                var $navbar = $(modal).find(".nav-tabs");
                if ($navbar.find("li").size() == 1) {
                    // hide tab bar for plugin/version modal wizard
                    $navbar.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();
                }

                $(".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 %}
