<template>
	<view class="page">
		<uni-nav-bar  :fixed="true" background-color="#FFE05C" :border="false" :statusBar="true"  left-icon="left" title="找回密码" @clickLeft="back" />

		<view :style="{'marginTop':'10rpx'}">
			<u--form labelPosition="left" :model="form" :rules="rules" ref="uForm">

				<u-form-item :labelWidth="80" label="手机号：" prop="username" ref="item1">
					<u--input v-model="form.username"  placeholder="请输入手机号"></u--input>
				</u-form-item>

				<u-form-item :labelWidth="80" label="验证码：" prop="code" ref="item1">
					<view class="inputCode">
						<u--input v-model="form.code"  placeholder="请输入验证码" ></u--input>
					</view>
					<u-button slot="right" @tap="getCode" :text="tips" type="success" size="small" :disabled="disabled1"></u-button>
				</u-form-item>

				<u-form-item :labelWidth="80" label="新密码：" prop="newPassword" ref="item1">
					<u--input v-model="form.newPassword" placeholder="请输入新密码" type="password"></u--input>
				</u-form-item>

				<u-form-item :labelWidth="80" label="确认密码：" prop="againPassword" ref="item1">
					<u--input v-model="form.againPassword" placeholder="再次确认新密码" type="password"></u--input>
				</u-form-item>

			</u--form>

			<view :style="{'marginTop':'100rpx'}">
				<button class="customStyle"  @click="retrievePassword">提交</button>
			</view>
		</view>
		<u-code ref="uCode" @change="codeChange" seconds="60" @start="disabled1 = true" @end="disabled1 = false"></u-code>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				tips:'',
				form:{
					username:'',
					code:'',
					newPassword:'',
					againPassword:''
				},
				rules: {
					'username': {
						type: 'string',
						required: true,
						min:11,
						max: 11,
						pattern:this.$phonePattern,
						message: '请输入正确的电话号码',
						trigger: ['blur', 'change']
					},
					'code': {
						type: 'string',
						required: true,
						min:4,
						max: 6,
						message: '请输入验证码',
						trigger: ['blur', 'change']
					},
					'newPassword': {
						type: 'string',
						min:6,
						max: 16,
						required: true,
						message: '请输入6-18位密码',
						trigger: ['blur', 'change']
					},
					'againPassword': {
						type: 'string',
						min:6,
						max: 16,
						required: true,
						message: '请输入6-18位密码',
						trigger: ['blur', 'change']
					}
				},
			}
		},
		methods: {
			retrievePassword(){
				this.$refs.uForm.validate().then(res => {
					this.$api.login.retrievePassword(this.form).then(res=>{
						uni.showToast({
							icon: 'success',
							duration: 2000,
							title: '修改成功，即将跳转登录页'
						});
						setTimeout(()=>{
							this.logout()
						},2000)

					})
				})
			},
			logout(){
				this.$api.login.logout().then(res=>{
					uni.clearStorageSync();
					uni.navigateTo({
						url:'/pages/login/login'
					})
				})
			},
			back(){
				uni.navigateBack({
					delta: 1
				});
			},
			codeChange(text) {
				this.tips = text;
			},
			getCode() {
				if (this.$refs.uCode.canGetCode) {
					// 模拟向后端请求验证码
					uni.showLoading({
						title: '正在获取验证码'
					})
					this.$api.service.getSmsCode({phonenumber: this.form.username}).then(res=>{
						uni.hideLoading();
						// 这里此提示会被this.start()方法中的提示覆盖
						uni.$u.toast('验证码已发送');
						// 通知验证码组件内部开始倒计时
						this.$refs.uCode.start();
					})
				} else {
					uni.$u.toast('倒计时结束后再发送');
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	@import './index.rpx.css';
</style>
