<template>
	<view class="content">
		<view class="head">
			<text>登录</text>
		</view>
		<view class="login-wrap">
			<view class="username item">
				<text>账号：</text>
				<!-- <text></text> -->
				<input type="text" v-model="username" placeholder="请输入账号" />
			</view>
			<view class="password item">
				<text>密码：</text>
				<!-- <text></text> -->
				<input type="text" v-model="password" placeholder="请输入密码" />
			</view>
			<navigator class="tip" url="/views/toRegister/index"><text>没有账号去注册</text></navigator>
			<button type="primary" @click="loginSubmit" class="submit-BT">登录</button>			
		</view>
	</view>
</template>

<script>
	import {login,getInfo} from '@/api/user';
	import {getInfoByAccountId} from '@/api/doctor';
	import {setToken} from '@/libs/auth';
	
	export default{
		data(){
			return{
				username:"",
				password:""				
			}
		},
		onLoad({username,password}) {
			if(username){
				this.username=username;
				this.password=password;				
			}
		},
		methods:{
			async loginSubmit(){
				let {userType,id}= await this.loginApi();		
				let {data}= await this.saveUserInfo();	
				uni.setStorageSync('userInfo',data);
					if(userType===1){//是医生登录检测是否审核通过
						getInfoByAccountId(id).then((result)=>{
							let doctorAudit=result.data.doctorAudit;
							if(doctorAudit===1){	
								uni.reLaunch({
									url:'/pages/home/index'
								})
							}else if(doctorAudit===0){
								uni.showToast({
									title:"当前账号正在审核中，请耐心等候！",
									icon:"none"
								})								
							}else if(doctorAudit===2){
								uni.setStorageSync('doctorAccountInfo',result.data);
								uni.showModal({
									title:"提示!",
									content:"当前账号被驳回！请补充资料",
									confirmText:"补充资料",
									success: function (res) {
										if (res.confirm) {
											uni.navigateTo({
												url:"/views/doctorItems/updateDoctorInfo/index",
												fail(err) {
													console.log(err)
												}
											})
										} else if (res.cancel) {
											uni.showToast('取消无法登录!')
										}
									}
								})
							}else{
								uni.showToast({
									title:"当前账号不能登录！请联系管理员",
									icon:"none"
								})								
							}
						})					
					}else if(userType===2){
						uni.reLaunch({
							url:'/pages/home/index'
						})						
					}
					else{//其他类型账号
						uni.showToast({
							title:"当前账号不支持小程序登录！",
							icon:"none"
						})
					}
			},
			loginApi(){
				return new Promise((resolve, reject) => {
					let username=(this.username).trim();
					let password=(this.password).trim();
					if(username.length<1){
						uni.showToast({
							title:'请填写账号',
							icon:"error"
						})
						return;
					}
					if(password.length<1){
						uni.showToast({
							title:'请填写密码',
							icon:"error"
						})
						return;
					}				
					login({
							username,password
						}).then((res)=>{
							let token=res.data.token;
							let userType=res.data.userType;
							setToken(token);
							this.$store.commit('setUserType',userType)
							this.$store.commit('setToken',token);					
							uni.setStorageSync('accountInfo',res.data);	//保存账号信息						
							resolve(res.data)
					}).catch((error)=>{
						reject(error)
					})				
				})
			},
			saveUserInfo(){//保存用户信息
				return new Promise((resolve, reject) => {
					getInfo().then((res)=>{
						resolve(res)
					}).catch((error)=>{
						reject(error)
					})					
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.content{
		.head{
			font-size: 50upx;
			text-align: center;
			padding: 40upx 0;
			letter-spacing: 2upx;
		}
		.login-wrap{
			width: 90%;
			margin:50upx auto 0;
			box-sizing: border-box;
			padding: 20upx;
			border-radius: 8upx;
			.item{
				display: flex;
				justify-content: flex-start;
				align-items: center;
				height: 100upx;
				border-bottom: 1px solid #e5e5e5;
				font-size: 32upx;
				line-height:100upx;
				input{
					font-size: 32upx;
					height: 100upx;
					border: 0;
				}
			}
			.tip{
				text-align: right;
				padding: 18upx 0;
				font-size: 28upx;
				display: block;
				background-color: #fff;
				&:active{
					background-color: #fff;
				}
			}
			.submit-BT{
				border-radius: 16upx;
				margin-top: 50upx;
				background-color: var(--sysblue);
			}
		}		
	}
</style>
