/**
 * create by robot 
 * date Sun Sep 29 17:36:35 CST 2019 
 */ 

$(document).ready(function(){
	//form表单样式
	layui.use(['layer', 'form'], function() {
		var layer = layui.layer,
			form = layui.form;
		form.render();
	});
	//li 切换事件
	$(".nav-tabs>li").click(function() {
		var index = $(".nav-tabs>li").index(this);
		$(this).addClass("active").siblings("li").removeClass("active");
		
	})
	//弹出一个提示层
	$('.fa-save').on('click', function() {
		saveAgreement();
	});

	//绑定标签栏事件
	$(document).on('click','.nav-tabs li',function(){

		var action =$(this).data('action');
		 switch (action) {
			case "base"://基本资料
				goBase();
				break;  
			case "functions"://多功能
				goFunctions();
				break;
			case "extends"://扩展    
				goExtends();         
				break;           
		 }
	})

	//加载第一个标签
	$('.nav-tabs li').eq(0).click();
	
});


//跳转基本信息 
function goBase(){
	var url=ctx+'/agreement/queryAgreementBaseInfo?id='+$('#agreementId').val();
	$('.tab-content').load(url);
	              
}
//跳转多功能输入
function goFunctions(){ 
	var url=ctx+'/agreement/queryAgreementFunctionsInfo?id='+$('#agreementId').val();
	$('.tab-content').load(url);  
	
}
//跳转到扩展   
function goExtends(){
	
	var url=ctx+'/agreement/queryAgreementExtendsInfo?id='+$('#agreementId').val();
	$('.tab-content').load(url);          
}  
  

 
//保存
function saveAgreement(){
	var oldDiscern=$("#oldDiscern").val();
	
	var id=$("#id").val();
	var discern=$("#discern option:selected").val();
	if(StringUtil.isBlank(discern)){
		layer.msg('请选择协议');
		return;
	}
	var name=$("#discern option:selected").text();
	var content=UM.getEditor('content').getContent().trim();
	if(StringUtil.isBlank(content)){
		layer.msg('请输入内容');
		return;
	}
	if(discern=='2'){
		content = content.replace(/(\n)/g, "");    
		content = content.replace(/(\t)/g, "");    
		content = content.replace(/(\r)/g, "");    
		content = content.replace(/<\/?[^>]*>/g, "");    
		content = content.replace(/\s*/g, ""); 
	}
	$(".fa-save").attr("disabled","disabled");
	$('.fa-save').html('正在保存...')
	$.ajax({
		url:ctx+"/agreement/saveAgreement",
		type:"POST",
		dataType:'json',
		data:{
			"id":id,
			"discern":discern,
			"name":name,
			"content":content,
			"oldDiscern":oldDiscern,
		},
		complete:function(){
			$(".fa-save").removeAttr("disabled");
			$('.fa-save').html('保存')
		},        
		success:function(service){ 
		    if(service.code=='0'){
		    	layer.msg('已拥有该规则，无法重复添加！');
		    }else{
		    	layer.confirm('保存成功,是否前往列表？', {
					  btn: ['确认','取消'] //按钮
					}, function(){
						$('#goback span').trigger('click'); 
					}, function(){         
						layer.closeAll();
				});
		    }
		},
		error:function(res){
			layer.msg('系统繁忙，请稍后再试');
		}
		
	});  
}
 

