<template>
	<view class="u-page">
		<u--form labelPosition="left" :model="registerData" ref="registerForm">
			<u-form-item label="新电话" prop="phone" borderBottom labelWidth="120">
				<u--input v-model="registerData.phone" border="none" placeholder="电话"></u--input>
			</u-form-item>

			<u-form-item label="验证码" prop="code" labelWidth="80" borderBottom>
				<u--input v-model="registerData.verifyCode" border="none" placeholder="请填写验证码"></u--input>
				<u-button slot="right" @tap="getCode" :text="tips" type="success" size="mini" :disabled="disabled1">
				</u-button>
			</u-form-item>
			<u-code ref="uCode" @change="codeChange" seconds="20" @start="disabled1 = true" @end="disabled1 = false">
			</u-code>
		</u--form>
		<u-button type="primary" @click='$u.throttle(reset, 1000)'>确定修改</u-button>

	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex';
	var _this;
	import helper from '@/common/helper.js';
	export default {
		data() {
			return {
				disabled1: false,
				tips: '',
				registerData: {
					id: '',
					phone: '',
					verifyCode: '',
				},
				rules: {
					phone: {
						type: 'string',
						required: true,
						len: 11,
						message: '请填写11位手机号',
						trigger: ['blur']
					},
					verifyCode: {
						type: 'string',
						required: true,
						len: 6,
						message: '请填写6位验证码',
						trigger: ['blur']
					},
				}
			}
		},
		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
			this.registerData.id = this.userInfo.id
		},
		methods: {
			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
							}).then(res => {
								uni.hideLoading();
								// 这里此提示会被this.start()方法中的提示覆盖
								uni.$u.toast('验证码已发送');
								// 通知验证码组件内部开始倒计时
								this.$refs.uCode.start();

							})
							.catch(res => {
								uni.$u.toast(res.message);
							});

						// setTimeout(() => {

						// }, 2000);
					} else {
						uni.$u.toast('倒计时结束后再发送');
					}
				} else {
					uni.$u.toast('请输入正确手机号');
				}

			},


			//修改账号
			reset() {
				uni.showLoading({
					title: '加载中'
				})
				this.$refs.registerForm.validate().then(res => {
					_this.isLoading = true
					this.$request.baseRequest('post', '/commonUser/resetAccount', _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()
									setTimeout(function(){
										uni.showToast({
											title: '修改成功',
											icon: 'none',
											duration: 2000
										})
										uni.navigateTo({
											url: `/pages/public/login`
										})
									},1000)
								
								})
							} else {
								uni.showToast({
									title: res.message,
									icon: 'none',
									duration: 2000
								})
							}
							uni.hideLoading()
						})
						.catch(res => {
							uni.$u.toast(res.message);
						});
				}).catch(errors => {
					uni.$u.toast('校验失败')
				})
			},
		},
	}
</script>

<style lang="scss">

</style>
