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

{% block main %}
<div class="row">
  <div class="col-sm-12">
    {{ tab_group.render }}
  </div>
</div>

<script type="text/javascript">
  addHorizonLoadEvent(function () {
      horizon.modals.addModalInitFunction(function (modal) {
        if ($("#id_tags_list").val()) {
            var tags = JSON.parse($("#id_tags_list").val());
            $.each(tags, function (i, tag) {
                var tagspan = '<span class="label label-info" id="image-tag">' +
                        tag +
                        '<i class="fa fa-close" onclick="remove_tag(this);"></i></span>';
                $("#image_tags_list").append(tagspan);
            });
        }
     });
  });
</script>

<script type="text/javascript">
    addHorizonLoadEvent(function () {
        horizon.modals.addModalInitFunction(function (modal) {
            // ensure that we only do this for the cluster template form
            if (($(modal).find("#create_cluster_template__selectpluginaction")).length > 0) {

                var add_ng_btn_label = '{% trans "Add Node Group" %}';
                $(".hidden_nodegroups_field").after("<input type='button' id='add_nodegroup' value='" + add_ng_btn_label + "'/><br/>");
                $("#add_nodegroup").click(function() {
                    $(".hidden_nodegroups_field").val("create_nodegroup");
                    $(".hidden_configure_field").val("create_nodegroup");
                    var form = $(".hidden_nodegroups_field").closest("form");
                    form.submit();
                });


                $(".hidden_nodegroups_field").val("");
                $(".hidden_configure_field").val("");

                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-clustertemplate-btn")[0].href;
                            var plugin = $("#id_plugin_name option:selected").val();
                            var version = $("#id_" + plugin + "_version option:selected").val();
                            form.find(".close").click();
                            $(".configure-clustertemplate-btn")[0].href = oldHref +
                                    "?plugin_name=" + encodeURIComponent(plugin) +
                                    "&hadoop_version=" + encodeURIComponent(version);
                            $(".configure-clustertemplate-btn").click();
                            $(".configure-clustertemplate-btn")[0].href = oldHref;
                            return false;
                    });
                }
            }
        });

    });
</script>

 <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) {
                // ensure that we only do this for the nodegroup template form
                if (($(modal).find("#create_nodegroup_template__selectpluginaction")).length > 0) {

                    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;
                        });
                    }
                }

                //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>

<script type="text/javascript">
    addHorizonLoadEvent(function () {

        horizon.modals.addModalInitFunction(function (modal) {
            // ensure that we only do this for the create cluster form
            if (($(modal).find("#create_cluster__selectpluginaction")).length > 0) {

                $(".hidden_nodegroups_field").val("");
                $(".hidden_configure_field").val("");

                lower_limit = 0;
                $(".count-field").change();

                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-cluster-btn")[0].href;
                        var plugin = $("#id_plugin_name option:selected").val();
                        var version = $("#id_" + plugin + "_version option:selected").val();
                        form.find(".close").click();
                        $(".configure-cluster-btn")[0].href = oldHref +
                                "?plugin_name=" + encodeURIComponent(plugin) +
                                "&hadoop_version=" + encodeURIComponent(version);
                        $(".configure-cluster-btn").click();
                        $(".configure-cluster-btn")[0].href = oldHref;
                        return false;
                    });
                }
            }
        });

    });
</script>

{% endblock %}
