
layui.use(['table', 'jquery'], function () {
    var table = layui.table;
    var form = layui.form,
       layer = parent.layer === undefined ? layui.layer : parent.layer,
       $ = layui.jquery;
  


    var zTreeObj;
    // zTree 的参数配置，深入使用请参考 API 文档（setting 配置详解）
    var setting = {
        check:
            {
                enable: true,
                chkStyle: "checkbox",
                chkboxType: { "Y": "ps", "N": "ps" }
            },
        callback: {

        }

    };
    var treeNodes;
    $(document).ready(function () {

        var roleid = $("#roleId").val();
        $.ajax({
            url: "/QualityManagement/GetMenuTreeData",
            type: "get",
            contentType: "application/json",
            dataType: "text",
            cache: false,
            async: false,
            data: { "roleId": roleid },
            success: function (result, status) {
                treeNodes = eval('(' + result + ')');
            }
        });
        zTreeObj = $.fn.zTree.init($("#ztree"), setting, treeNodes);


        $('.Save').on('click', function () {
            var nodes = zTreeObj.getCheckedNodes(true);

            var roleId = $(".thisRoleId").val();
            if (nodes.length <= 1) {
                layui.layer.alert("菜单不能为空!", {
                    icon: 2
                });
                return;
            }

            var menuStr = "";

            for (var i = 1; i < nodes.length; i++) {
                if (i == nodes.length - 1) {
                    menuStr += nodes[i].code;
                }
                else {
                    menuStr += nodes[i].code + ",";
                }
            }




            $.ajax({
                url: "/QualityManagement/AcceptList",
                type: "get",
                contentType: "application/json",
                dataType: "text",
                cache: false,
                async: false,
                data: { "roleId": roleId, "MenuIdStr": menuStr },
                success: function (result, status) {

                    if (result == "True") {
                        layer.open({
                            content: '保存成功'
                             , btn: ['确定']
                             , yes: function (index, layero) {
                                 layer.close(index);
                                 //刷新父窗体
                                 var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                                 parent.layer.close(index);
                             }
                                , cancel: function () {
                                    var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                                    parent.layer.close(index);
                                }
                        });



                    } else {
                        layui.layer.alert("保存失败!", {
                            icon: 2
                        });


                    }
                }
            });




        });
    });

});