<!-- 注册第2步 -->
<template>
	<view class="register-main">
		<view class="title">请输入验证码</view>
		<view class="hint">验证码已发送至：{{ phone }}</view>
		<view class="input-code">
			<verification-code-input v-model="code"></verification-code-input>
		</view>
		<view class="hint2">
			<view class="green">{{ codeDuration === 0 ? "" : codeDuration }}</view>
			<view class="grey" @click="sendRegisterCode">{{
        codeDuration === 0 ? "重新发送验证码" : "秒后可重新发送验证码"
      }}</view>
		</view>
	</view>

	<!-- <view class="btn">
		<submit-button title="下一步" @submit="nextStep"></submit-button>
	</view> -->
	<view class="register-main">
		<view class="title">请设置登录密码</view>
		<view class="hint">设置登录密码用于登录</view>
		<view class="hint1">密码</view>
		<view class="form-input">
			<input class="input" v-model="state.password" placeholder="请输入密码" placeholder-class="form-placeholder"
				:password="state.isPwdType ? true : false" type="text" />
			<image :src="`${$imgUrl}login/${
	      state.isPwdType ? 'icon_eye_close' : 'icon_eye_open'
	    }.png`" class="eye" @click="state.isPwdType = !state.isPwdType" mode="aspectFill"></image>
		</view>
		<view class="hint4">{{ state.pwdHint }}</view>
	</view>

	<view class="btn">
		<submit-button title="确认注册" @submit="doRegister"></submit-button>
	</view>
</template>

<script setup lang="ts">
	import { msg, navTo, confirm } from "@/utils/utils";
	import { onLoad } from "@dcloudio/uni-app";
	import { ref } from "vue";
	import { request,requestNew } from "@/utils/network/request";
	import { sendMessage,regist } from "@/utils/network/api.js";
	import { reactive } from "vue";

	//上个界面传递的电话号码
	const phone = ref("");
	const code = ref(null);

	//倒计时时常
	const codeDuration = ref(0);
	let interval = null;
	const state = reactive({
		isPwdType: true,
		isAffirmPwdType: true,
		password: "", //密码
		phone: "", //手机号
		code: "", //验证码
		pwdHint: "密码不少于10位，必须同时包含数字、大小写字母和特殊符号,不能使用3个连续字符",
	});
	onLoad((options) => {
		phone.value = options.phone;
		codeInterval();
	});

	/* 验证码倒计时 */
	const codeInterval = () => {
		codeDuration.value = 60;
		interval = setInterval(() => {
			codeDuration.value--;
			if (codeDuration.value === 0) {
				if (interval) {
					clearInterval(interval);
					interval = null;
				}
			}
		}, 1000);
	};

	/* 发送验证码 */
	const sendRegisterCode = () => {
		if (codeDuration.value !== 0) {
			return;
		}
		const options = {
			type: 2,
			data: { 
				mobile: phone.value ,
			    businessType:1
			},
			method: "POST",
			showLoading: true,
		};
		requestNew(sendMessage, options).then((res) => {
			msg("验证码发送成功！");
			codeInterval();
		});
	};
	/* 注册 */
	const doRegister = () => {
		if (!code.value) {
			msg("请先输入验证码！");
			return;
		}
		if (!state.password) {
			msg("请输入密码");
			return;
		}

		const options = {
			type: 2,
			data: {
				loginPass: state.password,
				mobile: phone.value,
				code: code.value,
				nickName: phone.value,
			},
			method: "POST",
			showLoading: true,
		};
		requestNew(regist, options).then((res) => {
			confirm(
				"注册成功，请登录！",
				() => {
					uni.$emit("login", { phone: phone.value });
					uni.navigateBack({ delta: 2 });
				},
				"提示",
				false
			);
		});
	};
</script>

<style lang="scss" scoped>
	.register-main {
		border-top: 1rpx solid #dcdcdc;
		padding: 78rpx 30rpx 50rpx;

		.title {
			font-size: 32rpx;
			color: #333333;
			font-weight: 600;
		}

		.hint {
			font-size: 24rpx;
			color: #999999;
			margin-top: 20rpx;
		}
	}

	.input-code {
		// margin: 100rpx -10rpx 0px;
		background: transparent !important;
	}

	::v-deep .flex-box .hide-input {
		background: transparent !important;
	}

	::v-deep .code-box {
		background: transparent !important;
	}

	.hint2 {
		margin-top: 40rpx;
		display: flex;

		.green {
			font-size: 28rpx;
			color: #00b38b;
		}

		.grey {
			font-size: 24rpx;
			color: #999999;
			margin-left: 16rpx;
		}
	}

	.btn {
		width: 90%;
		margin: 0 auto;
		margin-top: 100rpx;
	}

	.form-input {
		display: flex;
		flex-direction: row;
		align-items: center;
		height: 90rpx;
		margin-top: 30rpx;
		border-bottom: 1rpx solid #dcdcdc;
		color: #333333;

		.input {
			flex: 1;
			padding-right: 48rpx;
			font-size: 26rpx;
			background: transparent;
		}

		.eye {
			width: 48rpx;
			height: 48rpx;
		}
	}

	.register-main {
		border-top: 1rpx solid #dcdcdc;
		padding: 78rpx 30rpx 50rpx;

		.title {
			font-size: 32rpx;
			color: #333333;
			font-weight: 600;
		}

		.hint {
			font-size: 24rpx;
			color: #999999;
			margin-top: 20rpx;
		}

		.hint1 {
			color: #333333;
			font-size: 28rpx;
			margin-top: 100rpx;
			font-weight: 600;
		}

		.hint4 {
			margin-top: 30rpx;
			font-size: 26rpx;
			color: #00b38b;
		}
	}
</style>