var width = -10;
var height = -10;
var zNodes,rMenu;
var menu = {
    setting: {
        view:{
            fontCss : {"font-family": "微软雅黑","color":"blue"}
        },
        data:{
            simpleData: {
                enable: true,
                idKey: "id",
                pIdKey: "parentId",
            },
        },
        callback: { //回调函数
            onRightClick: OnRightClick,
            onClick: zTreeOnClick
        }
    },
    loadMenuTree:function(){
        $.ajax({
            type: "get",
            url:path+"/menuPower/getDictPowerTree",
            dataType:"json",
            success:function(data){
                $.fn.zTree.init($("#treeDemo"), menu.setting, data);
                zTree = $.fn.zTree.getZTreeObj("treeDemo");
                var nodes = zTree.getNodes();
                rMenu = $("#rMenu");
                for (var i = 0; i < nodes.length; i++) { //设置节点展开
                    zTree.expandNode(nodes[i], true, false, true);
                }
                zNodes = data;
                $('#loadingModal').modal('hide');
            }
        })
    }
};
$(function(){
    loadTree();
})
function loadTree(){
    menu.loadMenuTree();
    //初始化模糊搜索方法
    fuzzySearch('treeDemo','#key',null,true,menu.setting,zNodes);
}
//tree右键点击事件
function OnRightClick(event, treeId, treeNode) {
    if(treeNode != null){
        if (!treeNode && event.target.tagName.toLowerCase() != "button" && $(event.target).parents("a").length == 0) {
            zTree.cancelSelectedNode();
            showRMenu("root", event.clientX-width, event.clientY);
        } else if (treeNode && !treeNode.noR) {
            zTree.selectNode(treeNode);
            showRMenu("node", event.clientX-height, event.clientY,treeNode);
        }
    }
}
//显示右键菜单
function showRMenu(type, x, y,treeNode) {
    $("#iframe").prop("src",path+"/dict/dictManagePage");
    $("#rMenu ul").show();
    var level = treeNode.level;
    var selfId = treeNode.selfId;
    var children = treeNode.children;
    if (type=="root") {
        $("#m_del").hide();
        $("#m_add").hide();
        $("#m_reset").hide();
    } else {
        $("#rMenu").css("height",67);
        //删除，只能最后一层或是系统才能删除
        if(level != 0 && selfId >= 0 && children == undefined){
            $("#m_del").show();
        }else{
            $("#m_del").hide();
            $("#rMenu").css("height",$("#rMenu").height()-17);
        }
        //添加，系统不能添加
        if(level > 1 && selfId >= 0 && children == undefined) {
            $("#m_add").hide();
            $("#rMenu").css("height",$("#rMenu").height()-17);
        }else{
            $("#m_add").show();
        }

        //修改，只有医院或系统才能修改
        if(level != 0 && selfId >= 0){
            $("#m_reset").show();
        }else{
            $("#m_reset").hide();
            $("#rMenu").css("height",$("#rMenu").height()-17);
        }
    }
    y += document.body.scrollTop;
    x += document.body.scrollLeft;
    rMenu.css({"top":y+"px", "left":x+"px", "visibility":"visible"});
    $("body").bind("mousedown", onBodyMouseDown);
}
//隐藏右键菜单
function hideRMenu() {
    if (rMenu) rMenu.css({"visibility": "hidden"});
    $("body").unbind("mousedown", onBodyMouseDown);
}

function onBodyMouseDown(event){
    if (!(event.target.id == "rMenu" || $(event.target).parents("#rMenu").length>0)) {
        rMenu.css({"visibility" : "hidden"});
    }
}

//添加节点
function addTreeNode() {
    debugger
    hideRMenu();
    //树的层级
    var node = zTree.getSelectedNodes()[0];
    $("#currentTreeId").val(node.selfId);
    var treeLevel = node.level;
    var selfId = node.selfId;
    if(treeLevel > 2 || node.sysType == '权限系统'){
        toastr.warning("系统不能再往下添加了!");
    }else{
        //医院层level为1,只能创建去权限系统
        if(treeLevel == 1){
            $.ajax({
                type: "get",
                url: path+"/dict/getHospitalByParentId",
                data:{dictId:selfId},
                dataType:'json',
                success:function(data){
                    if(data.code == 200){
                        toastr.warning("只能创建权限系统，已存在权限系统，请选择科室!");
                    }else{
                        $("#iframe").prop("src",path+"/dict/dictManagePage?level="+treeLevel+"&selfId="+selfId+"&operFlag=add")
                    }
                }
            })
        }else{
            var deptId = '';
            if(treeLevel > 0 || selfId < 0){
                //科室层，获取父节点selfId
                selfId = node.getParentNode().selfId;
                deptId = node.selfId;
            }
            $("#iframe").prop("src",path+"/dict/dictManagePage?level="+treeLevel+"&selfId="+selfId+"&operFlag=add"+"&deptId="+deptId)
        }
    }
}
//删除节点
function removeTreeNode() {
    hideRMenu();
    var nodes = zTree.getSelectedNodes();
    if (nodes && nodes.length>0) {
        if (nodes[0].children && nodes[0].children.length > 0) {
            var msg = "要删除的节点是父节点，先从子节点删除";
            toastr.warning(msg);
        } else {
            Common.confirm({
                title: "提示",
                message: "确定是否删除此条记录?",
                operate: function (reselt) {
                    if (reselt) {
                        var selfId = nodes[0].selfId;
                        if (selfId != null) {
                            $.ajax({
                                type: "post",
                                url: path + "/dict/delete",
                                data: {dictId: selfId},
                                dataType: 'json',
                                success: function (data) {
                                    if (data.msg == 'success') {
                                        toastr.success("删除成功!");
                                        loadTree();
                                    } else {
                                        toastr.error("删除失败,请联系系统管理员!");
                                    }
                                }
                            })
                        } else {
                            toastr.warning("删除无效!");
                        }
                    }
                }
            })
        }
    }
}
function checkTreeNode(checked) {
    var nodes = zTree.getSelectedNodes();
    if (nodes && nodes.length>0) {
        zTree.checkNode(nodes[0], checked, true);
    }
    hideRMenu();
}
//更新节点
function updateTree() {
    hideRMenu();
    var node = zTree.getSelectedNodes()[0];
    $("#currentTreeId").val(node.selfId);
    var treeLevel = node.level;
    var selfId = node.selfId;
    if(treeLevel != 0 && selfId >= 0 || selfId == -100){
        $("#iframe").prop("src",path+"/dict/dictManagePage?level="+treeLevel+"&selfId="+selfId+"&operFlag=edit");
    }else{
        $("#iframe").prop("src",path+"/dict/dictManagePage");
        toastr.warning("请选中医院或系统!");
    }
}

function zTreeOnClick(event, treeId, treeNode) {
    var treeLevel = treeNode.level;
    var selfId = treeNode.selfId;
    $("#currentTreeId").val(selfId);
    if(treeLevel != 0 && selfId >= 0){
        $("#iframe").prop("src",path+"/dict/dictManagePage?level="+treeLevel+"&selfId="+selfId+"&operFlag=edit");
    }else{
        $("#iframe").prop("src",path+"/dict/dictManagePage");
        toastr.warning("请选中医院或系统!");
    }
}
//左侧树全部展开
function expandAll(){
    var treeObj=$.fn.zTree.getZTreeObj("treeDemo");
    treeObj.expandAll(true);
}
//左侧树全部关闭
function collapseAll(){
    var treeObj=$.fn.zTree.getZTreeObj("treeDemo");
    treeObj.expandAll(false);
}