<template>
	<view style="padding: 30rpx;">
		<u--form labelPosition="left" :model="form" :rules="rules" errorType="border-bottom" ref="uForm"
			labelWidth="150rpx">
			<u-form-item label="用户名" prop="username" borderBottom>
				<u--input v-model="form.username" border="none" placeholder="请输入用户名"></u--input>
			</u-form-item>
			<u-form-item label="登录密码" prop="password" borderBottom>
				<u--input v-model="form.password" type="password" border="none" placeholder="请输入登录密码"></u--input>
			</u-form-item>
			<u-form-item label="手机号" prop="mobile" borderBottom>
				<u--input v-model="form.mobile" type="number" border="none"  disabled
					disabledColor="#FFF"></u--input>
			</u-form-item>
			<u-form-item label="验证码" prop="code" borderBottom>
				<u-code-input v-model="form.code" :maxlength="4" mode="line"></u-code-input>
				<view slot="right">
					<u-toast ref="uToast"></u-toast>
					<u-code :seconds="code.seconds" ref="uCode" @change="codeChange"></u-code>
					<u-button type="primary" @tap="getCode">{{code.tips}}</u-button>
				</view>
			</u-form-item>
		</u--form>
		<view style="height: 80rpx;"></view>
		<u-button type="primary" @click="upMobile">完成</u-button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				form: {
					mobile: "",
					code: '',
					username: "",
					password: ""
				},
				rules: {
					'username': {
						type: 'string',
						required: true,
						message: '请输入用户名',
						trigger: ['blur', 'change']
					},
					'password': {
						type: 'string',
						required: true,
						message: '请输入登录密码',
						trigger: ['blur', 'change']
					},
					'mobile': {
						type: 'string',
						required: true,
						message: '请输入手机号',
						trigger: ['blur', 'change']
					},
					'code': {
						type: 'string',
						required: true,
						message: '请输入验证码',
						trigger: ['blur', 'change']
					},
				},
				code: {
					tips: '获取验证码',
					// refCode: null,
					seconds: 60,
				}
			}
		},
		onLoad(options) {
			this.form.mobile = options.phone
		},
		methods: {
			upMobile() {
				this.$refs.uForm.validate().then(res => {
					this.$http.post('/shop/v10/user/username', this.form).then((res) => {
						if (res.code == 0) {
							uni.navigateBack({
								delta: 2,
								complete: () => {
									setTimeout(() => {
										this.$store.commit('auth/resetUser');
										this.$store.commit('auth/resetToken');
										uni.navigateTo({
											url: `/pages/me/login`,
										})
									}, 100)
								},
							})
						}
					})
				}).catch(errors => {
					uni.$u.toast(errors[0].message)
				})
			},
			codeChange(text) {
				this.code.tips = text;
			},
			getCode() {
				if (this.form.mobile.length != 11) {
					uni.$u.toast('请输入手机号');
					return;
				}
				if (this.$refs.uCode.canGetCode) {
					// 模拟向后端请求验证码
					uni.showLoading({
						title: '正在获取验证码'
					})
					this.$http.get('/com/sms/sendcode', this.form).then(res => {
						if (res.success) {
							uni.hideLoading();
							// 这里此提示会被this.start()方法中的提示覆盖
							uni.$u.toast('验证码已发送，有效期5分钟');
							// 通知验证码组件内部开始倒计时
							this.$refs.uCode.start();
						}
					})
				} else {
					uni.$u.toast('倒计时结束后再发送');
				}
			},
		}
	}
</script>

<style>

</style>