/**
 * create by robot 
 * date Tue Sep 24 14:55:39 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() {
		saveActivityRecommend();
	});

	//绑定标签栏事件
	$(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+'/activityRecommend/queryActivityRecommendBaseInfo?id='+$('#activityRecommendId').val();
	$('.tab-content').load(url);
	              
}
//跳转多功能输入
function goFunctions(){ 
	var url=ctx+'/activityRecommend/queryActivityRecommendFunctionsInfo?id='+$('#activityRecommendId').val();
	$('.tab-content').load(url);  
	
}
//跳转到扩展   
function goExtends(){
	
	var url=ctx+'/activityRecommend/queryActivityRecommendExtendsInfo?id='+$('#activityRecommendId').val();
	$('.tab-content').load(url);          
}  
  

 
//保存
function saveActivityRecommend(){
	var isAdmin=$("#isAdmin").val();
	if(isAdmin=='1'){
		var merchantCode=$("#merchantCode option:selected").val();
		if(StringUtil.isBlank(merchantCode)){
			layer.msg('请选择商户');
			return;
		}
	}
	var storeId=$("#storeId option:selected").val();
	if(StringUtil.isBlank(storeId)){
		layer.msg('请选择门店');
		return;
	}
	
	var name=$("#name").val();
	var firstMixRechargeMoney=$("#firstMixRechargeMoney").val();
	var recommendMoney=$("#recommendMoney").val();
	if(StringUtil.isBlank(name)){
		layer.msg('请输入活动名称');
		return;
	}
	
	if(StringUtil.isNotJustIntgerAndDecimals2(firstMixRechargeMoney)){
		if(StringUtil.isBlank(firstMixRechargeMoney)){
			layer.msg('请输入首充最低充值金额');
			return;
		}
		layer.msg('请输入正确的首充最低充值金额(正整数或小数(保留两位小数))');
		return;
	}
	
	if(StringUtil.isNotJustIntgerAndDecimals2(recommendMoney)){
		if(StringUtil.isBlank(recommendMoney)){
			layer.msg('请输入推荐人与被推荐人奖励金额');
			return;
		}
		layer.msg('请输入正确的推荐人与被推荐人奖励金额(正整数或小数(保留两位小数))');
		return;
	}
	$(".fa-save").attr("disabled","disabled");
	$('.fa-save').html('正在保存...')
	$.ajax({
		url:ctx+"/activityRecommend/saveActivityRecommend",
		type:"POST",
		dataType:'json',
		data:$("#data_form").serializeArray(),
		complete:function(){
			$(".fa-save").removeAttr("disabled");
			$('.fa-save').html('保存')
		},        
		success:function(service){ 
		
			layer.confirm('保存成功,是否前往列表？', {
				  btn: ['确认','取消'] //按钮
				}, function(){
					$('#goback span').trigger('click'); 
				}, function(){         
					layer.closeAll();
			});  
			
		}
	});  
}

//onchange商户
$(function(){
	layui.use(['layer', 'form'], function() {
		var layer = layui.layer,
			form = layui.form;
		form.render();
		form.on('select(selectMerchantCode)', function(data){
			var merchantCode=data.value;
			$.ajax({
				url:ctx+'/merchantMenber/getStoreByMerchantCode',
				type:'post',
				dataType:'json',
				data:{
					'merchantCode':merchantCode,
				},
				success:function(result){
					var storeList=result.merchantStoreList;
					$("#storeId").empty();
					var data='';
					data+='<option value="">请选择</option>';
					for (var i = 0; i < storeList.length; i++) {
						data+='<option value='+storeList[i].storeId+'>'+storeList[i].storeName+'</option>'
					}
					$("#storeId").append(data);
					form.render("select");
				}
			})
		})
	});
});
 

