	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);
		$.fn.zTree.init($("#treeDemo"), setting1, result.data); //实例化树形图
	});
	}

	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');
      	tree();
      	clear();
	})
	//确定删除按钮
	$('.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();
              	clear();
              } else {
              	layer.msg("删除失败！");
              	tree();
              	clear();
              }
			}
		});
		$('.delModel').css('display', 'none');

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

	})
	//添加关闭按钮
	$('.addBtr').click(function() {
		$('.addModel').css('display', 'none');
		tree();
		clear();
	})
	//添加内容下拉
	$('.inps1').focus(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,addSort;
	//添加弹出框内 所属部门下拉框内数据
	function treeCont() {
		$.get(huayi.config.callcenter_url + 'KnowledgeClass/GetList', {
			"token": $.cookie("token"),
//			"pid": pid
		}, function(result) {
			result = $.parseJSON(result);
			$.fn.zTree.init($("#addTreeDemo"), setting2, result.data); //实例化树形图
		});
	}
	$('.addCun').click(function(){
		if($('.inps2').val()==""){
			layer.confirm('所添加分类名称不能为空！', {
					btn: ['确定']
				});
				return;
		}else{
		    addFenLeiName = $('.inps2').val();
		    addSort = $('.inps3').val();
		    if (addSort == "")
		        addSort = 0;
				$('.addModel').css('display', 'none');
			$.ajax({
				type:"post",
				url:huayi.config.callcenter_url+"KnowledgeClass/AddClass",
				dataType:'json',
				async:true,
				data:{
					pid:addPid,//当前选择节点ID
					cname:addFenLeiName,//部门名称
					sort:addSort,//排列序号
					token:token
				},
				success:function(data){
					//console.log(data)
					//console.log(data.state);
					if (data.state =="success"){
						layer.msg("添加成功！");
						tree();
						clear();
					} else {
						layer.msg("添加失败！");
						tree();
						clear();
					}
				}
			});
	
		}
		
	
	})
	//修改按钮
	var chanPid,//修改弹出框内 下拉框父节点ID
		chanId,//修改弹出框内 下拉框当前节点ID
	changeName,//修改弹出框内 下拉框当前节点name
	  chbcName,//保存的分类名称
	  chbcSort,//保存的排序号
	   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');
		tree();
		clear();
	})
	$('.inps4').focus(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);
			$.fn.zTree.init($("#changeTreeDemo"), setting3, result.data); //实例化树形图
		});
	}
	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);
				$('.inps6').val(chanCon.F_Sort);
				chbcSort=chanCon.F_Sort;
				chbcName=chanCon.F_CategoryName;
				//console.log(chbcPid)
			}
		});
	}
	//修改的保存按钮功能
	function changeBaoCunAjax(id,chbcPid,chbcName,chbcSort){
			$.ajax({
				type:"post",
				url:huayi.config.callcenter_url+"KnowledgeClass/EditClass",
				async:true,
				dataType:'json',
				data:{
					cid:id,
					pid:chbcPid,
					cname:chbcName,
					sort:chbcSort,
					token:token
				},
				success:function(data){
					if(data.state == "success"){
						layer.msg("修改成功！");
						tree();
						clear();
					} else {
						layer.msg("修改失败！");
						tree();
						clear();
					}
				}
			});
	}
	
	$('.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();
			chbcSort = $('.inps6').val();
			if (chbcSort == "")
			    chbcSort = 0;
			changeBaoCunAjax(id,chbcPid,chbcName,chbcSort);
		}
		
	})
	
	function clear() {
		$('.inps1').val('');
		$('.inps2').val('');
		$('.inps3').val('');
		name = '';
		id = 0;
		chbcPid = 0;
	}
	
	
