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'),
		toLink: medtapCore.getRequest('toLink'),
		toBBS: medtapCore.getRequest('toBBS'),
		BBSurl: 'https://gateway.medtap.cn/wechat/lung/oauth_stepEnd?type=BBS_LUNG_SHARE',
		cardId: medtapCore.getRequest('cardId'),
		healthType:'',
		init: function() {

		},
		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.toast({
							message: '请输入手机号',
							time: 1500
						});
						return;
					}
					if (!/^1[0-9]{10}$/.test($('#mobile').val())) {
						medtapCore.toast({
							message: '请输入合法的手机号',
							time: 1500
						});
						return;
					}
					if (!$('#smsCode').val()) {
						medtapCore.toast({
							message: '请输入验证码',
							time: 1500
						});
						return;
					}
					//兼容历史版本使用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调整
								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);
								if (!login.toLink) {
									if(login.toBBS){
										window.location.replace('https://review-formal.iplusmed.com/wechatForLungCancer/prd/medtap_bbs_lung/index.html?wechatId=' + login.wechatId)
									}else if(login.cardId){
										window.location.replace(login.BBSurl+'&cardId=' + login.cardId)
									}else{
										setTimeout(function() {
											self.location = document.referrer;
										}, 1000)
									}

								} else {
									login.getUserInfo();
								}
							} else {
								medtapCore.toast({
									message: data.resultDesc,
									time: 1500
								});
							}
						},
						error: function(err) {
							console.log('err: ' + err);
						}
					});

				}
			});
			$('.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');
				}
			}
		},
		getUserInfo: function() {
			medtapCore.loading(1);
			medtapCore.submitAjax({
				url: 'https://gateway.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('pages/basicUserInfo.html?wechatId=' + login.wechatId + '&doctorId=' + login.doctorId +
								'&operationType=12&toLink=' + login.toLink);
						} else {
							//login.getUserPatientInfo();
							if (info.hasOwnProperty('healthType')) {
								login.healthType = info.healthType.child;
								login.getUserPatientInfo();
							}
						}
					}
				}
			})
		},
		getUserPatientInfo: function() {
			medtapCore.loading(1);
			medtapCore.submitAjax({
				url: 'https://gateway.medtap.cn/user/patientEMR/listPatientEMR',
				type: 'POST',
				async: false,
				contentType: 'application/json',
				data: {
					pageNum: 1,
					pageSize: 10,
					condition: {

					}
				},
				success: function(res) {
					medtapCore.loading(0);
					if (res.success == true) {
						var patientInfo = res.content.list;
						if (patientInfo.length == 0) {
							//未完善患者信息
							/* window.location.replace('pages/createPatientInfo.html?wechatId=' + login.wechatId + '&doctorId=' + login.doctorId +
								'&operationType=12&toLink=' + login.toLink); */
							if (login.healthType == 'HE1') {
								switch (login.toLink) {
									case 'MYDOCTOR':
										window.location.replace(
											'https://review-formal.iplusmed.com/wechatForLungCancer/prd/wechat_lung_myHome/pages/myDoctor.html?wechatId=' +
											login.wechatId)
										break;
									case 'MYEMR':
										window.location.replace(
											'https://review-formal.iplusmed.com/wechatForLungCancer/prd/wechat_lung_myHome/pages/myEMR.html?wechatId=' +
											login.wechatId);
										break;
									case 'MYREMIND':
										window.location.replace(
											'https://review-formal.iplusmed.com/wechatForLungCancer/prd/wechat_lung_myHome/pages/myRemind.html?wechatId=' +
											login.wechatId);
										break;
									case 'MYCONSULT':
										window.location.replace(
											'https://review-formal.iplusmed.com/wechatForLungCancer/prd/wechat_lung_myHome/pages/myConsult.html?wechatId=' +
											login.wechatId);
									default:
										break;
								}
							} else {
								window.location.replace('pages/createPatientInfo.html?wechatId=' + login.wechatId + '&doctorId=' + login
									.doctorId +
									'&operationType=12&toLink=' + login.toLink);
							}
						} else {
							//跳转至医生主页
							switch (login.toLink) {
								case 'MYDOCTOR':
									window.location.replace(
										'https://review-formal.iplusmed.com/wechatForLungCancer/prd/wechat_lung_myHome/pages/myDoctor.html?wechatId=' +
										login.wechatId)
									break;
								case 'MYEMR':
									window.location.replace(
										'https://review-formal.iplusmed.com/wechatForLungCancer/prd/wechat_lung_myHome/pages/myEMR.html?wechatId=' +
										login.wechatId);
									break;
								case 'MYREMIND':
									window.location.replace(
										'https://review-formal.iplusmed.com/wechatForLungCancer/prd/wechat_lung_myHome/pages/myRemind.html?wechatId=' +
										login.wechatId);
									break;
								case 'MYCONSULT':
									window.location.replace(
										'https://review-formal.iplusmed.com/wechatForLungCancer/prd/wechat_lung_myHome/pages/myConsult.html?wechatId=' +
										login.wechatId);
								default:
									break;
							}
						}
					}
				}
			})
		}
	};

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