define(['zepto', 'medtap'], function($, medtapCore) {
	var clock = null,
		nums = 60;

	var login = {
		wechatId: medtapCore.getRequest('wechatId'),
		doctorId:medtapCore.getRequest('doctorId'),
		inviteCode:medtapCore.getRequest('inviteCode'),
		agencyId:medtapCore.getRequest('agencyId'),
		operationType:medtapCore.getRequest('operationType'),
		businessDetail:medtapCore.getRequest('businessDetail'),
		channelId:medtapCore.getRequest('channelId'),
		init: function() {
			/* $('input').on('change',function(){
				if(!$('#mobile').val() || $('#mobile').val().length !== 11){
					$('#login').removeClass('loginOn')
				}else if(!$('#smsCode').val() || $('#smsCode').val().length !== 4){
					$('#login').removeClass('loginOn')
				}else{
					$('#login').addClass('loginOn');
				}
			}) */
		},
		bind: function() {
			$('#getSms').click(function() {
				if(!/^1[0-9]{10}$/.test($('#mobile').val())) {
					medtapCore.toast({
						message:'请输入合法的手机号',
						time:1500
					});
					return;
				}else{
					$('#__nc').show();
					$('.smsCode').hide();
				}
			});

			$('#login').on('click',function() {
				if($(this).hasClass('loginOn')){
					if(!$('#mobile').val()) {
						//medtapCore.winPop('请输入手机号');
						medtapCore.toast({
							message:'请输入手机号',
							time:1500
						});
						return;
					}
					if(!/^1[0-9]{10}$/.test($('#mobile').val())) {
						//medtapCore.winPop('请输入合法的手机号');
						medtapCore.toast({
							message:'请输入合法的手机号',
							time:1500
						});
						return;
					}
					if(!$('#smsCode').val()) {
						//medtapCore.winPop('请输入验证码');
						medtapCore.toast({
							message:'请输入验证码',
							time:1500
						});
						return;
					}
					if(login.agencyId != ''){ //扫描代理商二维码进来的用户
						$.ajax({
							url: 'https://testdevgw.medtap.cn/user/loginUserByWechat',
							type: 'post',
							async: true,
							dataType: 'json',
							headers: {
								'apptype': 'wechat_lung',
								'user-os':'wechat'
							},
							contentType: "application/json",
							data: JSON.stringify({
								mobile: $('#mobile').val(),
								smsCode: $('#smsCode').val(),
								wechatId: login.wechatId,
								extra:{  //登录附加参数，用于甄别用户渠道来源2019.03调整
									agencyId:login.agencyId,
									operationType:login.operationType,
									businessDetail:login.businessDetail
								}
							}),
							success: function(data) {
								if(data.success) {
									//medtapCore.winPop('登录/注册成功!');
									medtapCore.toast({
										message:'登录/注册成功!',
										time:1500
									});
									var token = data.content.token;
									localStorage.setItem('wxLungToken',token);
									login.getUserDetail();
								} else {
									medtapCore.winPop(data.resultDesc);
									
								}
							},
							error: function(err) {
								console.log('err: ' + err);
							}
						});
					}else if(login.inviteCode != ''){ //兼容历史版本使用inviteCode做业务的使用场景
						$.ajax({
							url: 'https://testdevgw.medtap.cn/user/loginUserByWechat',
							type: 'post',
							async: true,
							dataType: 'json',
							headers: {
								'apptype': 'wechat_lung',
								'user-os':'wechat'
							},
							contentType: "application/json",
							data: JSON.stringify({
								mobile: $('#mobile').val(),
								smsCode: $('#smsCode').val(),
								wechatId: login.wechatId,
								extra:{  //登录附加参数，用于甄别用户渠道来源2019.03调整
									inviteCode:login.inviteCode,
									operationType:login.operationType,
									businessDetail:login.businessDetail
								}
							}),
							success: function(data) {
								if(data.success) {
									medtapCore.toast({
										message:'登录/注册成功!',
										time:1500
									});
									var token = data.content.token;
									localStorage.setItem('wxLungToken',token);
									login.getUserDetail();
								} else {
									//medtapCore.winPop(data.resultDesc);
									medtapCore.toast({
										message:data.resultDesc,
										time:1500
									});
									
								}
							},
							error: function(err) {
								console.log('err: ' + err);
							}
						});
					}else if(login.doctorId != ''){ //兼容历史版本使用doctorId做业务的使用场景
						$.ajax({
							url: 'https://testdevgw.medtap.cn/user/loginUserByWechat',
							type: 'post',
							async: true,
							dataType: 'json',
							headers: {
								'apptype': 'wechat_lung',
								'user-os':'wechat'
							},
							contentType: "application/json",
							data: JSON.stringify({
								mobile: $('#mobile').val(),
								smsCode: $('#smsCode').val(),
								wechatId: login.wechatId,
								extra:{  //登录附加参数，用于甄别用户渠道来源2019.03调整
									doctorId:login.doctorId,
									operationType:login.operationType,
									businessDetail:login.businessDetail
								}
							}),
							success: function(data) {
								if(data.success) {
									medtapCore.toast({
										message:'登录/注册成功!',
										time:1500
									});
									var token = data.content.token;
									localStorage.setItem('wxLungToken',token);
									login.getUserDetail();
								} else {
									medtapCore.toast({
										message:data.resultDesc,
										time:1500
									});
									
								}
							},
							error: function(err) {
								console.log('err: ' + err);
							}
						});
					}else{
						$.ajax({
							url: 'https://testdevgw.medtap.cn/user/loginUserByWechat',
							type: 'post',
							async: true,
							dataType: 'json',
							headers: {
								'apptype': 'wechat_lung',
								'user-os':'wechat'
							},
							contentType: "application/json",
							data: JSON.stringify({
								mobile: $('#mobile').val(),
								smsCode: $('#smsCode').val(),
								wechatId: login.wechatId,
								extra:{  //登录附加参数，用于甄别用户渠道来源2019.03调整
									operationType:login.operationType,
									channelId:login.channelId
								}
							}),
							success: function(data) {
								if(data.success) {
									medtapCore.toast({
										message:'登录/注册成功!',
										time:1500
									});
									var token = data.content.token;
									localStorage.setItem('wxLungToken',token);
									login.getUserDetail();
								} else {
									medtapCore.toast({
										message:data.resultDesc,
										time:1500
									});
									
								}
							},
							error: function(err) {
								console.log('err: ' + err);
							}
						});
					}
				}else{
					
				}
			});
			$('.userLicens').click(function() {
				
			});
		},
		sendCode: function(thisBtn) {
			btn = thisBtn;
			btn.disabled = true;
			btn.innerHTML = nums + 'S';
			clock = setInterval(doLoop, 1000);
			$(thisBtn).addClass('bg');

			function doLoop() {
				nums--;
				if(nums > 0) {
					btn.innerHTML = nums + 'S';
				} else {
					clearInterval(clock); //清除js定时器
					btn.disabled = false;
					btn.innerHTML = '获取验证码';
					nums = 60; //重置时间
					$(thisBtn).removeClass('bg');
				}
			}
		},
		getUserDetail:function(){
			medtapCore.loading(1);
			medtapCore.submitAjax({
				url:'https://testdevgw.medtap.cn/user/getUserDetail',
				type:'GET',
				async:false,
				data:{
					
				},
				success:function(res){
					medtapCore.loading(0);
					if(res.success == true){
						var info = res.content.userInfo;
						if(!info.hasOwnProperty('healthType') ||!info['username'] || !info['sex'] || !info['telephone']){
							//用户信息未完善
							window.location.replace('basicUserInfo.html?wechatId=' + login.wechatId + '&doctorId=' + login.doctorId)
						}else{
							//判断患者信息完善情况
							login.getUserPatientInfo();
						}
					}
				}
				
			})
		},
		getUserPatientInfo:function(){
			medtap.loading(1);
			medtap.submitAjax({
				url:'https://testdevgw.medtap.cn/user/patientEMR/listPatientEMR',
				type:'POST',
				async:false,
				contentType: 'application/json',
				data:{
					pageNum:1,
					pageSize:10,
					condition:{
						
					}
				},
				success:function(res){
					medtap.loading(0);
					if(res.success == true){
						var patientInfo = res.content.list;
						if(patientInfo.length == 0){
							//未完善患者信息
							window.location.replace('createPatientInfo.html?wechatId=' + login.wechatId + '&doctorId=' + login.doctorId);
						}else{
							//跳转至医生主页
							window.location.replace('doctorHomePage.html?wechatId=' + login.wechatId + '&doctorId=' + login.doctorId);
						}
					}
				}
			})
		}
	};

	login.init();
	login.bind();
});