<template>
	
		<view class="page-box">
			

		   <uni-forms :modelValue="postForm" ref="postForm" :rules="rules" :label-width="80">
			   
			   <sms-box v-model="postForm" ref="smsBox" :type="2"></sms-box>
			   
			   <uni-forms-item name="newPassword" label="新的密码" required>
			   			<uni-easyinput type="text" v-model="postForm.newPassword" placeholder="输入新密码" />
			   </uni-forms-item>
			   
			   
		   </uni-forms>
			   

			<view>
				<button type="primary" @tap="handleReset">重设密码</button>
			</view>
			
			<view class="login-opt">
			   <a @tap="$navs.toReg">立即注册</a>
			   <a @tap="$navs.toLogin">已有账号？</a>
			</view>
			       


		</view>
		
</template>

<script>
	import { resetPass } from '@/api/user.js'
	
	import SmsBox from './components/SmsBox.vue'

	export default {
		components: {
			SmsBox
		},
		data() {
			return {
				rules: {
					
					newPassword: {
						rules: [{
							required: true,
							errorMessage: '新密码不能为空！'
						}]
					}
				},
				postForm: {
					captchaKey: '',
					captchaValue: '',
					smsCode: '',
					mobile: '',
					newPassword: ''
				}
			}
		},
		
		created(){
		
		
		},

		methods: {
			


			async handleReset() {
				
				let that = this
				
				// 消息框
				const smsCheck = await this.$refs.smsBox.validate().then(res=>{return res});
				
				if(!smsCheck){
					return;
				}
				
				
				// 对整个表单进行校验，返回一个 Promise
				this.$refs.postForm.validate().then((res)=>{
					
					uni.showLoading({
						title: '加载中...'
					});
					
					// 快速注册后开始考试
					resetPass(this.postForm).then(data => {
					
						// 关闭加载
						uni.hideLoading()
						
						uni.showToast({
							title: '密码重置成功，请重新登录！',
							icon:'none',
							duration: 2000
						})
						
						setTimeout(()=>{
							//打印请求返回的数据
							uni.switchTab({
								url: '/pages/index/index'
							});
						},1500)
					
					});
					
					
				}).catch((err)=>{
				   
				})
					
			}

		}
	}
</script>

<style>
	
	

</style>
