var dataTable = null;
var treeObj = null;
        var setting = {
        		check : {
        			enable : true,
        			chkDisabledInherit : true
        		},
                data: {
                    simpleData: {
                        enable: true
                    }
                },
                view: {
                    fontCss: setHighlight // 高亮一定要设置，setHighlight是自定义方法
                },
            	callback : {
            		beforeCheck : beforeCheck,
            		onClick: zTreeOnClick
            	}
        };
        
// tree节点选中之前进行判断
var className = "dark";
function beforeCheck(treeId, treeNode) {
	className = (className === "dark" ? "" : "dark");
	return (treeNode.doCheck !== false);
}
var typeId="";
function zTreeOnClick() {
    var zTree = $.fn.zTree.getZTreeObj("tree");
    
    if (zTree != null) {
        var nodes = zTree.getSelectedNodes();   //获取树控件选中对象集合
        
        // 关闭所有节点的高亮
        var allNodes = treeObj.transformToArray(treeObj.getNodes());
        for (var i = 0; i < allNodes.length; i ++) {
            allNodes[i].highlight = false;
            treeObj.updateNode(allNodes[i]);
        }
        
        // 高亮并展开搜索到的节点
        for (var i = 0; i < nodes.length; i ++) {
            nodes[i].highlight = true;
            treeObj.updateNode(nodes[i]); // 更新节点，让高亮生效
        }
        
        typeId = nodes[0].rd_Index;
        cc(typeId);
        
    }
}
function getname() {
    return typeId;
}
        function getTree() {
        	jQuery.ajax({
        		type : 'POST',
        		dataType : "json",
        		url : '/pqs9900/type/getTree',
        		data : {},
        		async:false,
        		error : function() {
        			console.log("获取终端树请求发送失败！");
        		},
        		success : function(data) {
        			var datalist = [];
        			for ( var i = 0; i < data.length; i++) {
        				var str = '';

        				str += '{' + "id:" + data[i].id;
        				str += ',' + "name:'" + data[i].name + "'";
        				str += ',' + "nodeIndex:" + data[i].nodeIndex;
        				str += ',' + "nodeType:" + data[i].nodeType;
        				str += ',' + "ppId:" + data[i].ppId;
        				if(data[i].nodeType == "0"){
        					str += ',' + "open:" + true;
        				}else{
        					str += ',' + "open:" + false;
        				}
        				str += ',' + "describe:" + data[i].describe;
        				str += ',' + "rd_Index:'" + data[i].rdIndex + "'";
        				str += ',' + "pId:" + data[i].pId + '}';
        				datalist.push(str);
        			}
        			datalist = eval('[' + datalist + ']');// string->json数组

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


        $(document).ready(function(){
        	getTree();
        });
        
        // 设置高亮字体
        function setHighlight(treeId, treeNode) {
            return (!!treeNode.highlight) ? {color:"#000000", "font-weight":"bold"} : {color:"#666", "font-weight":"normal"};
        }
function cc(typeId){
    //查询所有
    $.ajax({
        type : "post",
        url : "/pqs9900/dic/selAll",
        data : {
            typeId : typeId
        },
        beforeSend: function () {
            i = ityzl_SHOW_LOAD_LAYER();
        },
        success : function(data) {
            var height = $(window).height() - 238;
            var length = Math.floor(height / 28);
            if (length < 1) {
                length = 1;
            }
            // var heightTemp = screen.height;
            // var length;
            // if (heightTemp === 768) {
            //     length = 10;
            // } else if (heightTemp === 1024) {
            //     length = 20;
            // } else if (heightTemp === 960) {
            //     length = 15;
            // } else if (heightTemp === 1050) {
            //     length = 18;
            // } else {
            //     length = 15;
            // }
            dataTable = $("#dicTyId").DataTable();
            dataTable.clear().draw();
            dataTable = $('#dicTyId').DataTable({
                destroy: true,
                dom: 'rtip',
                ordering: false,
                columnDefs: [
                    {
                        orderable: false,
                        targets: [0]
                    }
                ],
                "columns": [
                    {"width": "15%"},
                    {"width": "8%"},
                    {"width": "15%"},
                    {"width": "10%"},
                    {"width": "6%"},
                    {"width": "12%"}
                ],
                data: data.body,
                pageLength: length,
                language: {
                    url: '/pqs9900/json/chine.json'
                },
                    'fnDrawCallback': function(oTable) {
                        $("#dicTyId_paginate").append("<div style='float:right;padding-top:4px;padding-left:10px'>到第 <input type='text' id='changePage' class='input-text' style='width:50px;height:28px;'> 页  <button class='btn btn-primary' id='dataTable-btn'>确定</button></div>");
                        var table = $("#dicTyId").dataTable();
                        $('#dataTable-btn').click(function(e) {
                            if($("#changePage").val() && $("#changePage").val() > 0) {
                                var redirectpage = $("#changePage").val() - 1;
                            } else {
                                var redirectpage = 0;
                            }
                            table.fnPageChange(redirectpage);
                        });
                    }
                });
            //***********关闭loading
            ityzl_CLOSE_LOAD_LAYER(i);
            }
    });
}

function searchNode(value) {
    if (value == "") return;

    // 获取搜索的节点
    var nodes = treeObj.getNodesByParamFuzzy("name", value);
    // 关闭所有节点的高亮
    var allNodes = treeObj.transformToArray(treeObj.getNodes());
    for (var i = 0; i < allNodes.length; i ++) {
        allNodes[i].highlight = false;
        treeObj.updateNode(allNodes[i]);
    }
    // 展开第一层节点
    treeObj.expandAll(false);
    treeObj.expandNode(treeObj.getNodes()[0],true);
    // 高亮并展开搜索到的节点
    for (var i = 0; i < nodes.length; i ++) {
        nodes[i].highlight = true;
        treeObj.updateNode(nodes[i]); // 更新节点，让高亮生效
        treeObj.expandNode(nodes[i].getParentNode(),true);
    }
}