<template>
	<view class="editphone">
		<view class="row">
			<input type="text" placeholder="身份证号" v-model="actForm.sfzh" />
		</view>
		<view class="row">
			<input type="text" placeholder="新手机号" v-model="actForm.phone" />
		</view>
		<view class="row">
			<input type="text" placeholder="手机验证码" v-model="actForm.code" />
			<button :disabled="time<60" @click="getCode" class="mini-btn" type="primary"
				size="mini">{{codeTxt}}</button>
		</view>
		<button type="primary" @tap="changePhone" class="submit">更改</button>
	</view>
</template>

<script>
	import {
		verifyPhone,
		sendSmsUpdatePhone,
		doLogin
	} from "@/api/validApi.js";
	export default {
		data() {
			return {
				ksh: "",
				time: 60,
				codeTxt: "获取验证码",
				actForm: {
					sfzh: "",
					phone: "",
					code: ""
				},
			}
		},
		onLoad() {
			
		},
		methods: {
			async getCode() {
				let sdata = {
					KSH: uni.getStorageSync("ksh"),
					SFZH: this.actForm.sfzh,
					SJH: this.actForm.phone
				};
				let res = await sendSmsUpdatePhone(sdata);
				if (res.code == 200) {
					var timer = null;
					timer = setInterval(() => {
						if (this.time > 0) {
							this.time--;
							this.codeTxt = this.time + "S之后重新发送";
							console.log(this.time);
						} else {
							this.time = 60;
							this.codeTxt = "获取验证码";
							clearInterval(timer);
						}

					}, 1000)
				}else{
					uni.showToast({
						title: res.msg, // 显示对应字段的提示信息  
						icon: "none"
					});
				}
			},
			changePhone() {
				const requiredFields = {
					sfzh: '身份证号不能为空',
					phone: "手机号不能为空",
					code: '验证码不能为空'
				};
				// 查找第一个空字段并显示错误消息  
				const emptyField = Object.keys(requiredFields).find(field => this.actForm[field] === "");
				if (emptyField) {
					uni.showToast({
						title: requiredFields[emptyField], // 显示对应字段的提示信息  
						icon: "none"
					});
				} else {
					verifyPhone({
						KSH: uni.getStorageSync("ksh"),
						SJH: this.actForm.phone,
						SFZH: this.actForm.sfzh,
						code: this.actForm.code
					}).then(res => {
						if (res.code == 200) {
							uni.showToast({
								title: "手机号修改成功",
							})
							setTimeout(async () => {
								let sdata = {
									username: uni.getStorageSync("loginInfo").username,
									password: uni.getStorageSync("loginInfo").password,
								}
								let res = await doLogin(sdata);
								if(res.code == 200){
									uni.setStorageSync("token", res.token);
									uni.navigateTo({
										url: "/pages/newindex/newindex"
									});
								}
								
							}, 1500)
						} else {
							uni.showToast({
								title: res.msg, // 显示对应字段的提示信息  
								icon: "none"
							});
						}
					})
				}

			}
		}
	}
</script>

<style scope lang="scss">
	.editphone {
		padding: 40rpx;

		.row {
			display: flex;
			border-bottom: 1px solid #d2d2d2;
			padding: 10rpx 0;
			align-items: center;

			input {
				height: 110rpx;
				padding-left: 20rpx;
				flex: 1;
			}

			.mini-btn {
				background-color: white;
				color: #1584FF;
				border-radius: 0;
				border-bottom: 1.5px solid #1584FF;
				font-size: 32rpx;
				padding: 0;
				height: 32px;

				&:after {
					border: none;
				}

				&[disabled] {
					color: #9ACAFF;
					border-bottom: 1.5px solid #9ACAFF;
				}
			}
		}

		.submit {
			margin-top: 150rpx;
			height: 100rpx;
			line-height: 100rpx;
		}

	}
</style>