<template>
	<view>

		<view class="content">
			<!-- 主体 -->
			<view class="main">
				<view class="tips">若你忘记了密码，可在此重置为：123456。</view>
				<wInput v-model="phoneData" type="text" maxlength="11" placeholder="请输入手机号码"></wInput>
				<wInput v-model="userName" type="text" maxlength="11" placeholder="请输入姓名" isShowPass></wInput>
			</view>

			<wButton bgColor="linear-gradient(to bottom right, rgb(55,186,189), #94e2c4)" class="wbutton" text="重置密码"
				:rotate="isRotate" @click.native="startReset()"></wButton>

		</view>
	</view>
</template>

<script>
	let _this;
	import wInput from '../watch-login/watch-input.vue' //input
	import wButton from '../watch-login/watch-button.vue' //button
	export default {
		data() {
			return {
				phoneData: "", //电话
				userName: "", //姓名
				verCode: "", //验证码
				isRotate: false, //是否加载旋转
			}
		},
		components: {
			wInput,
			wButton
		},
		mounted() {
			_this = this;
		},
		methods: {
			async startReset(e) {
				//登录
				if (this.isRotate) {
					//判断是否加载中，避免重复点击请求
					return false;
				}
				if (this.phoneData.length == "") {
					uni.showToast({
						icon: 'none',
						position: 'bottom',
						title: '手机号不能为空'
					});
					return;
				}
				if (!/^1[3|4|5|6|7|8|9][0-9]\d{4,8}$/.test(this.phoneData)) {
					uni.showToast({
						icon: 'none',
						position: 'bottom',
						title: '手机号不正确'
					});
					return
				}
				if (this.userName.length == "") {
					uni.showToast({
						icon: 'none',
						position: 'bottom',
						title: '姓名不能为空'
					});
					return;
				}
				_this.isRotate = true
				// 发送请求
				const {
					data: res
				} = await this.$httpRequest({
					url: '/api/resetPwd?un=' + this.userName + "&phone=" + this.phoneData,
					method: 'get',
					urlType: 1,
					isNotToken: true
				})
				_this.isRotate = false;
				if (res.code == 200) {
					_this.isRotate = false
					uni.showToast({
						icon: 'ok',
						position: 'bottom',
						title: '已重置为默认密码'
					});
					this.$goto('login');
				} else {
					uni.showToast({
						icon: 'error',
						position: 'bottom',
						title: '手机号与姓名不匹配'
					});
				}
				_this.isRotate = false;
			},
			getVerCode() {
				//获取验证码
				if (_this.phoneData.length != 11) {
					uni.showToast({
						icon: 'none',
						position: 'bottom',
						title: '手机号不正确'
					});
					return false;
				}
				console.log("获取验证码")
				this.$refs.runCode.$emit('runCode'); //触发倒计时（一般用于请求成功验证码后调用）
				uni.showToast({
					icon: 'none',
					position: 'bottom',
					title: '模拟倒计时触发'
				});

				setTimeout(function() {
					_this.$refs.runCode.$emit('runCode', 0); //假装模拟下需要 终止倒计时
					uni.showToast({
						icon: 'none',
						position: 'bottom',
						title: '模拟倒计时终止'
					});
				}, 3000)
			},
			startRePass() {
				//重置密码
				if (this.isRotate) {
					//判断是否加载中，避免重复点击请求
					return false;
				}
				if (this.phoneData.length != 11) {
					uni.showToast({
						icon: 'none',
						position: 'bottom',
						title: '手机号不正确'
					});
					return false;
				}
				if (this.passData.length < 6) {
					uni.showToast({
						icon: 'none',
						position: 'bottom',
						title: '密码不正确'
					});
					return false;
				}
				if (this.verCode.length != 4) {
					uni.showToast({
						icon: 'none',
						position: 'bottom',
						title: '验证码不正确'
					});
					return false;
				}
				console.log("重置密码成功")
				_this.isRotate = true
				setTimeout(function() {
					_this.isRotate = false
				}, 3000)


			}
		}
	}
</script>

<style>
	@import url("../watch-login/css/icon.css");
	@import url("./css/main.css");
</style>
