/**
 * create by robot 
 * date Thu Sep 12 16:16:36 CST 2019 
 */ 

$(document).ready(function(){
	//form表单样式
	layui.use(['layer', 'form'], function() {
		var layer = layui.layer,
			form = layui.form;
		form.render();
	});
	
	//日历控件
	layui.use(['layer', 'laydate'], function() {
		var layer = layui.layer;
		laydate = layui.laydate;
		laydate.render({
		elem: '#date1' //指定元素
		});
		laydate.render({
			elem: '#date2' //指定元素
		});
	});
	
	//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() {
		saveActivityRecharge();
	});

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

 
//保存
function saveActivityRecharge(){
	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 rechargeMoney=$("#rechargeMoney").val();
	if(StringUtil.isNotJustIntgerAndDecimals2(rechargeMoney)){
		if(StringUtil.isBlank(rechargeMoney)){
			layer.msg('请输入充值金额');
			return;
		}
		layer.msg('请输入正确的充值金额(正整数或小数(保留两位小数))');
		return;
	}
	
	if(StringUtil.isNotBlank(rechargeMoney)){
		if(rechargeMoney<0.01){
			layer.msg('充值金额必须大于0.01');
			return;
		}
	}
	
	var giveMoney=$("#giveMoney").val();
	if(StringUtil.isNotJustIntgerAndDecimals2(giveMoney)){
		if(StringUtil.isBlank(giveMoney)){
			layer.msg('请输入赠送金额');
			return;
		}
		layer.msg('请输入正确的赠送金额(正整数或小数(保留两位小数))');
		return;
	}
	
	if(StringUtil.isNotBlank(giveMoney)){
		if(parseInt(giveMoney)>parseInt(rechargeMoney)){
			layer.msg('赠送金额必须小于充值金额');
			return;
		}
	}
	
	if(StringUtil.isNotBlank(giveMoney)){
		if(giveMoney<0.01){
			layer.msg('赠送金额必须大于0.01');
			return;
		}
	}
	var timeType=$("#timeType option:selected").val();
	var date1=$("#date1").val();
	var date2=$("#date2").val();
	if(timeType=='1'){
		if(StringUtil.isBlank(date1)){
			layer.msg('请输入开始时间');
			return;
		}
		if(StringUtil.isBlank(date2)){
			layer.msg('请输入结束时间');
			return;
		}
	}
	
	 
	$(".fa-save").attr("disabled","disabled");
	$('.fa-save').html('正在保存...')
	$.ajax({
		url:ctx+"/activityRecharge/saveActivityRecharge",
		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");
				}
			})
		})
	});
	
	layui.use(['layer', 'form'], function() {
		var layer = layui.layer,
			form = layui.form;
		form.render();
		form.on('select(selectTimeType)', function(data){
			var timeType=data.value;
			if(timeType=='1'){
				 $("#dateDetail").show();
			}else{
				$("#date1").val('');
				$("#date2").val('');
				$("#dateDetail").hide();
			}
		})
	});
});

