<template>
	<view class="container">
		<u-form :model="forgetForm" ref="forgetForm" label-width="160"
			:label-style="{ color: '#000', fontSize: '30rpx'}">
			<u-form-item label="手机号" prop="phone" :border-bottom="false">
				<u-input height="80" placeholder="请输入手机号" border-color="#d0d0d0" v-model="forgetForm.phone"
					:border="true" class="form-item-input" type="number" />
			</u-form-item>
			<u-form-item label="验证码" prop="captcha" :border-bottom="false">
				<view class="code-form-item">
					<u-input height="80" placeholder="请输入验证码" border-color="#d0d0d0" v-model="forgetForm.captcha"
						:border="true" class="form-item-input code-input">
					</u-input>
					<u-button size="mini" type="primary" class="code-button" @click="getCode">{{ tips }}</u-button>
				</view>
			</u-form-item>
			<u-form-item label="新密码" prop="newPwd" :border-bottom="false">
				<u-input height="80" placeholder="请输入新密码" border-color="#d0d0d0" v-model="forgetForm.newPwd"
					:border="true" class="form-item-input" type="password" />
			</u-form-item>
			<view class="form-button" @click="submit">
				确认
			</view>
		</u-form>
		<u-verification-code :seconds="seconds" ref="uCode1" @change="codeChange"></u-verification-code>
		<u-popup v-model="show" mode="center" border-radius="10">
			<view class="popup">
				<text class="popup-title">请输入验证码</text>
				<view class="popup-content">
					<u-input height="80" placeholder="请输入验证码" border-color="#d0d0d0"
						v-model="numberCaptchaForm.numberCaptcha" :border="true" class="form-item-input"
						style="flex-grow: 1;" @confirm="submitCode" />
					<image :src="numberCaptchaSrc" mode="aspectFit" @click="loadImage"
						style="height:40px;margin-left:20rpx;width: 180rpx;flex: 0 0 auto;"></image>
				</view>
				<view class="popup-btn">
					<u-button type="primary" @click="submitCode" style="width: 48%;flex: 0 0 auto;">提交</u-button>
					<u-button type="default" @click="closePopup" style="width: 48%;flex: 0 0 auto;">取消</u-button>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import {
		getCaptchaImage,
		checknumberCaptcha,
		smsSend,
		resetPwd
	} from "@/api/login.js"
	import {
		msg, msgSuccess
	} from "@/utils/common.js"
	import {
		encryptAESCBC
	} from "@/utils/crypto.js"
	export default {
		data() {
			return {
				// ----验证码 start -----
				numberCaptchaForm: {
					numberCaptcha: '',
					numberCaptchaUuid: ''
				},
				numberCaptchaSrc: 'https://imgconvert.csdnimg.cn/aHR0cDovL3RjLnhpYW55dWNvZGVyLmNuL2Jsb2cyMDIwMDEwNDIwMzUwNy5wbmc?x-oss-process=image/format,png',
				show: false,
				tips: '',
				seconds: 60,
				// ----验证码 end -----
				forgetForm: {
					phone: '18320082803', //手机号
					captcha: "", //验证码
					captchaUuid: "",
					newPwd: '', // 新密码
				},
				rules: {
					captcha: [{
						required: true,
						message: '请输入验证码',
						trigger: ['blur']
					}],
					newPwd: [{
						required: true,
						message: '请输入新密码',
						trigger: ['blur']
					}],
					phone: [{
						required: true,
						message: '请输入手机号码',
						trigger: ['blur']
					}, {
						pattern: /^1[3-9]\d{9}$/,
						message: '请输入正确的手机号',
						trigger: ['blur']
					}]
				}
			};
		},
		methods: {
			submit() {
				const that = this;
				that.$refs.forgetForm.validate((valid) => {
					// console.log(valid, that.forgetForm, 456);
					if (valid) {
						// 联调
						const form = { ...that.forgetForm };
						form.newPwd = encryptAESCBC(form.newPwd);
						resetPwd(form).then(res=>{
							if(res.code == 0){
								msgSuccess("重置成功!");
								setTimeout(()=>{
									uni.reLaunch({
										url: "/pages/login/login",
									})
								}, 1000)
							}
						})
					}
				})
			},
			back() {
				uni.navigateBack({
					delta: 1,
					animationType: 'pop-out',
					animationDuration: 200
				})
			},
			// --------验证码 start---------
			codeChange(text) {
				this.tips = text;
			},
			getCode() {
				let { phone } = this.forgetForm
				if(!/^1[3-9]\d{9}$/.test(phone)){
					msg("请输入正确的手机号");
					return;
				}
				if (this.$refs.uCode1.canGetCode) {
					this.showPopup()
				} else {
					msg('倒计时结束后再发送');
				}
			},
			// end() {
			// 	msg('倒计时结束');
			// },
			// start() {
			// 	msg('倒计时开始');
			// },
			showPopup() {
				this.loadImage()
				this.show = true;
			},
			closePopup() {
				this.show = false;
				this.numberCaptchaForm.numberCaptcha = ''; // 清空输入框
			},
			async submitCode() {
				let { numberCaptcha } = this.numberCaptchaForm
				if (numberCaptcha.length!=4) {
					uni.showToast({
						title: '请输入4位验证码',
						icon: 'none'
					});
					return;
				}
				uni.showLoading({
					title: '正在校验验证码'
				})
				
				const checkRes = await checknumberCaptcha(this.numberCaptchaForm);
				if (!checkRes.data.valid) {
					uni.showToast({
						title: '验证码输入错误',
						icon: 'none'
					});
					return;
				}
				
				const codeRes = await smsSend({
					phone: this.forgetForm.phone
				});
				
				if (codeRes && codeRes.data.captchaUuid) {
					this.getPhoneCapt++;
					this.forgetForm.captchaUuid = codeRes.data.captchaUuid
					this.closePopup();
					this.$refs.uCode1.start()
				}
				
				uni.hideLoading();
			},
			// 加载验证码图片
			async loadImage() {
				// 联调
				uni.showLoading({
					title: '正在获取图片'
				})
				const img = await getCaptchaImage();
				if (!img.data.numberCaptchaSrc) {
					return
				}
				this.numberCaptchaForm.numberCaptchaUuid = img.data.numberCaptchaUuid
				this.numberCaptchaSrc = img.data.numberCaptchaSrc;
				uni.hideLoading();
			}
			// --------验证码 end---------
		},
		onLoad() {},
		onReady() {
			this.$refs.forgetForm.setRules(this.rules);
		}
	}
</script>
<style lang="scss">
	.u-form-item {
		padding: 30rpx 0 !important;
	}

	.u-form-item .u-input--border {
		border-radius: 16rpx !important;
	}

	.u-form-item .u-form-item--left__content__label {
		display: block !important;
		text-align: justify;
		text-align-last: justify;
		padding: 0 20rpx 0 10rpx;
	}

	.u-input {
		background-color: #f7f7f7 !important;
	}
	.code-input .u-input{
		padding-right: 200rpx !important;
	}
	.code-button .u-size-mini{
		display: block !important;
	}
</style>
<style lang="scss" scoped>
	.container {
		height: 100vh;
		width: 100vw;
		background-color: #ffffff;
		padding: 0 40rpx;
	}

	.form-item-input {
		width: 100%;
	}

	.form-button {
		background: linear-gradient(90deg, #006af5, #21b0fc);
		border-radius: 20rpx;
		line-height: 80rpx;
		text-align: center;
		color: #fff;
		margin: 50rpx 0;
	}

	.form-tip {
		text-align: center;
		color: #000000;
	}
	.code-form-item{
		position: relative;
		.code-button{
			position: absolute;
			right: 15rpx;
			top: 50%;
			transform: translateY(-50%);
		}
	}

	// 数字验证码弹窗
	.popup {
		padding: 40rpx;
		border-radius: 20rpx;

		.popup-title {
			font-size: 18px;
			text-align: center;
			display: inline-block;
			width: 100%;
			// margin-bottom: 15px;
		}

		.popup-content {
			display: flex;
			align-items: center;
			margin: 50rpx 0;
		}

		.popup-btn {
			display: flex;
			justify-content: space-between;
		}
	}
</style>
