//树操作
var zNodes;
var menu = {
	setting: {
		view:{
			fontCss : {"font-family": "微软雅黑","color":"blue"}
		},
		data:{
			key:{
				name:"newName"
			},
			simpleData: {
				enable: true,
				idKey: "id",
				pIdKey: "parentId",
			}
		},
		callback: { //回调函数
			onClick: zTreeOnClick }
	},
	loadMenuTree:function(){
		$.ajax({
			type: "post",
			url:path+"/dictionary/getDictionaryTree?searchContext="+$("#searchContext").val(),
			dataType:"json",
			success:function(data){
				$('#myModal').modal('hide');
				$.fn.zTree.init($("#ztree"), menu.setting, data);
				zTree = $.fn.zTree.getZTreeObj("ztree");
				var nodes = zTree.getNodes();
				for (var i = 0; i < nodes.length; i++) { //设置节点展开
					zTree.expandNode(nodes[i], true, false, true);
				}
				zNodes = data;
			}
		})
	}
};
//加载树
$().ready(function(data){
	menu.loadMenuTree();
	$("#loading").css("background","url("+path+"/static/img/load.gif) no-repeat 10px 50%");
	$('#myModal').modal('show');
});
//加载树
function reloadTree(){
	//初始化模糊搜索方法
	menu.loadMenuTree();
	setTimeout(function(){
		var currentId = $("#treeId").val();
		if(currentId != ''){
			var zTree = $.fn.zTree.getZTreeObj("ztree");
			var node = zTree.getNodeByParam("id",currentId);
			zTree.selectNode(node);
		}
	},1000)
}

//树点击事件
function zTreeOnClick(event, treeId, treeNode) {
	var level = treeNode.level;
	$("#treeId").val(treeNode.id);
	$("#typeCode").val(treeNode.typecode);
	$("#newName").val(treeNode.newName);
	var effective = treeNode.effective;
	if(level == 0 && (effective == '' || effective == null)){
		$("#contentDiv").hide();
	}else{
		$("#contentDiv").show();
		$("#form")[0].reset();
		//赋值当前节点id
		$("#id").val(treeNode.id);
		$("#typecode").val(treeNode.typecode);
		$("#typename").val(treeNode.typename);
		$("#code").val(treeNode.code);
		$("#name").val(treeNode.name);
		$("#pyCode").val(treeNode.pyCode);
		$("#wbCode").val(treeNode.wbCode);
		$("#zipCode").val(treeNode.zipCode);
		$("#gbCode").val(treeNode.gbCode);
		$("#cComment").val(treeNode.cComment);
		var flag = treeNode.flag;
		if(flag == 1){
			$("#flag").prop("checked",true);
		}else{
			$("#flag").prop("checked",false);
		}
		//获取父节点
		$("#parentId").empty();
		var parentName = '';
		var newParentId = '0';
		//var newParentId = treeNode.newParentId;
		if(level == 0 && effective != ''){
			$.ajax({
				type:'get',
				url:path+'/dictionary/getDictionaryByParentId',
				data:{parentId:newParentId},
				dataType:'json',
				async:false,
				success:function(data){
					parentName = data;
				}
			})
		}else{
			parentName = treeNode.getParentNode().newName;
		}
		var parentTypeCode = treeNode.getParentNode().typecode;
		if(parentTypeCode != '' && parentTypeCode != null){
			newParentId  = parentTypeCode;
		}
		$("#parentId").append("<option value='"+newParentId+"'>"+parentName+"</option>");
		$("#effective").val(treeNode.effective);
		$("#customizeSort").val(treeNode.customizeSort);
		$("#remark").val(treeNode.remark);
		//是0为根节点显示类型输入框，否则显示代码框
		if($("#parentId").val() == 0){
			$(".typeDiv").show();
			$(".codeDiv").hide();
		}else{
			$(".typeDiv").hide();
			$(".codeDiv").show();
		}
	}
	//备注同节点信息
	var sblingNode = treeNode.children;
	var treeTypeCode = '';
	var treeTypeName = '';
	var treeCode = '';
	var treeName = '';
	$("#treeTypeCode").val("");
	$("#treeTypeName").val("");
	$("#treeCode").val("");
	$("#treeName").val("");
	$("#childNode").val("");
	if(sblingNode != undefined){
		for (var i = 0; i < sblingNode.length; i++) {
			if(sblingNode[i].typecode != '' && sblingNode[i].typecode != null){
				treeTypeCode += sblingNode[i].typecode + ",";
			}
			if(sblingNode[i].typename != '' && sblingNode[i].typename != null){
				treeTypeName += sblingNode[i].typename + ",";
			}
			if(sblingNode[i].code != '' && sblingNode[i].code != null){
				treeCode += sblingNode[i].code + ",";
			}
			if(sblingNode[i].name != '' && sblingNode[i].name != null){
				treeName += sblingNode[i].name + ",";
			}
			$("#treeTypeCode").val(treeTypeCode);
			$("#treeTypeName").val(treeTypeName);
			$("#treeCode").val(treeCode);
			$("#treeName").val(treeName);
		}
		$("#childNode").val(sblingNode);
	}
	//判断显示添加子节点按钮
	var parentId = treeNode.newParentId;
	if(null != parentId && parentId != 0){
		$("#addBtn").hide();
	}else{
		$("#addBtn").show();
	}
};
//添加按钮
function add(){
	var parentId = $("#treeId").val();
	var name = $("#newName").val();
	var typecode = $("#typecode").val();
	if(parentId == ''){
		toastr.warning("请选中节点!");
	}else{
		$("#contentDiv").show();
		$("#form")[0].reset();
		$("#id").val("");
		$("#parentId").empty();
		//根目录节点情况
		if(typecode == ''){
			typecode = '0';
		}
		$("#parentId").append("<option value='"+typecode+"'>"+name+"</option>");
		//是0为根节点显示类型输入框，否则显示代码框
		if($("#parentId").val() == 0){
			$(".typeDiv").show();
			$(".codeDiv").hide();
		}else{
			$(".typeDiv").hide();
			$(".codeDiv").show();
		}
	}
}
//验证类别代码重复
$("#typecode").blur(function(){
	var typecode = $("#typecode").val();
	var id = $("#id").val();
	var parentId = $("#parentId").val();
	if(id == '' && typecode != '' && parentId == 0){
		var treeTypeCode = $("#treeTypeCode").val();
		if(treeTypeCode != ''){
			var flag = false;
			var treeTypeCodes = treeTypeCode.split(",");
			for (var i = 0; i < treeTypeCodes.length; i++) {
				if(treeTypeCodes[i] != '' && treeTypeCodes[i] == typecode){
					flag = true;
					break;
				}
			}
			if(flag){
				toastr.warning("类别代码已存在!");
				$("#typecode").val("");
				$("#typecode").focus();
			}
		}
	}
})
//验证类别名称重复
$("#typename").blur(function(){
	var typename = $("#typename").val();
	var id = $("#id").val();
	var parentId = $("#parentId").val();
	if(id == '' && typename != '' && parentId == 0){
		var treeTypeName = $("#treeTypeName").val();
		if(treeTypeName != ''){
			var flag = false;
			var treeTypeNames = treeTypeName.split(",");
			for (var i = 0; i < treeTypeNames.length; i++) {
				if(treeTypeNames[i] != '' && treeTypeNames[i] == typename){
					flag = true;
					break;
				}
			}
			if(flag){
				toastr.warning("类别名称已存在!");
				$("#typename").val("");
				$("#typename").focus();
			}
		}
	}
})
//验证代码重复
$("#code").blur(function(){
	var code = $("#code").val();
	var id = $("#id").val();
	var parentId = $("#parentId").val();
	if(id == '' && code != '' && parentId != 0){
		var treeCode = $("#treeCode").val();
		if(treeCode != ''){
			var flag = false;
			var treeCodes = treeCode.split(",");
			for (var i = 0; i < treeCodes.length; i++) {
				if(treeCodes[i] != '' && treeCodes[i] == code){
					flag = true;
					break;
				}
			}
			if(flag){
				toastr.warning("代码已存在!");
				$("#code").val("");
				$("#code").focus();
			}
		}
	}
})
//验证名称重复
$("#name").blur(function(){
	var name = $("#name").val();
	var id = $("#id").val();
	var parentId = $("#parentId").val();
	if(id == '' && name != '' && parentId != 0){
		var treeName = $("#treeName").val();
		if(treeName != ''){
			var flag = false;
			var treeNames = treeName.split(",");
			for (var i = 0; i < treeNames.length; i++) {
				if(treeNames[i] != '' && treeNames[i] == name){
					flag = true;
					break;
				}
			}
			if(flag){
				toastr.warning("类别标志已存在!");
				$("#name").val("");
				$("#name").focus();
			}
		}
	}
})
//保存
function save(){
	var parentId = $("#parentId").val();
	var typecode = $("#typecode").val();
	var typename = $("#typename").val();
	var code = $("#code").val();
	var name = $("#name").val();
	//分类
	if(parentId == 0){
		if(typecode == ''){
			toastr.warning("类型代码不能为空!");
			$("#typecode").focus();
		}else{
			if(typename == ''){
				toastr.warning("类型名称不能为空!");
				$("#typename").focus();
			}else{
				if($("#id").val() == ''){
					addMethod();
				}else{
					updateMethod();
				}
			}
		}
	}else{
		if(code == ''){
			toastr.warning("代码不能为空!");
			$("#code").focus();
		}else{
			if(name == ''){
				toastr.warning("名称不能为空!");
				$("#name").focus();
			}else{
				if($("#id").val() == ''){
					addMethod();
				}else{
					updateMethod();
				}
			}
		}
	}
}

//封装添加操作
function  addMethod(){
	$.ajax({
		type:'post',
		url:path+'/dictionary/add',
		data:$("#form").serialize(),
		dataType:'json',
		success:function(data){
			if(data.code == 0){
				toastr.success("保存成功!");
				reloadTree();
				$("#id").val(data.msg);
			}else{
				toastr.error(data.msg);
			}
		}
	})
}
//封装修改操作
function  updateMethod(){
	$.ajax({
		type:'post',
		url:path+'/dictionary/update',
		data:$("#form").serialize(),
		dataType:'json',
		success:function(data){
			if(data.code == 0){
				toastr.success("保存成功!");
				$("#newName").val($("#typename").val());
				reloadTree();
			}else{
				toastr.error(data.msg);
			}
		}
	})
}
//删除
function del(){
	var childNode = $("#childNode").val();
	if(childNode == ''){
		var id = $("#id").val();
		if (id != '' && id != 0) {
			Common.confirm({
				title: "提示",
				message: "请确认是否删除该对象?",
				operate: function (reselt) {
					if (reselt) {
						$.ajax({
							type: 'post',
							url: path + '/dictionary/delete',
							data: {id: id},
							dataType: 'json',
							success: function (data) {
								//错误编号500，表示有重复
								if (data.code == 0) {
									toastr.success("删除成功！");
								} else {
									toastr.error("删除失败！");
								}
							}
						})
						setTimeout(function () {
							window.location.reload();
						}, 1000);
					}
				}
			})
		} else {
			toastr.warning("未选中，删除无效!");
		}
	}else{
		toastr.warning("存在子节点,先从子节点开始删!");
	}
}
//树全部展开
function expandAll(){
	var treeObj=$.fn.zTree.getZTreeObj("ztree");
	treeObj.expandAll(true);
}
//树全部关闭
function collapseAll(){
	var treeObj=$.fn.zTree.getZTreeObj("ztree");
	treeObj.expandAll(false);
}