<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head th:replace="common/head :: head(tree)"/>
<body>
<div class="ok-body" id="app" v-cloak>
    <div id="treePanel" style="overflow: auto;">
        <ul id="orgTree" class="ztree"></ul>
    </div>
</div>
<div th:replace="common/foot :: foot(jquery、tree)"></div>
<script th:inline="none">
var ztree;
var setting = {
    check:{
        enable:true,
        nocheckInherit:true
    },
    data : {
        simpleData : {
            enable : true,
            idKey : "orgId",
            pIdKey : "parentId",
            rootPId : "0"
        },
        key : {
            url : "nourl"
        }
    },
    callback : {
        onClick : function(event, treeId, treeNode) {
            vm.parentId = treeNode.orgId;
            vm.layer = treeNode.type;
            vm.parentName = treeNode.name;
            vm.load();
        }
    }
};
var vm = new Vue({
    el: '#app',
    data: {
        okUtils:null,
        okLayer:null,
        okJquery:null,
        roleId:null,
        orgIds:null
    },
    methods: {
        load : function() {
            layui.use(["okUtils", "okLayer"], function () {
                 vm.okUtils = layui.okUtils;
                 vm.okLayer = layui.okLayer;
                 vm.okJquery = layui.jquery;
                 vm.okUtils.ajaxCloud({
                    url:"/sys/org/select",
                    param : {parentId: null},
                    close:false,
                    success : function(result) {
                        ztree = $.fn.zTree.init($("#orgTree"), setting, result.msg);
                        ztree.expandAll(true);
                        vm.getOrg();
                    }
                 });
            });
        },
        getOrg : function() {
            vm.okUtils.ajaxCloud({
                url:"/sys/role/getOrg",
                param : {roleId: vm.roleId},
                close:false,
                success : function(result) {
                    vm.orgIds = result.msg;
                    for(var i=0; i<vm.orgIds.length; i++) {
                        var node = ztree.getNodeByParam("orgId", vm.orgIds[i]);
                        ztree.checkNode(node, true, false);
                    }
                }
            });
        },
        acceptClick : function(dialog){
            var role = {};
            role.roleId = vm.roleId;
            var nodes = ztree.getCheckedNodes(true);
            var orgIdList = new Array();
            for(var i=0; i<nodes.length; i++) {
                orgIdList.push(nodes[i].orgId);
            }
            role.orgIdList = orgIdList;
            vm.okUtils.ajaxCloud({
                url:"/sys/role/saveOrg",
                param : role,
                json:true,
                success : function(result) {
                     vm.okLayer.msg.greenTick(result.msg)
                }
            });
        }
    },
    mounted: function() {

    }
});
</script>
</body>
</html>
