<template>
	<view class="container">
		<view class="title">绑定手机号</view>
		<view class="phone formItem">
			<image style="width: 28rpx;height: 28rpx;" src="../../static/icons/order-icon2.png" mode="aspectFit">
			</image>
			<input v-model="phone" placeholder="请输入手机号" class="formItemInput" type="text" />
		</view>
		<view class="validaCode formItem">
			<uni-icons type="locked"></uni-icons>
			<input v-model="validaCode" class="formItemInput" placeholder="请输入验证码" type="text" />
			<view :class="{ 'disabled': isButtonDisabled }" class="validaCodeBtn" @click="sendVerificationCode">{{buttonText}}</view>
		</view>

		<view class="submit" @click="bindPhone">
			绑定
		</view>
	</view>
</template>

<script>
	import { setToken } from '../../utils/auth';
import request from '../../utils/request';
	export default {
		data() {
			return {
				phone: '15254781016',
				validaCode: '',
				countdownTime: 60,
				isButtonDisabled: false,
			}
		},
		computed: {
			buttonText() {
				if (this.isButtonDisabled) {
					return `${this.countdownTime}s 后重新发送`;
				} else {
					return "获取验证码";
				}
			},
		},
		methods: {
			sendVerificationCode() {
				if (this.isButtonDisabled) {
					return
				}
				let reg = /^((13[0-9])|(14[0-9])|(15[0-9])|(17[0-9])|(18[0-9]))\d{8}$/;
				if (!reg.test(this.phone)) {
					uni.showToast({
						icon: 'none',
						title: '请输入正确的11位手机号'
					})
					this.phone = '';
					return false;
				}
				if (this.isButtonDisabled) {
					return; // 如果按钮已禁用，则直接返回
				}
				const data = {
					phone: this.phone,
				}
				request({
					url: '/rescue/sendSmsQx',
					method: 'get',
					params: data,
					headers: {
						isToken: false,
					},
					header: {
						Authorization: 'Bearer ' + uni.getStorageSync('validaCodeToken')
					}
				}).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);
			},
			bindPhone() {
				let reg = /^((13[0-9])|(14[0-9])|(15[0-9])|(17[0-9])|(18[0-9]))\d{8}$/;
				if (!reg.test(this.phone)) {
					uni.showToast({
						icon: 'none',
						title: '请输入正确的11位手机号'
					})
					this.phone = '';
					return false;
				}
				if (!this.validaCode) {
					uni.showToast({
						icon: 'none',
						title: '请输入验证码'
					})
					return false;
				}

				request({
					url: '/rescue/updateUserQx',
					method: 'post',
					data: {
						phone: this.phone,
						code: this.validaCode
					},
					headers: {
						isToken: false,
					},
					header: {
						Authorization: 'Bearer ' + uni.getStorageSync('validaCodeToken')
					}
				}).then(res => {
					console.log('updateUserQx: ',res);
					if (res.code == 200) {
						setToken(uni.getStorageSync('validaCodeToken'))
						uni.reLaunch({
						  url: '/pages-home/home/home'
						})
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				})
			}
		}
	}
</script>

<style lang="less" scoped>
	.container {
		height: 100%;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		row-gap: 40rpx;
		background-color: #F5F5F5;

		.title {
			font-size: 40rpx;
			font-weight: bold;
			margin-bottom: 20rpx;
		}

		.formItem {
			box-sizing: border-box;
			width: 680rpx;
			height: 100rpx;
			border-radius: 50rpx;
			background-color: #fff;
			padding: 0 30rpx;

			display: flex;
			align-items: center;
			column-gap: 16rpx;

			.formItemInput {
				flex: 1;
				width: 0;
			}

			.validaCodeBtn {
				background-color: #1890ff;
				color: #fff;
				height: 60rpx;
				border-radius: 30rpx;
				padding: 0 30rpx;
				font-size: 28rpx;

				display: flex;
				align-items: center;
				justify-content: center;

				&.disabled {
					background-color: #1890ff99;
				}
			}
		}

		.submit {
			height: 100rpx;
			border-radius: 50rpx;
			width: 600rpx;
			background-color: #1890ff;
			color: #fff;

			display: flex;
			align-items: center;
			justify-content: center;
		}
	}
</style>
