define(['zepto', 'medtap'], function($, medtap) {
	var rukou = {
		wechatId: medtap.getRequest('wechatId'),
		linkType:medtap.getRequest('linkType'),
		healthType:"",
		init: function() {
			rukou.getToken();
		},
		checkUser: function() {
			medtap.loading(1);
			medtap.submitAjax({
				url: 'https://testdevgw.medtap.cn/wechat/user/getWechatUser',
				type: 'GET',
				async: false,
				data: {
					wechatId: rukou.wechatId
				},
				success: function(res) {
					medtap.loading(0);
					var info = res.content.wechatUser;
					if (info.hasOwnProperty('userId')) {
						
						rukou.getUserDetail();
					} else {
						window.location.replace(
							'https://review-formal.iplusmed.com/wechatForLungCancer/dev/wechat_lung_login/login.html?wechatId=' +
							rukou.wechatId + '&operationType=12&toLink=' + rukou.linkType);
					}
				}
			})
		},
		getToken: function() {
			localStorage.wxLungToken = '';
			medtap.loading(1);
			medtap.submitAjax({
				url: 'https://testdevgw.medtap.cn/user/auth/token',
				type: 'POST',
				async: false,
				contentType: 'application/json',
				data: {
					wechatId: rukou.wechatId
				},
				success: function(res) {
					medtap.loading(0);
					if (res.success == true) {
						var token = res.content.token;
						localStorage.setItem('wxLungToken', token);
						rukou.checkUser();
					} else {

					}
				},
				error: function(res) {
					console.log(res.resultDesc);
				}
			})
		},
		getUserDetail:function(){
			medtap.loading(1);
			medtap.submitAjax({
				url:'https://testdevgw.medtap.cn/user/getUserDetail',
				type:'GET',
				async:false,
				data:{
					
				},
				success:function(res){
					medtap.loading(0);
					if(res.success == true){
						var info = res.content.userInfo;
						if(!info.hasOwnProperty('healthType') ||!info['username'] || !info['sex'] || !info['telephone']){
							window.location.replace('https://review-formal.iplusmed.com/wechatForLungCancer/dev/wechat_lung_login/pages/basicUserInfo.html?wechatId=' + rukou.wechatId + '&toLink='+ rukou.linkType);
						}else{
							if(info.hasOwnProperty('healthType')){
								rukou.headlthType = info.healthType.child;
							}
							rukou.getUserPatientInfo();
							//console.log(rukou.headlthType)
						}
					}
				}
				
			})
		},
		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 ts = new Date().getTime();
						var patientInfo = res.content.list;
						if(patientInfo.length == 0){
							//未完善患者信息
							if(rukou.headlthType == 'HE1'){
								switch (rukou.linkType){
									case 'MYDOCTOR':
										window.location.replace('pages/myDoctor.html?wechatId=' + rukou.wechatId + '&ts=' + ts)
										break;
									case 'MYEMR':
										window.location.replace('pages/myEMR.html?wechatId=' + rukou.wechatId + '&ts=' + ts);
										break;
									case 'MYREMIND':
										window.location.replace('pages/myRemind.html?wechatId=' + rukou.wechatId + '&ts=' + ts);
										break;
									case 'MYCONSULT':
										window.location.replace('pages/myConsult.html?wechatId=' + rukou.wechatId + '&ts=' + ts);
									default:
										break;
								}
							}else{
								window.location.replace('https://review-formal.iplusmed.com/wechatForLungCancer/dev/wechat_lung_login/pages/createPatientInfo.html?wechatId=' + rukou.wechatId + '&toLink=' + rukou.linkType + '&ts=' + ts);
							}
						}else{
							//跳转至
							switch (rukou.linkType){
								case 'MYDOCTOR':
									window.location.replace('pages/myDoctor.html?wechatId=' + rukou.wechatId + '&ts=' + ts)
									break;
								case 'MYEMR':
									window.location.replace('pages/myEMR.html?wechatId=' + rukou.wechatId + '&ts=' + ts);
									break;
								case 'MYREMIND':
									window.location.replace('pages/myRemind.html?wechatId=' + rukou.wechatId + '&ts=' + ts);
									break;
								case 'MYCONSULT':
									window.location.replace('pages/myConsult.html?wechatId=' + rukou.wechatId + '&ts=' + ts);
								default:
									break;
							}
						}
					}
				}
			})
		}
	}
	rukou.init();
})
