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

	var login = {
		wechatId: medtapCore.getRequest('wechatId'),
		isClose: medtapCore.getRequest('isClose'),
		redirect_uri: medtapCore.getRequest('redirect_uri'),
		unionId: medtapCore.getRequest('unionId'),
		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() {
			var noticeText = medtapCore.getRequest('notice');
			if(!!noticeText) {
				$('.notice-text').text(noticeText);
				$('.notice').css('opacity', 1);
			}
		},
		bind: function() {
			$('#getSms').click(function() {
				if(!/^1[0-9]{10}$/.test($('#mobile').val())) {
					medtapCore.winPop('请输入合法的手机号');
					return;
				}else{
					$('#__nc').show();
					$('.icon-smsCode').hide();
				}
			});

			$('.icon-close').click(function() {
				$('.notice').css('opacity', 0);
			});

			$('#login').on('click',function() {
				if(!$('#mobile').val()) {
					medtapCore.winPop('请输入手机号');
					return;
				}
				if(!/^1[0-9]{10}$/.test($('#mobile').val())) {
					medtapCore.winPop('请输入合法的手机号');
					return;
				}
				if(!$('#smsCode').val()) {
					medtapCore.winPop('请输入验证码');
					return;
				}
				if(login.agencyId != ''){ //扫描代理商二维码进来的用户
					$.ajax({
						url: 'https://gateway.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('登录/注册成功!');
								var token = data.content.token;
								localStorage.setItem('wxLungToken',token);
								setTimeout(function(){
									window.parent._beforeSubmitSuccess && window.parent._beforeSubmitSuccess();
									window.parent._beforeSubmitClose && window.parent._beforeSubmitClose();
								},1000)
							} else {
								medtapCore.winPop(data.resultDesc);
								window.parent._beforeSubmitError && window.parent._beforeSubmitError();
							}
						},
						error: function(err) {
							console.log('err: ' + err);
						}
					});
				}else if(login.inviteCode != ''){ //兼容历史版本使用inviteCode做业务的使用场景
					$.ajax({
						url: 'https://gateway.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.winPop('登录/注册成功!');
								var token = data.content.token;
								localStorage.setItem('wxLungToken',token);
								setTimeout(function(){
									window.parent._beforeSubmitSuccess && window.parent._beforeSubmitSuccess();
									window.parent._beforeSubmitClose && window.parent._beforeSubmitClose();
								},1000)
							} else {
								medtapCore.winPop(data.resultDesc);
								window.parent._beforeSubmitError && window.parent._beforeSubmitError();
							}
						},
						error: function(err) {
							console.log('err: ' + err);
						}
					});
				}else if(login.doctorId != ''){ //兼容历史版本使用doctorId做业务的使用场景
					$.ajax({
						url: 'https://gateway.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.winPop('登录/注册成功!');
								var token = data.content.token;
								localStorage.setItem('wxLungToken',token);
								setTimeout(function(){
									window.parent._beforeSubmitSuccess && window.parent._beforeSubmitSuccess();
									window.parent._beforeSubmitClose && window.parent._beforeSubmitClose();
								},1000)
							} else {
								medtapCore.winPop(data.resultDesc);
								window.parent._beforeSubmitError && window.parent._beforeSubmitError();
							}
						},
						error: function(err) {
							console.log('err: ' + err);
						}
					});
				}else{
					$.ajax({
						url: 'https://gateway.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.winPop('登录/注册成功!');
								var token = data.content.token;
								localStorage.setItem('wxLungToken',token);
								setTimeout(function(){
									window.parent._beforeSubmitSuccess && window.parent._beforeSubmitSuccess();
									window.parent._beforeSubmitClose && window.parent._beforeSubmitClose();
								},1000)
							} else {
								medtapCore.winPop(data.resultDesc);
								window.parent._beforeSubmitError && window.parent._beforeSubmitError();
							}
						},
						error: function(err) {
							console.log('err: ' + err);
						}
					});
				}
			});
			$('.userLicens').click(function() {
				window.parent.document.getElementById('_beforeSubmitIframe').setAttribute('src', 'https://review-formal.iplusmed.com/wechatForLungCancer/prd/wechat_lung_login/userLicens.html?wechatId=' + login.wechatId + '&agencyId=' + login.agencyId + '&inviteCode=' + login.inviteCode + '&doctorId=' + login.doctorId + '&operationType=' + login.operationType + '&businessDetail=' + login.businessDetail);
			});
		},
		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');
				}
			}
		}
	};

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