<template>
	<view class="reg">
		<view class="title">
			<u-icon name="arrow-left" size="34" @click="back"></u-icon>
			<view class="text">找回密码</view>
		</view>
		<u-form :model="form" ref="uForm" label-width="160" class="form">
			<u-form-item prop="mobile" :class="{focusCode: iscFocusMobile, error: isMobileError}">
				<u-input v-model="form.mobile" @blur="inputBlur('mobile')" @focus="inputFocus('mobile')"
					:clearable="false" type="number" placeholder="请输入手机号" />
			</u-form-item>

			<u-form-item prop="imgcode" v-if="codeSrc">
				<u-input v-model="form.imgCode" placeholder="图片验证码" :clearable="false" />
				<u-image @click="getCodeSrc" width="300rpx" height="70rpx" :src="codeSrc" mode="scaleToFill"></u-image>
			</u-form-item>

			<u-form-item prop="code" :class="{focusCode: iscFocusCode, error: isCodeError}">
				<u-input v-model="form.code" @blur="inputBlur('code')" @focus="inputFocus('code')" placeholder="验证码"
					:clearable="false" />
				<view class="code" @click="codeTime">
					{{codeText}}
				</view>
			</u-form-item>

			<u-form-item :leftIconStyle="leftIconStyle" prop="password"
				:class="{focusCode: iscFocusPsw, error: isPswError}" v-if="showPsw" class="psw"
				:label-position="labelPosition" label-width="150">
				<u-input :border="false" :password-icon="false" @blur="inputBlur('psw')" @focus="inputFocus('psw')"
					:clearable="false" v-model="form.password" placeholder="设置登录密码"></u-input>
				<u-icon name="eye" size="36" v-if="showPsw" @click="showPsw = false"></u-icon>
			</u-form-item>

			<u-form-item :leftIconStyle="leftIconStyle" :class="{focusCode: iscFocusPsw, error: isPswError}"
				prop="password" v-else class="psw" :label-position="labelPosition" label-width="150">
				<u-input :border="false" type="password" @blur="inputBlur('psw')" @focus="inputFocus('psw')"
					:password-icon="false" :clearable="false" v-model="form.password" placeholder="设置登录密码"></u-input>
				<u-icon name="eye-off" size="36" @click="showPsw = true"></u-icon>
			</u-form-item>
		</u-form>
		<view class="submit " @click="submit" :class="{active: isActive}">
			确定
		</view>
		<view class="creation">
			注册即代表同意 <text @click="toLogin" class="link">《用户服务协议》</text>
		</view>
	</view>
</template>

<script>
	import config from '../../config/config.js'
	import rulesMixin from '../../config/rulesMixin.js'

	export default {
		mixins: [rulesMixin],
		data() {
			return {
				form: {
					mobile: '',
					password: '',
					code: '',
					imgCode: '',
				},
				time: 6,
				codeSrc: '',
				phoneHome: '86',
				resCode: 3322,
				show: false,
				codeText: '发送验证码',
				leftIconStyle: {
					color: '#888',
					fontSize: '36rpx'
				},
				labelPosition: 'left',
				showPsw: false,
			}
		},
		methods: {
			getCodeSrc() {
				if (this.form.mobile.length === 11) {
					this.codeSrc = config.baseUrl + '/home/imgCode/' + this.form.mobile + '?' + Date.now()
				}
			},
			back() {
				uni.navigateBack()
			},
			confirm(item) {
				this.phoneHome = item[0].value
				console.log(item)
			},
			async codeTime() {
				if (this.time !== 6) {
					return
				}
				if (!this.$u.test.mobile(this.form.mobile)) {
					this.$u.toast('手机号有误');
					return;
				}
				let res = await this.$u.api.user.sendSmsCode({
					name: this.form.mobile,
					imgCode: this.form.imgCode,
					type: 'register'
				})
				uni.showToast({
					title: '验证码已发送',
					duration: 2000,
					icon: 'none'
				});
				let timer = null
				this.codeText = this.time + ' s'
				timer = setInterval(() => {
					this.time = this.time - 1
					this.codeText = this.time + ' s'
					if (this.time < 1) {
						clearInterval(timer)
						this.time = 6
						this.codeText = '发送验证码'
					}
				}, 1000)
			},
			toLogin() {
				uni.navigateTo({
					url: './login'
				})
			},
			async submit() {
				let {
					mobile,
					code,
					password
				} = this.form
				let res = await this.$u.api.user.regPassword({
					mobile,
					code,
					password,
				})
			}
		},
		computed: {
			isActive() {
				let {
					phone,
					password,
					code
				} = this.form
				return phone !== '' && password !== '' && code !== ''
			}
		},
		watch: {
			'form.mobile'() {
				this.getCodeSrc()
			}
		},
		onReady() {},
	}
</script>

<style lang="scss" scoped>
	.title {
		padding-top: 96rpx;
		font-size: 48rpx;
		color: #303133;
		font-weight: 500;
		line-height: 58px;
		display: flex;
		padding-left: 30rpx;

		.text {
			margin-left: 10rpx;
			vertical-align: middle;
		}
	}

	.form {
		margin: 0 auto;
		margin-top: 82rpx;
		width: 654rpx;

		.text {
			padding-left: 24rpx;
			font-size: 28rpx;
			color: #000000;
			border-left: 1px solid #EDEFF2;

		}

		.item {
			text-align: center;

		}

		.code {
			color: #cacaca;
		}


		/deep/ .u-form-item__message {
			padding-left: 0 !important;
		}
	}

	.submit {
		font-size: 32rpx;
		color: #FFFFFF;
		background-color: #dfe0e5;
		width: 654rpx;
		height: 100rpx;
		background: #DFE0E5;
		border-radius: 16rpx;
		line-height: 100rpx;
		text-align: center;
		margin: 0 auto;
		margin-top: 120rpx;
	}

	.active {
		background-color: #e67d29;
	}

	.creation {
		margin-top: 568rpx;
		padding-bottom: 52rpx;
		color: #d3d3d3;
		font-size: 28rpx;
		text-align: center;

		.link {
			color: #708bc5;
			font-size: 28rpx;
			margin-left: 20rpx;
		}
	}
</style>
