	var tps = $('.tps');
	var id=''; //当前节点id
	var name=''; //当前节点name
	var pid; //当前节点父ID
	var pidName;//当前节点父name
	var xlName; //添加弹出框内下拉框内节点name
	var token=$.cookie("token");
	tree();
	function tree(){
		
	$.get(huayi.config.callcenter_url+'KnowledgeClass/GetList', {
		"token": $.cookie("token"),
//		"pid": pid
	}, function(result) {
		result = $.parseJSON(result);
		var zTreeObj=$.fn.zTree.init($("#treeDemo"), setting1, result.data); //实例化树形图
		 var nodes = zTreeObj.getNodes();
            for (var i = 0; i < nodes.length; i++) { //设置节点展开
                zTreeObj.expandNode(nodes[i], true, false, true);
            }
	});
	}
	var setting1 = {
		data: {
			key: {
				name: "text"
			},
			simpleData: {
				enable: true,
				idKey: "id",
				rootPId: 0
			}
		},
		callback: {
			onClick: zTreeOnClick
		}
	};
	function zTreeOnClick(event, treeId, treeNode) {
//		pid = treeNode.pId;
		id = treeNode.id;
		name = treeNode.text;
		var pidnode = treeNode.getParentNode();
		if(pidnode) {
			pidName = pidnode.text;
			pid = pidnode.id;
		} else {
			pidName = "顶级分类";
			pid =0;
		}
		
	};
	var setting2 = {
		data: {
			key: {
				name: "text"
			},
			simpleData: {
				enable: true,
				idKey: "id",
				rootPId: 0
			}
		},
		callback: {
			onClick: addTreeClick
		}
	}
	function addTreeClick(event, treeId, treeNode) {
				addPid=treeNode.id;
				xlName = treeNode.text;
				$('.inps1').val(xlName);
	};
	//删除按钮
	$('.del').click(function() {
		if(id == 'null' || id == '') {
				layer.confirm('没有要删除的分类！', {
				btn: ['确定']
			});
			return
		} else {
			$('.delModel').css('display', 'block');
			$('.delName').html(name);
		}
	})
	//关闭按钮
	$('.delBtr').click(function() {
		$('.delModel').css('display', 'none');
	})
	//确定删除按钮
	$('.sure').click(function() {
		$.ajax({
			type: "get",
			url: huayi.config.callcenter_url+"KnowledgeClass/DelClass",
			async: true,
			dataType:'json',
			data: {
				ids: id,
				token:token
			},
			success: function(data) {
              if(data.state == "success"){
              layer.msg("删除成功！");
              tree();
              }
			}
		});
		$('.delModel').css('display', 'none');

	})
	//取消删除按钮
	$('.return').click(function() {
		$('.delModel').css('display', 'none');
	})
	//添加按钮
	$('.add').click(function() {
		$('.addModel').css('display', 'block');
			if(name) {
			tps.html(name);
			$('.inps1').val(name);
			addPid = id;
		} else {
			tps.html('顶级分类');
			$('.inps1').val('顶级分类');
			addPid = 0;
		}
		treeCont();

	})
	//添加关闭按钮
	$('.addBtr').click(function() {
		$('.addModel').css('display', 'none');
	})
	//添加内容下拉
	$('.inps1').click(function() {
		$('.xlAdd').css('display', 'block')
	})
	$('.xl_one').click(function() {
		if($('.xlAdd').css('display') == 'block') {
			$('.xlAdd').css('display', 'none')
		} else {
			$('.xlAdd').css('display', 'block')
		}
	})
	$('.addTree').mouseleave(function() {
		$(this).css('display', 'none')
	})
	//保存添加按钮
	var addPid,addFenLeiName;
	//添加弹出框内 所属部门下拉框内数据
	function treeCont() {
		$.get(huayi.config.callcenter_url + 'KnowledgeClass/GetList', {
			"token": $.cookie("token"),
//			"pid": pid
		}, function(result) {
			result = $.parseJSON(result);
			var zTreeObj_add=$.fn.zTree.init($("#addTreeDemo"), setting2, result.data); //实例化树形图
			 var nodes_add = zTreeObj_add.getNodes();
            for (var i = 0; i < nodes_add.length; i++) { //设置节点展开
                zTreeObj_add.expandNode(nodes_add[i], true, false, true);
            }
		});
	}
	$('.addCun').click(function(){
		console.log(addPid);
		if($('.inps2').val()==""){
			layer.confirm('所添加分类名称不能为空！', {
					btn: ['确定']
				});
				return;
		}else{
		    addFenLeiName = $('.inps2').val();
			$('.addModel').css('display', 'none');
			$.ajax({
				type:"post",
				url:huayi.config.callcenter_url+"KnowledgeClass/AddClass",
				dataType:'json',
				async:true,
				data:{
					pid:addPid,//当前选择节点ID
					cname:addFenLeiName,//部门名称
					token:token
				},
				success:function(data){
					//console.log(data)
					//console.log(data.state);
			if(data.state =="success"){
				layer.msg("添加成功！");
				tree();
				 $('.inps1').val('');
				 $('.inps2').val('');
				 $('.inps3').val('');
			}
				}
				
			});
	
		}
		
	
	})
	//修改按钮
	var chanPid,//修改弹出框内 下拉框父节点ID
		chanId,//修改弹出框内 下拉框当前节点ID
	changeName,//修改弹出框内 下拉框当前节点name
	  chbcName,//保存的分类名称
	   chbcPid;//保存的所属分类ID
	$('.change').click(function(){
		if(id == 'null' || id == '') {
			layer.confirm('没有选择要修改的分类！', {
				btn: ['确定']
			})
			return

		} else {
			changeAjax(id);
			$('.changeModel').css('display', 'block');
			$('.chtps').html(name);
		changeTreeCont();
		}
		

	})
	$('.changeBtr').click(function(){
			$('.changeModel').css('display', 'none');
	})
	$('.inps4').click(function() {
		$('.xlChange').css('display', 'block')
	})
	//下拉按钮功能
	$('.xl_two').click(function() {
		if($('.xlChange').css('display') == 'block') {
			$('.xlChange').css('display', 'none')
		} else {
			$('.xlChange').css('display', 'block')
		}

	})
	//修改弹出框内 下拉树形图参数配置项
		var setting3= {
		data: {
			key: {
				name: "text"
			},
			simpleData: {
				enable: true,
				idKey: "id",
				rootPId: 0
			}
		},
		callback: {
			onClick: changeTreeClick
		}
	}

	function changeTreeClick(event, treeId, treeNode) {
//		chanPid = treeNode.pId;
		chanId = treeNode.id;
		changeName = treeNode.text;
		$('.inps4').val(changeName);
		var pidnode = treeNode.getParentNode();
		if(pidnode) {
			chanPid = pidnode.id;
		} else {
			chanPid =0;
		}
		chbcPid=chanId;
		
	};
	
	//修改弹出框内 所属分类下拉框内数据
	function changeTreeCont() {
			$.get(huayi.config.callcenter_url + 'KnowledgeClass/GetList', {
			"token": $.cookie("token"),
//			"pid": pid
		}, function(result) {
			result = $.parseJSON(result);
			var zTreeObj2=$.fn.zTree.init($("#changeTreeDemo"), setting3, result.data); //实例化树形图
			 var nodes2 = zTreeObj2.getNodes();
            for (var i = 0; i < nodes2.length; i++) { //设置节点展开
                zTreeObj2.expandNode(nodes2[i], true, false, true);
            }
		});
	}
	function changeAjax(id){
		$.ajax({
			type:"get",
			url:huayi.config.callcenter_url+"KnowledgeClass/GetClass",
			dataType:'json',
			async:true,
			data:{
				categoryid:id,
				token:token
			},
			success:function(data){
				var chanCon=data.data;
				if(pid==0){
					$('.inps4').val('顶级分类');
					chbcPid =pid;
				}else{
					$('.inps4').val(pidName);
					chbcPid=chanCon.F_ParentId;
				}
				$('.inps5').val(chanCon.F_CategoryName);
				chbcName=chanCon.F_CategoryName;
				//console.log(chbcPid)
			}
		});
	}
	//修改的保存按钮功能
	function changeBaoCunAjax(id,chbcPid,chbcName){
			$.ajax({
				type:"post",
				url:huayi.config.callcenter_url+"KnowledgeClass/EditClass",
				async:true,
				dataType:'json',
				data:{
					cid:id,
					pid:chbcPid,
					cname:chbcName,
					token:token
				},
				success:function(data){
					if(data.state == "success"){
						layer.msg("修改成功！");
						tree();
					}
				}
			});
	}
	
	$('.changeCun').click(function(){
		if($('.inps4').val()=="" ){
		    layer.confirm('请选择分类！', {
				btn: ['确定']
			});
			return;
		}
	else if($('.inps5').val()=="")
		{
	    layer.confirm('所修改分类名称不允许为空！', {
	        btn: ['确定']
	    });
	    return;
		}
		else {
			$('.changeModel').css('display', 'none');
			//console.log('分类id'+id +'所属分类id' + chbcPid +'分类名称'+chbcName+'排序'+chbcSort);
			chbcPid;
			chbcName=$('.inps5').val();
			
			changeBaoCunAjax(id,chbcPid,chbcName);
		}
		
	})
	
	
