<template>
	<view class="content">
		<view class="user-info-item userInfo" v-if="Formtype==='edit'">
			<view class="zhcx-table">
				<view class="zhcx-table-row">				  
					<view class="name">
						<text>姓名</text>
					</view>
					<view class="text" >
						<input type="text" v-model="name" disabled />
					</view>
				</view>		
				<view class="zhcx-table-row">
					<view class="name">
						<text>电话号码</text>
					</view>
					<view class="text" >
						<input type="text" v-model="phoneNumber" disabled  />
					</view>
				</view>	
				<view class="zhcx-table-row">
					<view class="name">
						<text>身份证</text>
					</view>
					<view class="text" >
						<input type="text" v-model="idCard" disabled />
					</view>
				</view>	
				<view class="zhcx-table-row">
					<view class="name">
						<text>性别</text>
					</view>
					<view class="text" >
						<radio-group @change="changeGender">
							<label>
								<radio value="0" :checked="gender==='0'" disabled /><text>男</text>
							</label>
							<label class="gender-item-woman">
								<radio value="1" :checked="gender==='1'" disabled /><text>女</text>
							</label>
						</radio-group>					
					</view>
				</view>		
				<view class="zhcx-table-row">
					<view class="name">
						<text>住址</text>
					</view>
					<view class="text" >
						<input type="text" v-model="address" />
					</view>
				</view>					
			</view>				
		</view>	
		<!-- 修改密码 -->
		<view class="user-info-item password" v-else>
			<view class="zhcx-table">
				<view class="zhcx-table-row">
					<view class="name">
						<text>原密码</text>
					</view>
					<view class="text" >
						<input type="text" v-model="oldPassword" placeholder="请输入原密码" />
					</view>
				</view>	
				<view class="zhcx-table-row">
					<view class="name">
						<text>新密码</text>
					</view>
					<view class="text" >
						<input type="text" v-model="password" placeholder="请输入新密码" />
					</view>
				</view>	
			</view>
		</view>
		<view class="handle-wrap">
			<view class="submit-BT" @click="submit">
				<text>提交</text>
			</view>			
		</view>
	</view>
</template>
<script>
	import {getInfo,updatePwd,logout} from '@/api/user';
	import {updatePatientrInfo} from '@/api/patient';
	import {updateDoctorInfo} from '@/api/doctor';	
	export default {
		data() {
			return {
				Formtype:'edit',
				name:"",
				phoneNumber:"",
				idCard:"",
				gender:"",
				address:"",
				oldPassword:"",
				password:"",
				userId:null,
				userType:null
			}
		},
		onLoad({type}) {
			this.Formtype=type;
			this.init(type);
		},
		methods: {
			init(type){
				if(type==="edit"){
					this.getUserInfo();
				}else{
					uni.setNavigationBarTitle({
						title:"修改密码"
					})
				}
			},
			getUserInfo(){
				let accountInfo=uni.getStorageSync("accountInfo");
				let userType=accountInfo.userType;
				this.userType=userType;
				getInfo().then((res)=>{
					let info=res.data;
					if(userType==1){
						this.userId=info.doctorId;
						this.name=info.doctorName;
						this.gender=info.doctorSex;
						this.phoneNumber=info.accountTel;
						this.idCard=info.doctorIdCard;
						this.address=info.doctorAddress;
					}
					else if(userType==2){
						this.userId=info.patientId;
						this.name=info.patientName;
						this.gender=info.patientSex;
						this.phoneNumber=info.accountTel;
						this.idCard=info.patientIdCard;
						this.address=info.patientAddress;
					}
				})
			},
			changeGender({detail}){
				this.gender=detail.value;
			},
			submit(){
				let type=this.Formtype;
				let userType=this.userType;
				let userId=this.userId;
				let params=null;
				if(type==="edit"){
					let address=(this.address).trim();
					if(address.length<1){
						uni.showToast({
							title:"请填写地址！"
						})
						return
					}
					if(userType==1){
					    params={"doctorAddress":address,"doctorId":userId}
	                    updateDoctorInfo(params).then((res)=>{
							uni.showToast({
								title:"修改成功！",
								mask:true,
								success() {
									uni.reLaunch({
										url:"/pages/myCenter/index"
									})	
								}
							})
	                    }); 					
					}else{
						params={"patientAddress":address,"patientId":userId}
	                    updatePatientrInfo(params).then((res)=>{
							uni.showToast({
								title:"修改成功！",
								mask:true,
								success() {
									uni.reLaunch({
										url:"/pages/myCenter/index"
									})								
								}
							})
	                    }); 					
					}
				}else{
					let oldPassword=(this.oldPassword).trim()
					let password=(this.password).trim();		
					  updatePwd({
						oldPassword,password
					  }).then((res)=>{
						  if(res.code===0){
								if(res.code===0){
									uni.showToast({
										title:"修改成功！",
										mask:true,
										success() {
											logout().then(()=>{
												uni.clearStorageSync();
												uni.reLaunch({
													url:"/pages/login/index"
												})
											})										
										}
									})
								}
						  }
					  })				
				}	
			}
		}
	}
</script>

<style lang="scss" scoped>
	.content{
		.user-info-item{
			.zhcx-table-row{
				.text{
					text-align: left;
					.gender-item-woman{
						margin-left: 40upx;
					}
				}
			}
		}
	}
	.handle-wrap{
		&{
			width: 100%;
			position: fixed;
			bottom: 0;
			left: 0;
			padding: 10upx 0;
		}
		.submit-BT{
			width: 702upx;
			height: 100upx;
			line-height: 100upx;
			text-align: center;
			background-color: #3384FF;
			border-radius: 12upx;	
			color: #fff;
			font-size: 32upx;
			margin: 0 auto;
		}				
	}
</style>
