<template>
	<view class="main">
		<view :style="{height:iSMT+'px'}"></view>

		<view style="height:1.5vh;" />

		<view class="top">
			<view class="top-list">
				<view class="left">
					<image class="image" src="/static/my/bindedEmail.png" />
					<text class="label">已绑邮箱：{{ email }}</text>
				</view>
			</view>

			<view class="top-list">
				<view class="left">
					<image class="image" src="/static/my/changeEmail.png" />
					<input v-model="userEmail" placeholder="请输入您的换绑邮箱" class="input" />
				</view>
				<view class="right">
					<button class="verification" :class="{ 'disabled': gettingCode }" @click="getCode"
						:disabled="gettingCode">
						{{ gettingCode ? `重新发送 ${time}s` : '获取验证码' }}
					</button>
				</view>
			</view>

			<view class="top-list">
				<view class="left">
					<image class="image" src="/static/my/verification.png" />
					<input type="text" v-model="verifyCode" placeholder="请输入验证码" class="input" />
				</view>
			</view>
		</view>

		<view class="bottom">
			<button class="change-btn" @click="changeAccount">换绑</button>
		</view>
	</view>
</template>

<script setup>
	import {
		ref,
		onMounted
	} from 'vue'
	import {
		getUserInfo
	} from "@/api/member"
	import {
		sendEmail,
		changeBind,
		validateCode
	} from "@/api/setting/password"

	const iSMT = ref(0)
	const email = ref('')
	const gettingCode = ref(false)
	const time = ref(60)
	const userEmail = ref('')
	const verifyCode = ref('')

	const userInfoPromise = getUserInfo()
	userInfoPromise.then(res => {
		if (res.code === 200) {
			console.log('个人信息', res.data)
			email.value = res.data.email
		} else {
			uni.showToast({
				title: '用户信息请求失败',
				icon: 'none',
			})
		}
	})

	const changeAccount = async () => {
		if (!userEmail) {
			uni.showToast({
				title: '请输入邮箱',
				icon: 'none'
			})
			return
		}

		if (!verifyCode) {
			uni.showToast({
				title: '请输入验证码',
				icon: 'none'
			})
			return
		}

		const res1 = await validateCode({
			loginType: 'EMAIL',
			account: email.value,
			verifyCode: verifyCode.value
		})
			console.log('校验码的结果',res1.code)
		if (res1.code === 200) {
			const res2 = await changeBind({
				verificateType: 0,
				account: userEmail.value
			})
				console.log('换绑的结果',res2.code)
			if (res2.code === 200) {
				uni.showToast({
					title: '绑定成功',
					icon: 'none',
				})
				uni.navigateTo({
					url: '/pages/home/member'
				})

			} else {
				uni.showToast({
					title: '用户绑定失败',
					icon: 'none',
				})
			}
		} else {
			uni.showToast({
				title: '验证失败，请检查验证码',
				icon: 'none',
			})

			return
		}
	}

	const getCode = () => {
		if (gettingCode.value) return
		gettingCode.value = true

		time.value = 2

		const timer = setInterval(() => {
			time.value--
			if (time.value <= 0) {
				clearInterval(timer)
				gettingCode.value = false
				time.value = 2
			}
		}, 1000)
		sendEmail({
			email: email.value
		})
	}

	onMounted(() => {
		// 状态栏高度
		iSMT.value = uni.getSystemInfoSync().statusBarHeight;
		console.log('看看高度', iSMT.value)
	})
</script>

<style>
	.top {
		height: auto;
		background-color: white;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.top-list {
		width: 630rpx;
		height: 7vh;
		margin: 0rpx 40rpx;
		display: flex;
		align-items: center;
		border-bottom: 1rpx solid #eee;
	}

	.left {
		flex: 1;
		display: flex;
		align-items: center;
	}

	.label {
		font-size: 28rpx;
		margin-left: 10rpx;
	}

	.right {
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.input {
		flex: 1;
		height: 70rpx;
		font-size: 29rpx;
		margin-left: 20rpx;
	}

	.verification {
		font-size: 24rpx;
		border-radius: 10rpx;
		background-color: rgb(230, 230, 230);
	}

	.bottom {
		height: 22vh;
		background-color: white;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.change-btn {
		height: 85rpx;
		width: 610rpx;
		padding: 0 20rpx;
		background-color: black;
		color: white;
		border-radius: 40rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.image {
		width: 40rpx;
		height: 40rpx;
	}
</style>