var IDMark_Switch = "_switch",
        IDMark_Icon = "_ico",
        IDMark_Span = "_span",
        IDMark_Input = "_input",
        IDMark_Check = "_check",
        IDMark_Edit = "_edit",
        IDMark_Remove = "_remove",
        IDMark_Ul = "_ul",
        IDMark_A = "_a";
        var setting = {
            view:{
                addHoverDom: addHoverDom,
                removeHoverDom:removeHoverDom,
                addDiyDom:addDiyDom,
                selectedMulti:false
                // showIcon:false
            },
            edit:{
                enable:true,
                editNameSelectAll:true,
                showRemoveBtn:showRemoveBtn,
                showRenameBtn:showRenameBtn,
                removeTitle:'delete permission',
                renameTitle:'edit pemission'
            },
            async: {
                enable:true,
                //url:'<?php echo site_url('c=permissions&m=data');?>',
                autoParam:[],
                otherParam:{}
            },
            callback:{
                beforeRemove:beforeRemove,
                beforeEditName:beforeEditName,
                onRemove:onRemove
            }
        };
        
        function beforeRemove(treeId, treeNode){
            var zTree = $.fn.zTree.getZTreeObj(treeId);
            zTree.selectNode(treeNode);
            return confirm('确认删除 节点 -- ' + treeNode.name + "吗？");
        }
        
        function onRemove(event, treeId, treeNode){
            console.log(treeNode);
            $.get('#', {id:treeNode.id}, function(data){ //<?php echo site_url('c=permissions&m=delete');?>
                var json = $.parseJSON(data);
                if(json.success){
                    toastr.success(json.msg);
                }else{
                    toastr.error(json.msg);
                }
            });
        }
        
        function beforeEditName(treeId, treeNode){
           // location.href = '#/permissions?m=edit&id=' + treeNode.id;
            return false;
        }
        
        function showRemoveBtn(treeId, treeNode){
            if(treeNode.hasOwnProperty('children')){
                return false;
            }else{
                return true;
            }
        }
        
        function showRenameBtn(treeId, treeNode){
            return true;
        }
        
        var newCount = 1;
        
        function addHoverDom(treeId, treeNode){
            var sObj = $('#' + treeNode.tId + '_span');
            if(treeNode.editNameFlag || $('#addBtn_' + treeNode.tId).length>0) return;
            var addStr = '<span class="button add" id="addBtn_' + treeNode.tId + '" title="添加权限" onfocus="this.blur();"></span>';
            sObj.after(addStr);
            var btn = $('#addBtn_' + treeNode.tId);
            if(btn){
                btn.bind('click', function(){
                    location.href = '#/permissions?m=add&parent_id=' + treeNode.id;
                    return false;
                });
            } 
        }
        
        function removeHoverDom(treeId, treeNode){
            $('#addBtn_' + treeNode.tId).unbind().remove();
        }
        
        function addDiyDom(treeId, treeNode){
            var aObj = $('#' + treeNode.tId + IDMark_A);
            var diyStr = '(' + treeNode.permKey + ')';
            aObj.after(diyStr);
        }
                
        $(document).ready(function(){
            $.fn.zTree.init($("#treeDemo"), setting);
            $('#expandAllBtn').on('click', function(){
                var zTree = $.fn.zTree.getZTreeObj('treeDemo');
                zTree.expandAll(true);
            });
            $('#collapseAllBtn').on('click', function(){
                var zTree = $.fn.zTree.getZTreeObj('treeDemo');
                zTree.expandAll(false);
            });
        });
        