define(['zepto','medtap'],function($,medtap){
	var main = {
		wechatId:medtap.getRequest('wechatId'),
		doctorId:medtap.getRequest('doctorId'),
		operationType:medtap.getRequest('operationType'),
		healthType:'',
		init:function(){
			main.getToken();
		},
		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: main.wechatId
				},
				success: function(res) {
					medtap.loading(0);
					if (res.success == true) {
						var token = res.content.token;
						localStorage.setItem('wxLungToken', token);
						main.checkUser();
					} else {
			
					}
				},
				error: function(res) {
					console.log(res.resultDesc);
				}
			})
		},
		//
		checkUser:function(){
			medtap.loading(1);
			medtap.submitAjax({
				url:'https://testdevgw.medtap.cn/wechat/user/getWechatUser',
				type:'GET',
				async: false,
				data:{
					wechatId:main.wechatId
				},
				success:function(res){
					medtap.loading(0);
					var info = res.content.wechatUser;
					if(info.hasOwnProperty('userId')){
						//已绑定手机号
						main.getUserDetail();
						//main.getUserPatientInfo();
					}else{
						//未绑定手机号
						window.location.replace('pages/login.html?wechatId=' + main.wechatId + '&doctorId=' + main.doctorId + '&operationType=' + main.operationType);
					}
				}
			})
		},
		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('pages/basicUserInfo.html?wechatId=' + main.wechatId + '&doctorId=' + main.doctorId + '&operationType=' + main.operationType)
						}else{
							if(info.hasOwnProperty('healthType')){
								main.healthType = info.healthType.child;
							}
							main.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){
							//未完善患者信息
							if(main.healthType == 'HE1'){
								window.location.replace('pages/doctorHomePage.html?wechatId=' + main.wechatId + '&doctorId=' + main.doctorId + '&operationType=' + main.operationType + '&needAttention=attention');
							}else{
								window.location.replace('pages/createPatientInfo.html?wechatId=' + main.wechatId + '&doctorId=' + main.doctorId + '&operationType=' + main.operationType + '&canSkip=false');
							}
						}else{
							//跳转至医生主页
							window.location.replace('pages/doctorHomePage.html?wechatId=' + main.wechatId + '&doctorId=' + main.doctorId + '&operationType=' + main.operationType  + '&needAttention=attention');
						}
					}
				}
			})
		}

	}
	main.init()
	
})