$(document).ready(function () {

    $("#main_content_title").html("权限管理")



    
     $('#dashboard').dashboard({ draggable: true });
     var zTreeObj;
     var zNodes = [];
     dept_tree = $.fn.zTree.init($("#permision_tree"), {}, zNodes);

     getPermission()

});



function getPermission() {
     function zTreeOnRename(event, treeId, treeNode, isCancel) {
         alert(treeNode.tId + ", " + treeNode.name);
     }

     //  function zTreeBeforeDrop(treeId, treeNodes, targetNode, moveType) {
     //      return !(targetNode == null || (moveType != "inner" && !targetNode.parentTId));
     //  };

     function zTreeOnRemove(event, treeId, treeNode) {
         alert(treeNode.tId + ", " + treeNode.name);
     }

     function zTreeOnClick(event, treeId, treeNode) {
         updateUser(treeNode.id)
     };


     var setting = {
         data: {
             simpleData: {
                 enable: true,
                 idKey: "id",
                 pIdKey: "pId",
                 rootPId: null
             }
         },
         view: { //表示tree的显示状态
             selectMulti: false //表示禁止多选
         },
         check: { //表示tree的节点在点击时的相关设置
             enable: false, //是否显示radio/checkbox
             chkStyle: "checkbox", //值为checkbox或者radio表示
             checkboxType: { p: "", s: "" }, //表示父子节点的联动效果
             radioType: "level" //设置tree的分组
         },
         callback: {
             onRename: zTreeOnRename,
             //  beforeDrop: zTreeBeforeDrop,
             onRemove: zTreeOnRemove,
             onClick: zTreeOnClick
         },
         edit: {
             enable: true,
             drag: {
                 autoExpandTrigger: true,
                 isMove: true,
                 prev: false,
                 next: true,
                 inner: true,
                 autoOpenTime: 0
             },
             showRenameBtn: false,
             showRemoveBtn: false
         }
     };

     PermisionGetAll(function(data) {
         console.log(data)
         zdata = [];
         for (var i = 0; i < data.length; i++) {
             var m = data[i];
             var zdataItem = {};
             zdataItem["id"] = m.id;
             zdataItem["value"] = m.id;
             zdataItem["pId"] = null;
             zdataItem["name"] = m.name;
             zdataItem["open"] = true;
             zdataItem["children"] = addobj(m);
             zdata.push(zdataItem);
         }
         dept_tree = $.fn.zTree.init($("#permision_tree"), setting, zdata)
     }, function(error) {

     });
 }



 function addobj(data) {
     var zdata = [];
     if (typeof(data.childs) != "undefined" && data.childs != null &&
         data.childs.length > 0) {
         for (var i = 0; i < data.childs.length; i++) {
             var m = data.childs[i];
             var zdataItem = {};
             zdataItem["id"] = m.id;
             zdataItem["value"] = m.id;
             zdataItem["pId"] = data.id;
             zdataItem["icon"] = "";
             zdataItem["open"] = true;
             zdataItem["name"] = m.name;
             zdataItem["children"] = addobj(m);
             zdata.push(zdataItem);
         }
         return zdata;
     } else {
         return [];
     }
 }
