//树操作
var zNodes;
var menu = {
	setting: {
		view:{
			fontCss : {"font-family": "微软雅黑","color":"blue"}
		},
		check: {
			enable: true,
			chkStyle: "radio",
			radioType: "level"
		},
		data:{
			key:{
				name:"modleName"
			},
			simpleData: {
				enable: true,
				idKey: "id",
				pIdKey: "parentId",
			},
		},
		callback: { //回调函数
			onClick: zTreeOnClick,
			onCheck:zTreeOnCheck
		}
	},
	loadMenuTree:function(){
		$.ajax({
			type: "post",
			url:path+"/template/getTemplateSelectTree",
			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+'/template/udpateCheckByModle/'+treeNode.id,
				dataType:'json',
				success:function(data){
					if(data.code == 0){
						toastr.success("默认模板变更成功!");
						$("#iframe").prop("src",path+"/template/templateSelectIframe?id="+treeNode.id);
					}else{
						toastr.error(data.msg)
					}
				}
			})
		}else{
			toastr.warning("请选中子级模板！");
		}
	}else{
		zTree.checkNode(treeNode, true, true);
		toastr.warning("取消失败，至少选中一个!")
	}
}
//树点击事件
function zTreeOnClick(event, treeId, treeNode) {
	$("#currentTypeId").val(treeNode.id);
	if(treeNode.level != 0){
		$("#iframe").prop("src",path+"/template/templateSelectIframe?id="+treeNode.id);
	}
};
//添加模板按钮方法
function addModle(){
	setFormToken();
	var currentTypeId = $("#currentTypeId").val();
	if(currentTypeId == '' || currentTypeId != -1){
		toastr.warning("请选中根节点添加!");
		$("#addModle").removeAttr("data-toggle");
		$("#addModle").removeAttr("data-target");
	}else{
		$("#addModle").attr("data-toggle","modal");
		$("#addModle").attr("data-target","#myModal1");
	}
	$("#addForm")[0].reset();
}

//bootstap时间插件
$('.datepicker').datepicker({
	startDate:new Date(),
	format: 'yyyy-mm-dd',
	autoclose: true
})
//验证模板标识不能重复
$("#modleFlagCheck").blur(function(){
	var modleFlag = $("#modleFlagCheck").val();
	if(modleFlag != ''){
		$.ajax({
			type:'post',
			url:path+'/template/checkModleFlag',
			data:{modleFlag:modleFlag},
			dataType:'json',
			success:function(data){
				if(data.code != 0){
					$("#modleFlagCheck").val("");
					$("#modleFlagCheck").focus();
					toastr.warning(data.msg);
				}
			}
		})
	}
})
//验证模板名称不能重复
$("#modleNameCheck").blur(function(){
	var modleName = $("#modleNameCheck").val();
	if(modleName != ''){
		$.ajax({
			type:'post',
			url:path+'/template/checkModleName',
			data:{modleName:modleName},
			dataType:'json',
			success:function(data){
				if(data.code != 0){
					$("#modleNameCheck").val("");
					$("#modleNameCheck").focus();
					toastr.warning(data.msg);
				}
			}
		})
	}
})
//添加模板
function add(){
	var modleFlag = $("#modleFlagCheck").val();
	var modleName = $("#modleNameCheck").val();
	if(modleFlag != ''){
		if(modleName != ''){
			var flag = false;
			var newModleId = '';
			$.ajax({
				type:'post',
				url:path+"/template/addTemplateSelect",
				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+"/template/templateSelectIframe?id="+newModleId);
				}
			},1000);
		}else{
			toastr.warning("模板名称不能为空！");
			$("#modleNameCheck").focus();
		}
	}else{
		toastr.warning("模板标识不能为空！");
		$("#modleFlagCheck").focus();
	}
}