//树操作
var zNodes;
var menu = {
	setting: {
		view:{
			fontCss : {"font-family": "微软雅黑","color":"blue"}
		},
		check: {
			enable: true,
			chkStyle: "radio",
			radioType: "level"
		},
		data:{
			key:{
				name:"typeName"
			},
			simpleData: {
				enable: true,
				idKey: "id",
				pIdKey: "parentId",
			},
		},
		callback: { //回调函数
			onClick: zTreeOnClick,
			onCheck:zTreeOnCheck
		}
	},
	loadMenuTree:function(){
		$.ajax({
			type: "post",
			url:path+"/printing/getEmrTypeTree",
			dataType:"json",
			success:function(data){
				$.fn.zTree.init($("#ztree"), menu.setting, data);
				zTree = $.fn.zTree.getZTreeObj("ztree");
				var nodes = zTree.getNodes();
				nodes[0].nocheck=true;
				for (var i = 0; i < nodes.length; i++) { //设置节点展开
					zTree.expandNode(nodes[i], true, false, true);
				}
				zNodes = data;
			}
		})
	}
};
//加载树
$().ready(function(data){
	menu.loadMenuTree();
	fuzzySearch('ztree','#key',null,true,menu.setting,zNodes); //初始化模糊搜索方法
});

//树选中事件
function zTreeOnCheck(event, treeId, treeNode) {
	var treeObj=$.fn.zTree.getZTreeObj("ztree");
	var nodes=treeObj.getCheckedNodes(true);
	if(nodes.length > 0){
		if(treeNode.level != 0){
			//变更选中
			$.ajax({
				type:'post',
				url:path+'/printing/udpateCheckByEmrType/'+treeNode.id,
				dataType:'json',
				success:function(data){
					if(data.code == 0){
						toastr.success("选中选项变更成功!");
						$("#iframe").prop("src",path+"/printing/printManageIframe?id="+treeNode.id);
					}else{
						toastr.error(data.msg);
					}
				}
			})
		}else{
			toastr.warning("请选中下级！");
		}
	}else{
		zTree.checkNode(treeNode, true, true);
		toastr.warning("取消失败，至少选中一个!")
	}
};
//树点击事件
function zTreeOnClick(event, treeId, treeNode) {
	$("#currentId").val(treeNode.id);
	if(treeNode.level != 0){
		$("#iframe").prop("src",path+"/printing/printManageIframe?id="+treeNode.id);
	}
};
//添加类别按钮方法
function addEmrType(){
	setFormToken();
	var currentId = $("#currentId").val();
	if(currentId == '' || currentId != 0){
		toastr.warning("请选中根节点添加!");
		$("#addModle").removeAttr("data-toggle");
		$("#addModle").removeAttr("data-target");
	}else{
		$("#addModle").attr("data-toggle","modal");
		$("#addModle").attr("data-target","#myModal1");
	}
	$("#addForm")[0].reset();
}
//验证类别标识是否重复
$("#typeFlag").blur(function(){
	var typeFlag = $("#typeFlag").val();
	if(typeFlag != ''){
		$.ajax({
			type:'get',
			url:path+'/printing/checkTypeFlagByCreater',
			data:{typeFlag:typeFlag},
			dataType:'json',
			success:function(data){
				if(data.code != 0){
					$("#typeFlag").val('');
					$("#typeFlag").focus();
					toastr.warning(data.msg);
				}
			}
		})
	}
})
//验证类别名称是否重复
$("#typeName2").blur(function(){
	var typeName = $("#typeName2").val();
	if(typeName != ''){
		$.ajax({
			type:'get',
			url:path+'/printing/checkTypeNameByCreater',
			data:{typeName:typeName},
			dataType:'json',
			success:function(data){
				if(data.code != 0){
					$("#typeName2").val('');
					$("#typeName2").focus();
					toastr.warning(data.msg);
				}
			}
		})
	}
})
//添加模板
function add(){
	var typeFlag = $("#typeFlag").val();
	var typeName = $("#typeName2").val();
	if(typeFlag != ''){
		if(typeName != ''){
			var flag = false;
			var newModleId = '';
			$.ajax({
				type:'post',
				url:path+"/printing/addEmrType",
				data:$("#addForm").serialize(),
				dataType:'json',
				success:function(data){
					if(null != data && data.code == 0){
						toastr.success("添加成功！");
						flag = true;
						newModleId = data.data;
						//隐藏模态框
						$('#myModal1').modal('hide')
						//刷新树
						menu.loadMenuTree();
					}else{
						toastr.warning(data.msg);
					}
				}
			})
			setTimeout(function(){
				if(flag){
					//选中最新节点
					var zTree = $.fn.zTree.getZTreeObj("ztree");
					var node = zTree.getNodeByParam("id",newModleId);
					zTree.selectNode(node);
					$("#iframe").prop("src",path+"/printing/printManageIframe?id="+newModleId);
				}
			},1000);
		}else{
			toastr.warning("类别名称不能为空！");
		}
	}else{
		toastr.warning("类别标志不能为空！");
	}
}
