<template>
	<view style="margin: 20rpx 40rpx;">
		<up-form labelPosition="left" labelWidth='90' :model="password" ref="form1">
			<up-form-item label="手机号:" prop="userInfo.name" :borderBottom="true" ref="item1">
				<up-input inputAlign='right' v-model="password.phone" readonly='true' border="none"></up-input>
			</up-form-item>
			<up-form-item label="验证码:" prop="userInfo.name" :borderBottom="true" ref="item1">
				<up-input inputAlign='right' type='number' v-model="password.code" clearable='true' placeholder="请输入验证码"
					border="none">
					<template #suffix>
						<up-code ref="uCodeRef" @change="codeChange" seconds="60" changeText="X秒重新获取"></up-code>
						<up-button @tap="getCode" :text="tips" type="success" size="mini"></up-button>
					</template>
				</up-input>
			</up-form-item>
			<up-form-item label="密码:" prop="userInfo.name" :borderBottom="true" ref="item1">
				<up-input inputAlign='right' v-model="password.password" type="number" placeholder="请输入密码"
					border="none"></up-input>
			</up-form-item>
			<up-form-item label="确认密码:" prop="userInfo.name" :borderBottom="true" ref="item1">
				<up-input inputAlign='right' v-model="password.rePassword" type="number" placeholder="请再次输入密码"
					border="none"></up-input>
			</up-form-item>
		</up-form>
		<view class="" style="margin-top: 20rpx;">
			<button @click="save()" :disabled='!password.password || !password.rePassword || !password.code  '
				class="logout-btn">确认</button>
		</view>
	</view>
</template>

<script setup lang="ts">
	import { onShow, onLoad } from "@dcloudio/uni-app";
	import { ref } from "vue";
	import { Service } from "../../Service/Service";
	import { CNRiderDataService } from "@/Service/CN/CNRiderDataService"
	import { CNRiderLoginService } from '@/Service/CN/CNRiderLoginService'


	let password = ref({
		realPhone: '',
		phone: '',
		password: '',
		code: '',
		rePassword: ''
	})


	let tips = ref('')
	const uCodeRef = ref(null);

	onLoad((data : any) => {
		getData()
	});

	onShow(() => {

	});

	const getData = () => {
		CNRiderDataService.GetRiderInfo().then(res => {
			if (res.code == 0) {
				password.value.realPhone = res.data.riderInfo.phone
				password.value.phone = res.data.riderInfo.phone.slice(0, 3) + '****' + res.data.riderInfo.phone.slice(-4)
			}
		})
	}



	// 验证码
	const codeChange = (text) => {
		tips.value = text;
	};

	const getCode = () => {
		if (uCodeRef.value.canGetCode) {
			// 模拟向后端请求验证码
			uni.showLoading({
				title: '正在获取验证码',
			});
			CNRiderLoginService.SendUserSms(password.value.realPhone, 'RiderUpPwd').then(res => {
				if (res.code == 0) {
					uni.hideLoading();
					// 这里此提示会被start()方法中的提示覆盖
					uni.$u.toast('验证码已发送');
					// 通知验证码组件内部开始倒计时
					uCodeRef.value.start();
				} else {
					Service.Msg(res.msg)
				}
			})
		} else {
			uni.$u.toast('倒计时结束后再发送');
		}
	};

	const change = (e) => {
		console.log('change', e);
	};

	const save = () => {
		if (password.value.password !== password.value.rePassword) {
			Service.Msg('两次密码不一致，请重新输入！')
			return
		}
		CNRiderLoginService.UpdateRiderPassword(password.value.code, password.value.password, password.value.rePassword).then(res => {
			if (res.code == 0) {
				Service.Msg('修改成功!')
				setTimeout(() => {
					Service.GoPageBack()
				}, 1500)
			} else {
				Service.Msg(res.msg)
			}
		})
	}
</script>

<style lang="scss">
	page {
		background-color: #fff;
	}

	.logout-btn {
		background-color: var(--nav-diluted);
		color: #fff;
		font-weight: 500;
		border-radius: 10rpx;
		height: 80rpx;
		line-height: 80rpx;
		font-size: 30rpx;
		margin: 0;
		border: none;
	}
</style>