define(['zepto','medtap','mui', 'mui.picker'],function($,medtap,mui){
	var basic = {
		wechatId:medtap.getRequest('wechatId'),
		userPicker: [],
		showUserPickerButton: [],
		userResult: [],
		dPicker: null,
		selectGender:'',
		canSkip:false,
		healthData: [{
				text: '患者本人',
				value: 'HE3'
			},
			{
				text: '患者家属',
				value: 'HE2'
			},
			{
				text: '健康的咨询者',
				value: 'HE1'
			},
		],
		init:function(){
			basic.getUserInfo();
			basic.initHealthType();
			basic.bindEve();
		},
		bindEve:function(){
			$('.sex_box').on('click','span',function(){
				$(this).addClass('sexOn');
				$(this).siblings('span').removeClass('sexOn');
				if($(this).attr('id') == 'male'){
					basic.selectGender = 'M';
				}else if($(this).attr('id') == 'female'){
					basic.selectGender = 'F';
				}
				console.log(basic.selectGender)
			})
		},
		initHealthType:function(){
			basic.initPickers(1,'health_typeBox','health',basic.healthData,1);
			
			$('.footer_btn').on('click',function(){
				var result = basic.checkValue();
				if(result == true){
					basic.postData();
				}else{
					
				}
			})
		},
		initPickers: function(index, el, tarEl, vals, layer) {
			var _this = this;
			_this.userPicker[index] = new mui.PopPicker({
				layer: layer
			});
			_this.userPicker[index].setData(vals);
			_this.showUserPickerButton[index] = document.getElementById(el);
			_this.userResult[index] = document.getElementById(tarEl);
			_this.showUserPickerButton[index].addEventListener('tap', function(event) {
				_this.userPicker[index].show(function(items) {
					if(layer == 1) {
						document.getElementById(tarEl).nodeName === 'INPUT' || document.getElementById(tarEl).nodeName === 'TEXTAREA' ?
							(_this.userResult[index].value = items[0]['text']) :
							(_this.userResult[index].innerHTML = items[0]['text']);
						_this.userResult[index].setAttribute("sendKey", items[0]['value']);
						if(tarEl == 'health'){
							if(items[0]['value'] == 'HE1'){
								basic.canSkip = true;
							}else{
								basic.canSkip = false;
							}
						}
					} else {
						//只显示第二级的数据
						document.getElementById(tarEl).nodeName === 'INPUT' || document.getElementById(tarEl).nodeName === 'TEXTAREA' ?
							(_this.userResult[index].value = items[0]['text'] + items[1]['text']) :
							(_this.userResult[index].innerHTML = items[0]['text'] + ' ' + items[1]['text']);
						_this.userResult[index].setAttribute("sendKey", items[0]['value']);
						if(tarEl == 'disease') {
							document.getElementById(tarEl).setAttribute('diseaseid', items[1]['value']);
							_this.userResult[index].setAttribute("sendKey", items[1]['value']);
						} else if(tarEl == 'location') {
							document.getElementById(tarEl).setAttribute('locationid', items[1]['value']);
							_this.userResult[index].setAttribute("sendKey", items[1]['value']);
						}
					}
		
				});
			}, false);
		},
		checkValue:function(){
			var flag = false;
			if(!$('#health').val()){
				medtap.toast({
					message:'请选择您的身份',
					time:1500
				});
				flag = false;
			}else if(!$('#username').val()){
				medtap.toast({
					message:'请输入真实姓名',
					time:1500
				});
				flag = false;
			}else if(basic.selectGender == ''){
				medtap.toast({
					message:'请选择称呼',
					time:1500
				});
				flag = false;
			}else if(!$('#user_mobile').val()){
				medtap.toast({
					message:'请输入联系电话',
					time:1500
				});
				flag = false;
			}else{
				flag = true;
			}
			
			return flag;
		},
		postData:function(){
			medtap.loading(1);
			medtap.submitAjax({
				url:'https://testdevgw.medtap.cn/user/update',
				type: 'post',
				contentType: "application/json",
				data:{
					username:$('#username').val(),
					healthType:$('#health').attr('sendkey'),
					sex:basic.selectGender,
					telephone:$('#user_mobile').val(),
					wechatNumber:$('#wx_num').val() || '',
					email:$('#email').val()
				},
				success:function(res){
					medtap.loading(0);
					if(res.success == true){
						setTimeout(function(){
							medtap.toast({
								message:'保存成功',
								time:1500
							});
							setTimeout(function(){
								medtap.pushWindow('createPatientInfo.html?doctorId=' + medtap.getRequest('doctorId') + '&operationType=' +medtap.getRequest('operationType') + '&canSkip=' + basic.canSkip);
							},1500);
						},2000)
					}
				}
			})
		},
		getUserInfo: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;
						$('#user_mobile').val(info.mobile);
						$('#username').val(info.username);
						var sex = info.sex;
						if(sex == 'M'){
							$('#male').addClass('sexOn');
							basic.selectGender = 'M';
						}else if(sex == 'F'){
							$('#female').addClass('sexOn');
							basic.selectGender = 'F';
						}
						if(info.hasOwnProperty('healthType')){
							if(info.healthType.child == 'HE1'){
								basic.canSkip = true;
								$('#health').val('健康的咨询者').attr('sendkey',info.healthType.child);
							}else if(info.healthType.child == 'HE2'){
								basic.canSkip = false;
								$('#health').val('患者家属').attr('sendkey',info.healthType.child);
							}else if(info.healthType.child == 'HE3'){
								basic.canSkip = false;
								$('#health').val('患者本人').attr('sendkey',info.healthType.child);
							}
						}
						$('#wx_num').val(info.wechatNumber || '');
						$('#email').val(info.email || '');
					}
				}
			})
		}
	}
	basic.init();
})