function initConfigDiv() {
    document.getElementById("roleTree").style.height = document.body.offsetHeight - 4 + "px"; //减去border的宽度
    document.getElementById("roleZtree").style.height = document.getElementById("roleTree").offsetHeight - 2 + "px"; //减去border的宽度
}

layui.use(['form'], function () {
    var form = layui.form;

    form.on('submit(addConfig)', function (data) {
        submitRoleData(data.field);
        return false;
    });
});

function submitRoleData(data) {
    var name = data.name;
    name = name.replace(/(^\s*)|(\s*$)/g, '');
    name = filterXSS(name);
    var des = data.configdes;
    des = filterXSS(des);

    //获取role的guid
    var roleId = (roleData.id == "" ? getRoleSelected() : roleData.roleId);
    var id = (roleData.id == "" ? "" : roleData.id)

    if (roleId == "") {
        layer.msg("请选中一个角色");
        return;
    }

    //获取选中的菜单guid
    var array = getAllSelected();
    var menuIds = [];

    for (var i = 0; i < array.length; i++) {
        if (array[i].id != "0") {
            menuIds.push(array[i].id);
        }
    }
    if (menuIds.length == 0) {
        layer.msg("请选中一个菜单资源项");
        return;
    }

    $.ajax({
        url: "/pqs9900/sysset/addConfig",
        type: "post",
        async: false,
        dataType: "json",
        data: {
            id: id,
            roleId: roleId,
            name: name,
            menuIds: JSON.stringify(menuIds),
            des: des
        },
        success: function (data) {
            if (data.code == 200) {
                layer.msg("操作成功", function () {
                    var index = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(index);
                    parent.reloadMenuConfig();
                });
            } else {
                layer.msg(data.message);
            }
        }
    });
}

var roleData;

$(function () {
    triggerCheckBox(1);
    initConfigDiv();
    getRole();

    roleData = parent.getMenuConfigData();

    if (roleData.id != "") { //设置状态
        $("#name").val(roleData.name);
        $("#configdes").val(roleData.des);

        //角色无法选中
        var zTree = $.fn.zTree.getZTreeObj("roleZtree");
        var nodes = zTree.getNodes();

        for (var i = 0; i < nodes[0].children.length; i++) {
            zTree.setChkDisabled(nodes[0].children[i], true);

            if (roleData.roleId == nodes[0].children[i].id) {
                nodes[0].children[i].checked = true;
                zTree.updateNode(nodes[0].children[i]);
            }
        }

        //菜单选中
        getRoleFunction(roleData.roleId, roleData.id);
    }
});

function getRoleFunction(roleId, id) {
    $.ajax({
        url: "/pqs9900/sysset/getRoleFunction",
        type: "post",
        data: {
            roleId: roleId,
            id: id
        },
        async: false,
        dataType: "json",
        success: function (data) {
            if (data.code == 200) {
                setTreeDataSelect(data.body);
            } else {
                layer.msg("获取绑定资源失败");
            }
        }
    });
}

function getRole() {
    $.ajax({
        url: "/pqs9900/sysset/roleList",
        type: "post",
        async: false,
        dataType: "json",
        success: function (data) {
            intiRoleTree(data.body);
            setRoleTreeState();
        },
        error: function () {
            layer.msg("获取角色树基础配置失败.");
        }
    });
}

function setRoleTreeState() {
    $.ajax({
        url: "/pqs9900/sysset/roleStateList",
        type: "post",
        async: false,
        dataType: "json",
        success: function (data) {
            if (data.code == 200) {
                var zTree = $.fn.zTree.getZTreeObj("roleZtree");
                var nodes = zTree.getNodes();

                for (var i = 0; i < nodes[0].children.length; i++) {
                    for (var j = 0; j < data.body.length; j++) {
                        if (nodes[0].children[i].id === data.body[j]) {
                            zTree.setChkDisabled(nodes[0].children[i], true);
                            break;
                        }
                    }
                }
            }
        }
    });
}

function getRoleSelected() {
    var zTree = $.fn.zTree.getZTreeObj("roleZtree");
    var nodes = zTree.getCheckedNodes(true);

    if (0 === nodes.length) {
        return "";
    }

    return nodes[0].id;
}

var
    roleSetting = {
        check: {
            enable: true,
            chkStyle: "radio",
            radioType: "all"
        },
        data: {
            simpleData: {
                enable: true
            }
        }
    };

function intiRoleTree(treeData) {
    if (treeData == null || treeData.length == 0) {
        layer.msg("数据库数据为空,角色树基础配置载失败.");
        return;
    }

    var datalist = [];
    for (var i = 0; i < treeData.length; i++) {
        var str = '';
        str += '{id:"' + treeData[i].guid + '"';
        if (treeData[i].parentNode == null) {
            str += ',nocheck:true';
        }
        str += ',name:"' + treeData[i].menuDes + '"';
        str += ',pId:"' + treeData[i].parentNode + '"}';
        datalist.push(str);
    }

    datalist = eval('[' + datalist + ']');// string->json数组
    var zNodes = datalist;

    var treeObj = $.fn.zTree.init($("#roleZtree"), roleSetting, zNodes);
    treeObj.expandAll(true);
}