<template>
	<view class="u-page content">
		<u-icon class="back-btn" name="arrow-left" color="black" size="20" @click="navBack"></u-icon>
		<view class="wrapper">
			<h2 class="Semibold">重置密码</h2>
			<u--form labelPosition="left" :model="registerData" ref="registerForm" labelWidth="80">
				<u-form-item label="手机号" prop="phone" borderBottom>
					<u--input v-model="registerData.phone" border="none" maxlength="11" placeholder="请输入手机号码">
					</u--input>
				</u-form-item>
				<u-form-item label="验证码" prop="code" borderBottom>
					<u--input v-model="registerData.verifyCode" border="none" placeholder="请填写验证码"></u--input>
					<button :disabled="disabled1" class="code-style code-small" @tap="getCode">{{tips}}</button>
				</u-form-item>
				<u-form-item label="新密码" prop="password" borderBottom>
					<!-- <u--input v-model="registerData.password" border="none" suffixIcon="eye" @suffix="textChange" placeholder="请输入新密码，6-16位字符"></u--input> -->
					<input class='password' v-model='registerData.password' v-if="isShowPassword=='password'"
						placeholder="请输入密码" type="password">
					<input maxlength='16' class='password' v-model='registerData.password' v-else type="text"
						placeholder="请输入密码">
					<image class='yanjingicon' style='width:22px;height:22px;' @click="passWordStatus(1)"
						v-if="isShowPassword=='password'" src="../../../static/yioncang.png" mode=""></image>
					<image class='yanjingicon' style='width:22px;height:22px;' @click="passWordStatus(2)" v-else
						src="../../../static/xianshi.png" mode=""></image>
				</u-form-item>

				<u-code ref="uCode" @change="codeChange" seconds="60" @start="disabled1 = true"
					@end="disabled1 = false">
				</u-code>
			</u--form>
			<button class="code-style reset" @click="$u.throttle(reset, 1000)">重置密码</button>
		</view>
	</view>
</template>
<script>
	import {
		mapState
	} from 'vuex';
	var _this;
	import helper from '@/common/helper.js';
	export default {
		data() {
			return {
				disabled1: false,
				tips: '',
				registerData: {
					phone: '',
					password: '',
					verifyCode: '',
				},
				rules: {
					phone: {
						type: 'string',
						required: true,
						len: 11,
						message: '请填写11位手机号',
						trigger: ['blur']
					},
					verifyCode: {
						type: 'string',
						required: true,
						len: 6,
						message: '请填写6位验证码',
						trigger: ['blur']
					},
				},
				isShowPassword: 'password',
			}
		},
		computed: {
			...mapState(['hasLogin', 'userInfo']),
			// 手机号中间4位加*
			// starUserphone() {
			// 	let reg = /^(\d{3})\d{4}(\d{4})$/;
			// 	if (this.userphone) {
			// 		return this.userphone.replace(reg, "$1****$2");
			// 	}
			// }
		},
		onReady() {
			// 如果需要兼容微信小程序，并且校验规则中含有方法等，只能通过setRules方法设置规则
			this.$refs.registerForm.setRules(this.rules)
		},
		onLoad() {
			_this = this
			this.registerData.phone = this.userInfo.phone
		},
		methods: {
			passWordStatus(type) {
				if (type == 1) {
					this.isShowPassword = 'text'
				} else {
					this.isShowPassword = 'password'
				}
			},
			navBack() {
				uni.navigateBack();
			},
			codeChange(text) {
				this.tips = text;
			},
			// 获取验证码
			getCode() {
				if (/^0?1[3|4|5|6|7|8][0-9]\d{8}$/.test(this.registerData.phone)) {
					if (this.$refs.uCode.canGetCode) {

						// 模拟向后端请求验证码
						uni.showLoading({
							title: '正在获取验证码'
						})
						_this.$request.baseRequest('get', '/commonUser/sendVerifyCode', {
								phone: this.registerData.phone,
								identification: 2,
								flag: 2
							}).then(res => {
								if (res.code == 200) {
									uni.hideLoading();
									// 这里此提示会被this.start()方法中的提示覆盖
									uni.$u.toast('验证码已发送');
									// 通知验证码组件内部开始倒计时
									this.$refs.uCode.start();
								} else {
									uni.showToast({
										title: res.message,
										icon: 'none',
										duration: 2000
									})
								}
							})
							.catch(res => {
								uni.$u.toast(res.message);
							});

						// setTimeout(() => {

						// }, 2000);
					} else {
						uni.$u.toast('倒计时结束后再发送');
					}
				} else {
					uni.$u.toast('请输入正确手机号');
				}
			},
			//修改密码
			reset() {
				if (this.registerData.password.length < 6) {
					uni.showToast({
						title: '密码请输入6-16字符格式',
						icon: 'none',
						duration: 2000
					})
					return
				}
				uni.showLoading({
					title: '加载中'
				})
				this.$refs.registerForm.validate().then(res => {
					uni.$u.toast('校验通过')
					_this.isLoading = true
					_this.registerData.identification = 2
					_this.$request.baseRequest('post', '/commonUser/resetPassword', _this.registerData).then(
							res => {
								// 获得数据
								if (res.code == 200) {
									uni.clearStorageSync();
									this.$request.baseRequest('post', '/auth/api/logout').then(res => {})
									this.$store.commit('logout')
									// this.$api.logout()
									uni.showToast({
										title: '修改成功',
										icon: 'none',
										duration: 2000
									})
									uni.navigateTo({
										url: `/pages/public/login`
									})
								} else {
									uni.showToast({
										title: "系统异常或验证码已过期，请退出重试",
										icon: 'none',
										duration: 2000
									})
								}
								uni.hideLoading()
							})
						.catch(res => {
							uni.$u.toast(res.message)
						});
				}).catch(errors => {
					uni.$u.toast('校验失败')
				})
			},
		},
	}
</script>

<style lang="scss">
	.content {
		background: url(../../../static/images/mine/bg@2x.png);
		background-size: 100% 100%;
		height: 100vh;
		padding-top: 85px;
		position: relative;
		box-sizing: border-box;
	}

	.back-btn {
		position: absolute;
		left: 40upx;
		z-index: 9999;
		padding-top: var(--status-bar-height);
		top: 40upx;
		font-size: 40upx;
		color: red;
	}

	.wrapper {
		position: relative;
		z-index: 90;
		padding-bottom: 40upx;
		height: 100%;
		padding: 0 30px;
	}

	.code-style {
		background: #F5BA3C;
		color: white;
	}

	.reset {
		margin-top: 50rpx;
	}

	.Semibold {
		margin-bottom: 50rpx;
	}

	.code-small {
		width: 200rpx;
		height: 60rpx;
		font-size: 24rpx;
	}
</style>
