<template>
	<view class="content">
		<view class="top">
			<view class="touxiang">
				<image src="../../static/xinlogo.png" mode=""></image>
			</view>
			<view class="twotap" >
				<view  :class="{'blck':activeid == index}" v-for="(item,index) in qh " :key="index" @click="getxz(index)">{{item.text}}</view>
			</view>
			<view class="inputs"  v-if="activeid == 0">
				<input type="text" v-model="tel" placeholder="请输入手机号">
			</view>
			<view class="inputs"  v-if="activeid == 1">
				<input type="text" v-model="tel" placeholder="请输入手机号">
			</view>
			<view class="inputs"  style="justify-content: space-between; "  v-if="activeid == 1">
				<input type="text" v-model="yzm" placeholder="请输入验证码">
				 <button  class="jsq"  :disabled="isButtonDisabled" @click="sendVerificationCode">{{ buttonText }}</button>	 
			</view>
			<view class="inputs" v-if="activeid == 0">
				<input type="password"  v-model="wrod" placeholder="请输入密码">
			</view>
			<view class="wjworb" v-if="activeid == 0" @click="goforgot()">
				<view class="">忘记密码</view>
			</view>
			
			
		</view>
		
		<view class="dl" @click="getlogin()">
			<text>登 录</text>
		</view>
		<view class="hging">
			<text>没有账号？</text>
			<text style="color: #0078FF;" @click="goregister()">立即注册</text>
		</view>	
		<!-- <view class="wx">
			<image src="../../static/-h-wxdl.png" mode=""></image>
		</view>
		<view class="hui">
			<text>微信登录</text>
		</view> -->
		<view class="dbottom">
			
			<text>登录即代表您已同意</text>
			<text class="lan">《用户协议》</text>
			<text>和</text>
			<text class="lan">《隐私政策》</text>
		</view>
	</view>
</template>

<script>
	import request from '../../utils/request';
	import {setToken, setTenantId,removeToken} from '@/utils/auth.js'
  import {
    getTenantIdByWebsite
  } from '@/api/login.js'
	export default{
		data(){
			return{
				phoneNumber: "",
				isButtonDisabled: false,
				countdownTime: 60,
				qh:[
					{text:'密码登录'},
					{text:'验证码登录'}
				],
				tel:'',
				yzm:'',
				wrod:'',
				activeid:0,
				tenantCode: 'lanan'
			}
		},
		created() {
			getTenantIdByWebsite(this.tenantCode).then(res => {
			  const tenantId = res.data;
			  if (tenantId && tenantId >= 0) {
			    setTenantId(tenantId)
			  }
			})
		},
		computed: {
		   buttonText() {
		     if (this.isButtonDisabled) {
		       return `${this.countdownTime}s 后重新发送`;
		     } else {
		       return "获取验证码";
		     }
		   },
		 }, 
		 onLoad(){
			 removeToken()
		 },
		methods:{
			//登录请求
			getlogin(){
				if(this.tel == ''){
					uni.showToast({
						title:'输入框不能为空！',
						icon:'none'
					})
					return
				}
				
				if(this.activeid == 0){
					
					let data ={
						username:this.tel,
						password:this.wrod,
						type:0
					}
					console.log('密码请求',data);
					request({
						url: '/rescue/driverLogin',
						method: 'post',
						data: data
					}).then((res)=>{
						console.log('密码请求',res);
						if(res.code == 200){
							setToken(res.data.accessToken)
							
							this.userinfo()
						}
					})
					
				}
				if(this.activeid == 1){
					let data ={
						phone:this.tel,
						code:this.yzm,
						type:1
					}
					request({
						url: '/rescue/driverLogin',
						method: 'post',
						data: data
					}).then((res)=>{
						console.log('验证码请求',res);
						if(res.code == 200){
							setToken(res.data.accessToken)
							this.userinfo()
							uni.showToast({
								title:'登录成功',
								icon:'none'
							})
							
						}
					})
				}
			},
			userinfo(){
				request({
					url: '/rescue/getRescueDriverInfo',
					method: 'get',
					 
				}).then((res)=>{
					if(res.code =='200'){
						console.log(res)
						uni.setStorageSync('driverInfo', res.data.driverInfo.id)
						uni.setStorageSync('userId', res.data.user.id)
						this.goselect()
					}
					
					
	
				})
			},
			getxz(index){
				this.activeid = index
			},
			goregister(){
				uni.navigateTo({
					url:'/pages/login/register'
				})
			},
			goforgot(){
				uni.navigateTo({
					url:'/pages/login/forgot'
				})
			},
			goselect(){
				
				console.log('去司机端首页');
				uni.navigateTo({
					url:'/pages/index/index'
				})
			},
			// 验证码方法
			sendVerificationCode() {
				let reg = /^((13[0-9])|(14[0-9])|(15[0-9])|(17[0-9])|(18[0-9]))\d{8}$/;
				if (!reg.test(this.tel)) {
					uni.showToast({
						icon: 'none',
						title: '请输入正确的11位手机号'
					})
					this.tel = '';
					return false;
				}
			  if (this.isButtonDisabled) {
			    return; // 如果按钮已禁用，则直接返回
			  }
						const data = {
							  phone:this.tel,
						  }
						request({
						  url: '/loginSmsCode',
						  method: 'post',
						  params: data
						}).then((res)=>{
							console.log('验证码',res);
							if(res.code == 200){
								uni.showToast({
									title:'验证码已发送 请注意查收',
									icon:'none'
								})
							}else{
								uni.showToast({
									title:'网络不佳请稍后再试',
									icon:'none'
								})
							}
						})

						
			    this.disableButton(); // 禁用按钮
			    this.startCountdown(); // 启动倒计时
			 
			},
			disableButton() {
			  this.isButtonDisabled = true;
			},
			enableButton() {
			  this.isButtonDisabled = false;
			},
			startCountdown() {
			  let countdown = setInterval(() => {
			    this.countdownTime--;
			    if (this.countdownTime === 0) {
			      clearInterval(countdown);
			      this.enableButton(); // 启用按钮
			    }
			  }, 1000);
			},
		}
	}
	
</script>

<style scoped lang="scss">
	.jsq{
		border: none !important;
		font-size: 14px;
		font-weight: 400;
		color: #0078FF;
	}
	button {
	  border: none !important;
	  background: none !important;
	  margin: 0px;
	   display: block;
	}
	button:focus {
		color: #0078FF;
	  outline: none;
	  box-shadow: none;
	}
	.content{
		box-sizing: border-box;
	}
	.top{
		box-sizing: border-box;
		padding: 0px 27px;
		padding-top: 100px;
		// background-color:lightseagreen;
		background-image: url('../../static/loginbj.png');
		background-size: cover;
		background-position: center;
		width: 100%;
		// height: 385px;
	}
	.touxiang{
		width: 88px;
		height: 88px;
		border-radius: 10px;
		background: #D7D7D7;
		margin: 0 auto;
		margin-bottom: 40px;
		overflow: hidden;
		image{
			width: 100%;
			height: 100%;
		}
	}
	.twotap{
		width: 100%;
		display: flex;
		align-items: center;
		view{
			font-size: 15px;
			font-family: Microsoft YaHei;
			font-weight: 400;
			color: #666666;
			margin-right: 38.5px;
		}
	}
	.blck{
		color: #333333 !important;
		font-weight: bold !important;
	}
	.inputs{
		box-sizing: border-box;
		padding: 16px 0px;
		display: flex;
		align-items: center;
		border-bottom: 2px solid #E8E8E8 ;
		margin-top: 20px;
	}
	.wjworb{
		margin-top: 5px;
		width: 100%;
		display: flex;
		font-size: 12px;
		color: #0078FF;
		justify-content: flex-end;
		align-items: center;
		
	}
	.dl{
		width: 90%;
		height: 44px;
		background: linear-gradient(-46deg, #0853C4 0%, #4282D8 80%);
		border-radius: 5px;
		margin: 0 auto;
		font-size: 16px;
		color: #FFFFFF;
		display: flex;
		justify-content: center;
		align-items: center;
		margin-top: 22px;
	}
	.hging{
		width: 100%;
		text-align: center;
		font-size: 14px;
		font-weight: 400;
		color: #636363;
		margin-top: 20px;
	}
	.wx{
		width: 38px;
		height: 38px;
		overflow: hidden;
		box-sizing: border-box;
		margin: 0px auto;
		margin-top: 20px;
		image{
			width: 100%;
			height: 100%;
		}
	}
	.hui{
		font-size: 10px;
		width: 100%;
		text-align: center;
		font-weight: 400;
		color: #666666;
	}
	.dbottom{
		width: 100%;
		text-align: center;
		font-size: 12px;
		color: #333333;
		margin-top: 40px;
		position: fixed;
		bottom: 25px;
	}
	.lan{
		color: #0078FF;
	}
</style>