<template>
	<view class="ezy-login-page">

		<view class="ezy-login-wrap">
			<view class="login-body-box">
				<view class="login-title-img"></view>
				<view class="yzm-show">验证码已发送至：{{data.phoneNumber}}</view>
				<view class="phone-input-box">
					<input class="phone-input" type="text" v-model="data.yzmNumber" placeholder="请输入验证码" maxlength="6"
						@input="clearYzmInput" />
					<view class="close-btn" v-if="data.clearYzmIcon" @click="clearYzm"></view>
				</view>
				<view class="login-btn login-btn-disabled" @click="loginBtn" :class="data.yzmStatus"></view>
				<text class="cxfs-btn" @click="startCountdown"
					:class="{ 'cxfs-btn-disabled': data.isDisabled}">{{data.buttonText}}</text>
				<text class="login-text">无法收到验证码</text>
				<text class="login-text">客服电话:400-052-2130</text>
			</view>
		</view>

	</view>
</template>

<script setup>
	import {
		ref,
		reactive
	} from "vue"
	import {
		onLoad,
		onReady
	} from '@dcloudio/uni-app';
	import request from "@/utils/request.js"
	import {
		login,
		sendCode
	} from "@/api/login.js"
	const data = reactive({
		phoneNumber: null,
		yzmNumber: null,
		clearYzmIcon: false,
		yzmStatus: '',
		timeLeft: 60, // 初始倒计时时间（秒）
		intervalId: null, // 定时器ID
		isDisabled: false, // 按钮是否禁用
		buttonText: '重新发送', // 按钮文本
		sliderObj: {},
	})
	const pageData = reactive({

	})

	onLoad((options) => {
		Object.assign(pageData, JSON.parse(options.data))
		console.log(pageData)
		startCountdown();

		getYzmBtn();
	})


	const getYzmBtn = () => {
		let req = {
			appkey: "FFFF0N00000000007EC0",
			phone: "18640920672",
			scene: "nc_message_h5',",
			sessionid: pageData.sessionId,
			sig: pageData.sig,
			token: pageData.token,
		}
		sendCode(req).then(res => {

		})
	}

	const loginBtn = () => {
		let req = {
			tel: "1",
			code: '1'
		}
		login(req).then(res => {
			if (res.code == 0) {
				let obj = JSON.stringify(res.data)
				console.log(obj)
				uni.setStorage({
					key: 'Mta-Auth',
					data: obj // 假设 this.userInputValue 是用户输入的数据
				});
			}
	
					uni.switchTab({
					url: `/pages/study/index`
					})
		})

		// uni.navigateTo({
		// 	url: `/pages/study/index?gradeId=${options.grade}&termId=${options.term}&text=${text1}${text2}`
		// })
	}

	const clearYzmInput = (event) => {
		if (event.detail.value.length > 0) {
			data.clearYzmIcon = true;
		} else {
			data.clearYzmIcon = false;
		}
	}

	const clearYzm = () => {
		data.yzmNumber = '';
		data.clearYzmIcon = false;
	}

	const startCountdown = () => {
		data.isDisabled = true;
		data.buttonText = `重新发送（${data.timeLeft}S）`;

		// 清除之前的定时器（如果有）
		if (data.intervalId) {
			clearInterval(data.intervalId);
		}

		// 设置新的定时器
		data.intervalId = setInterval(() => {
			data.timeLeft--;
			if (data.timeLeft <= 0) {
				clearInterval(data.intervalId);
				data.timeLeft = 60; // 重置倒计时
				data.isDisabled = false;
				data.buttonText = '重新发送';
			} else {
				data.buttonText = `重新发送（${data.timeLeft}S）`;
			}
		}, 1000);
	}
</script>