var treeObj = null;
var setting = {
	check : {
		enable : true,
		chkDisabledInherit : true
	},
	data: {
		simpleData: {
			enable: true
		}
	},
	view: {
		fontCss: setHighlight // 高亮一定要设置，setHighlight是自定义方法
	},
	callback : {
		onClick: zTreeOnClick
	}
};

function zTreeOnClick() {
	var zTree = $.fn.zTree.getZTreeObj("tree");

	if (zTree != null) {
		var nodes = zTree.getSelectedNodes();   //获取树控件选中对象集合

		if (nodes[0].level == 0) {   //电科院
			$("#buttonarea button").attr("hidden","true");
//			$("input").attr("disabled","true");
			$("#addGd2").removeAttr("hidden");
//			layer.open({
//				  type: 2,
//				  title: '供电公司',
//				  shadeClose: true,
//				  shade: 0.3,
//				  area: ['380px', '350px'],
//				  content: 'addGd', 
//				  scrollbar: false,
//				  end:function(){
//					  getTree();
//				  }
//				});
		}else if(nodes[0].level == 1){    //供电公司
			$("#buttonarea button").attr("hidden","true");
//			$("input").attr("disabled","true");
			$("#addGd").removeAttr("hidden");
			$("#modGd").removeAttr("hidden");
			$("#delGd").removeAttr("hidden");
			$("#addSub2").removeAttr("hidden");
			
			var gdIndex=nodes[0].nodeIndex;
			$("#gdIndex").val(gdIndex);
//			var gdInfo=nodes[0].getParentNode();
//			layer.open({
//				  type: 2,
//				  title: '变电站',
//				  shadeClose: true,
//				  shade: 0.3,
//				  area: ['380px', '350px'],
//				  content: 'addSub?gdIndex='+gdInfo.nodeIndex, 
//				  scrollbar: false,
//				  end:function(){
//					  getTree();
//				  }
//				});
		}else if(nodes[0].level == 2){   //变电站
			$("#buttonarea button").attr("hidden","true");
//			$("input").attr("disabled","true");
			$("#addSub").removeAttr("hidden");
			$("#modSub").removeAttr("hidden");
			$("#delSub").removeAttr("hidden");
			$("#addDev2").removeAttr("hidden");

			var gdIndex=nodes[0].getParentNode().nodeIndex;
			$("#gdIndex").val(gdIndex);
			var subIndex=nodes[0].nodeIndex;
			$("#subIndex").val(subIndex);
		}else if(nodes[0].level == 3){    //终端
			$("#buttonarea button").attr("hidden","true");
//			$("input").attr("disabled","true");
			$("#addDev").removeAttr("hidden");
			$("#modDev").removeAttr("hidden");
			$("#delDev").removeAttr("hidden");
			$("#addSubv2").removeAttr("hidden");
			$("#devcommit").removeAttr("disabled");
			
			var gdIndex=nodes[0].getParentNode().getParentNode().nodeIndex;
			var gdname=nodes[0].getParentNode().getParentNode().name;
			$("#gdIndex").val(gdIndex);
			var subIndex=nodes[0].getParentNode().nodeIndex;
			var subname=nodes[0].getParentNode().name;
			$("#subIndex").val(subIndex);
			var devIndex=nodes[0].nodeIndex;
			$("#devIndex").val(devIndex);
			
			loadDevice(devIndex,gdname,subname);
		}else if(nodes[0].level == 4){     //母线
			$("#buttonarea button").attr("hidden","true");
//			$("input").attr("disabled","true");
			$("#addSubv").removeAttr("hidden");
			$("#modSubv").removeAttr("hidden");
			$("#delSubv").removeAttr("hidden");
			$("#addLine2").removeAttr("hidden");
			
			var gdIndex=nodes[0].getParentNode().getParentNode().getParentNode().nodeIndex;
			$("#gdIndex").val(gdIndex);
			var subIndex=nodes[0].getParentNode().getParentNode().nodeIndex;
			$("#subIndex").val(subIndex);
			var devIndex=nodes[0].getParentNode().nodeIndex;
			$("#devIndex").val(devIndex);
			var subvIndex=nodes[0].nodeIndex;
			$("#subvIndex").val(subvIndex);
		}else if(nodes[0].level == 5){    //线路
			$("#buttonarea button").attr("hidden","true");
//			$("input").attr("disabled","true");
			$("#addLine").removeAttr("hidden");
			$("#modLine").removeAttr("hidden");
			$("#delLine").removeAttr("hidden");
			
			var gdIndex=nodes[0].getParentNode().getParentNode().getParentNode().getParentNode().nodeIndex;
			var gdname=nodes[0].getParentNode().getParentNode().getParentNode().getParentNode().name;
			$("#gdIndex").val(gdIndex);
			var subIndex=nodes[0].getParentNode().getParentNode().getParentNode().nodeIndex;
			var subname=nodes[0].getParentNode().getParentNode().getParentNode().name;
			$("#subIndex").val(subIndex);
			var devIndex=nodes[0].getParentNode().getParentNode().nodeIndex;
			$("#devIndex").val(devIndex);
			var subvIndex=nodes[0].getParentNode().nodeIndex;
			$("#subvIndex").val(subvIndex);
			var lineIndex=nodes[0].nodeIndex;
			$("#lineIndex").val(lineIndex);
			
			$("#linecommit").removeAttr("disabled");
			loadLine(lineIndex);
			
			loadDevice(devIndex,gdname,subname);
		}
	}
}

function setHighlight(treeId, treeNode) {
    return (treeNode.highlight) ? {color:"#666", "font-weight":"bold","border-bottom":"dotted 1px #fff"} : {color:"#666", "font-weight":"normal"};
}

function getTree() {
	jQuery.ajax({
		type : 'POST',
		dataType : "json",
		url : '/pqs9200/device/tree',
		data : {},
		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 += ',' + "state:" + data[i].state;
				str += ',' + "pId:" + data[i].pId + '}';
				datalist.push(str);
			}
			datalist = eval('[' + datalist + ']');// string->json数组

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

$(document).ready(function(){
	getTree();
	var zTree = $.fn.zTree.getZTreeObj("tree");
});

function loadDevice(devIndex,gdname,subname){
	jQuery.ajax({
		type : 'POST',
		dataType : "json",
		url : '/pqs9200/device/loadDevice',
		data : {
			devIndex : devIndex
		},
		success : function(data) {
			$("#devname").val(data.name);
			$("#devtype").val(data.devType);
			$("#ip1").val(data.ip1);
			$("#ip2").val(data.ip2);
			$("#ip3").val(data.ip3);
			$("#ip4").val(data.ip4);
			
			if(data.status == 1){
				$("#status").val("正常");
			}else{
				$("#status").val("中断");
			}
			
			$("#gdname").val(gdname);
			$("#subname").val(subname);
			
			$("#LogonTime").val(data.logTime);
			$("#UpdateTime").val(data.updTime);
			$("#node").val(data.nodeIndex);
			
			$("#ThisTimeCheck").val(data.thisTimeCheck);
			$("#NextTimeCheck").val(data.nextTimeCheck);
			$("#manufacturer").val(data.manufacturer);
		}
	})
}

function loadLine(lineIndex){
	jQuery.ajax({
		type : 'POST',
		dataType : "json",
		url : '/pqs9200/device/loadLine',
		data : {
			lineIndex : lineIndex
		},
		success : function(data) {
			$("#linename").val(data.name);
			$("#scale").val(data.scale);
			$("#dlcMp").val(data.dlcMp);
			$("#xycMp").val(data.xycMp);
			$("#jzcMp").val(data.jzcMp);
			$("#devcMp").val(data.devcMp);
			$("#CT1").val(data.ct1);
			$("#CT2").val(data.ct2);
			$("#PT1").val(data.pt1);
			$("#PT2").val(data.pt2);
			$("#ptType").val(data.ptType);
			$("#tInterval").val(data.tinterval);
			$("#loadType").val(data.loadType);
			$("#bussinessType").val(data.businessType);
			$("#remark").val(data.remark);
			$("#monitorId").val(data.monitorId);
		}
	})
}