//areaTree的示例，数据中心的设备树
var areaTree;

// zTree 的参数配置，深入使用请参考 API 文档（setting 配置详解）
var settingAreaTree = {
    treeId: 'areaTree',
    data: {
        simpleData: {
            enable: true,
            idKey: "id",
            pidKey: "pId",
        }
    },
    view: {
        showIcon: false
    },
    callback: {
        onClick: focusZtree,
        onDblClick: setAreaValue,
    }
};



$(".areaicon").on("click",function (e) {
    stopPropagation(e);
    if($(".treeUpIcon").hasClass("disnone")){
        showAreas();
    }else{
        hideAreas();
    }
})
//显示下拉框
function showAreas() {
    $(".treeUpIcon").removeClass("disnone");
    $(".treeUpDiv").removeClass("disnone");
}
//隐藏下拉框
function hideAreas() {
    $(".treeUpIcon").addClass("disnone");
    $(".treeUpDiv").addClass("disnone");
}

//阻止冒泡事件
function stopPropagation(e) {
    if (e.stopPropagation)
        e.stopPropagation();
    else
        e.cancelBubble = true;
}
//点击除p元素以外隐藏下拉
$(document).bind("click",function(){
    hideAreas();
});
//树本身阻止冒泡隐藏树事件
$("#areaTree").on("click",function (e) {
    stopPropagation(e);
})

//区域框的事件
//点击事件
$("#area").on("click",function (e) {
    stopPropagation(e);
    if($(".treeUpIcon").hasClass("disnone")){
        showAreas();
    }else{
        hideAreas();
    }
})

function setAreaValue() {
    var zTree = $.fn.zTree.getZTreeObj("areaTree");
    if (zTree != null) {
        var nodes = zTree.getSelectedNodes(); //获取树控件选中对象集合
        var name = nodes[0].name;
        var id = nodes[0].id;
        $("#area").val(name);
        $("#area").attr("index", id);
        hideAreas();
    }
}
function focusZtree() {
    var zTree = $.fn.zTree.getZTreeObj("areaTree");
    if (zTree != null) {
        var nodes = zTree.getSelectedNodes(); //获取树控件选中对象集合
        var name = nodes[0].name;
        var id = nodes[0].id;
    }
};

//根据name找对应的区域索引
function getDeptsIndex(name) {
    var index;
    for (var i = 0; i < areaInfo.length; i++) {
        if (areaInfo[i].name === name) {
            index = areaInfo[i].id;
        }
    }
    return index;
}

//根据索引找对应的name
function getDeptsName(index) {
    var name;
    for (var i = 0; i < areaInfo.length; i++) {
        if (areaInfo[i].id === index) {
            name = areaInfo[i].name;
        }
    }
    return name;
}

//根据name找对应的父级区域索引
function getDeptsPIndex(name) {
    var index;
    for (var i = 0; i < areaInfo.length; i++) {
        if (areaInfo[i].name === name) {
            index = areaInfo[i].pId;
        }
    }
    return index;
}

