/*********************************************************************************************************************
 * zTree操作deviceTree代码段
 *********************************************************************************************************************/
//zTree属性配置
var
    setting = {
        check: {
            enable: true,
            nocheckInherit: true
        },
        data: {
            simpleData: {
                enable: true
            }
        },
        view: {
            fontCss: setHighlight // 高亮一定要设置，setHighlight是自定义方法
        },
        callback: {
            beforeClick: pointSelected,
            onClick: pointSelected
        }
    };

function pointSelected() {
    var zTree = $.fn.zTree.getZTreeObj("tree");
    var nodes = zTree.getSelectedNodes();
    var arrayNodes = zTree.transformToArray(nodes);

    if (0 === nodes.length) {
        return;
    }

    //获取每个节点的id添加空格作为表格的查询条件筛选数据
    try {
        if (typeof(valueChange) === "function") {
            arrayNodes[0].rdIndex == "0" ? valueChange("parentNull") : valueChange(arrayNodes[0].rdIndex);
        }
    } catch (e) {
        console.error('function test exception.');
    }

    //选中字体高亮
    nodes[0].highlight = !nodes[0].highlight;
    zTree.updateNode(nodes[0]);
}

function getAllSelected() {
    var zTree = $.fn.zTree.getZTreeObj("tree");
    return zTree.getCheckedNodes(true);
}

// 设置高亮字体
function setHighlight(treeId, treeNode) {
    return (!!treeNode.highlight) ? {color: "#000000", "font-weight": "bold"} : {
        color: "#666",
        "font-weight": "normal"
    };
}

/*********************************
 * 是否显示checkBox
 * type-1:显示
 * type-2:不显示
 *********************************/
function triggerCheckBox(type) {
    var zTree = $.fn.zTree.getZTreeObj("tree");
    var nodes = zTree.transformToArray(zTree.getNodes());
    var isShow = false;

    if (type == 2) {
        isShow = true;
    }

    for (var i = 0; i < nodes.length; i++) {
        nodes[i].nocheck = isShow;
        zTree.updateNode(nodes[i]);
    }
}

function initDiv() {
    document.getElementById("father").style.height = document.body.offsetHeight - 4 + "px"; //减去border的宽度
    document.getElementById("tree").style.height = document.getElementById("father").offsetHeight - 2 + "px"; //减去border的宽度
}

/*********************************************************************************************************************
 * zTree操作deviceTree代码段结束
 *********************************************************************************************************************/
function getTreeSelected() {
    var pointNodes = [];
    var nodes = getAllSelected();

    if (0 === nodes.length) {
        return pointNodes;
    }

    for (var i = 0; i < nodes.length; i++) {
        if (nodes[i].check_Child_State == -1) { //不存在子节点
            pointNodes.push(nodes[i]);
        }
    }

    return pointNodes;
}

function setTreeSelect(data) {
    var zTree = $.fn.zTree.getZTreeObj("tree");
    var nodes = zTree.transformToArray(zTree.getNodes());

    for (var i = 0; i < nodes.length; i++) {
        for (var j = 0; j < data.length; j++) {
            if (nodes[i].rdIndex === data[j]) {
                zTree.checkNode(nodes[i], true, true, false);
                break;
            }
        }
    }
}

$(function () {
    initDiv();
    getData();
    triggerCheckBox(2);
});

//数据库获取数据
function getData() {
    $.ajax({
        url: "/pqs9900/report/excelsetting",
        type: "post",
        async: false,
        dataType: "json",
        success: function (data) {
            initTree(data.body);

            try {
                if (typeof(setTableData) === "function") {
                    setTableData(data.body);
                }
            } catch (e) {
                console.error('function test exception.');
            }
        },
        error: function () {
            console.error("获取excel报表基础配置请求异常.");
            layer.msg("获取excel报表基础配置失败.");
        }
    });
}

//初始化终端树
function initTree(treeData) {
    if (treeData.length == 0) {
        layer.msg("数据库数据为空,excel报表基础配置树载失败.");
        return;
    }

    var datalist = [];
    for (var i = 0; i < treeData.length; i++) {
        var str = '';
        str += '{id:' + treeData[i].id;
        str += ',name:"' + treeData[i].rdDescribe + '"';
        str += ',rdIndex:"' + treeData[i].rdIndex + '"';
        str += ',pId:' + treeData[i].pId + '}';
        datalist.push(str);
    }

    datalist = eval('[' + datalist + ']');// string->json数组
    var zNodes = datalist;

    var treeObj = $.fn.zTree.init($("#tree"), setting, zNodes);
    treeObj.expandAll(true);
}