/**
 * create by robot 
 * date Thu Sep 12 16:16:37 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() {
		saveUserApp();
	});

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

 
//保存
function saveUserApp(){
	//alert(1);
	var isAdmin=$("#isAdmin").val();
	if(isAdmin=='1'){
		var merchantCode=$("#merchantCode").val();
		if(StringUtil.isBlank(merchantCode)){
			layer.msg('请选择商户');
			return;
		}
	}
	var storeId=$("#storeId").val();
	if(StringUtil.isBlank(storeId)){
		layer.msg('请选择门店');
		return;
	}
	var loginName=$("#loginName").val();
	if(StringUtil.isBlank(loginName)){
		layer.msg('请输入账号');
		return;
	}
	var userName=$("#userName").val();
	if(StringUtil.isBlank(userName)){
		layer.msg('请输入姓名');
		return;
	}
	var phone=$("#phone").val();
	if(StringUtil.isNotPhone(phone)){
		if(StringUtil.isBlank(phone)){
			layer.msg('请输入手机号');
			return;
		}
		layer.msg('请输入正确的手机号');
		return;
	}
	var password=$("#password").val();
	if(StringUtil.isBlank(password)){
		layer.msg('请输入密码');
		return;
	}
	var verifyPassword=$("#verifyPassword").val();
	if(StringUtil.isBlank(verifyPassword)){
		layer.msg('请再次输入密码');
		return;
	}
	if(password!=verifyPassword){
		layer.msg('两次密码不一致！');
		return;
	}
	$(".fa-save").attr("disabled","disabled");
	$('.fa-save').html('正在保存...')
	
	console.log($("#data_form").serializeArray());
	
	$.ajax({
		url:ctx+"/staff/saveStaff",
		type:"POST",
		dataType:'json',
		data:$("#data_form").serializeArray(),
		complete:function(){
			$(".fa-save").removeAttr("disabled");
			$('.fa-save').html('保存')
		},        
		success:function(service){ 
			if(service.code=='1'){
				layer.confirm('保存成功,是否前往列表？', {
					  btn: ['确认','取消'] //按钮
					}, function(){
						$('#goback span').trigger('click'); 
					}, function(){         
						layer.closeAll();
				}); 
			}else if(service.code=='0'){
				layer.msg(service.msg);
			}
		}
	});  
}

//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");
				}
			})
		})
	})
});
 

