<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="formItem" :class="{focusCode: iscFocusMobile, error: isMobileError}">
				<u-input v-model="form.mobile" :clearable="false" @blur="inputBlur('mobile')"
					@focus="inputFocus('mobile')" type="number" placeholder="请输入手机号" />
			</u-form-item>

			<u-form-item prop="imgcode" class="formItem" :class="{focusCode: iscFocusImgCode}"
				v-if="codeSrc && form.mobile.length === 11">
				<u-input v-model="form.imgCode" placeholder="图片验证码" @blur="inputBlur('imgCode')"
					@focus="inputFocus('imgCode')" :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="formItem" :class="{focusCode: iscFocusCode, error: isCodeError}">
				<u-input v-model="form.code" @blur="inputBlur('code')" @focus="inputFocus('code')" placeholder="验证码"
					:clearable="false" />
				<view class="code" v-if="codeText === '发送验证码'" @click="codeTime">
					{{codeText}}
				</view>
				<view class="time" v-else>
					{{codeText}}
				</view>
			</u-form-item>

			<u-form-item :leftIconStyle="leftIconStyle" class="psw formItem" :class="{focusCode: iscFocusPsw, error: isPswError}"
				:label-position="labelPosition" label-width="150">
				<u-input :border="false" v-if="showPsw" @blur="inputBlur('psw')" @focus="inputFocus('psw')"
					:password-icon="false" :clearable="false" v-model="form.password" placeholder="设置登录密码"></u-input>
				<u-input :border="false" type="password" v-else :password-icon="false" :clearable="false"
					v-model="form.password" @blur="inputBlur('psw')" @focus="inputFocus('psw')" placeholder="设置登录密码">
				</u-input>
				<u-icon name="eye" size="36" v-if="showPsw" @click="showPsw = false"></u-icon>
				<u-icon name="eye-off" size="36" color="#ccc" v-else @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: '',
					inviteCode: '', // 邀请码
				},
				codeSrc: '',
				phoneHome: '86',
				resCode: 3322,
				time: 6,
				show: false,
				codeText: '发送验证码',
				leftIconStyle: {
					color: '#888',
					fontSize: '36rpx'
				},
				labelPosition: 'left',
				showPsw: false,
			}
		},
		methods: {

			
			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'
				});
				console.log(res, 999900)
				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,
					password,
					code
				} = this.form;
				console.log(mobile, password, code, 88888)
				if (!this.$u.test.mobile(mobile)) {
					this.$u.toast('手机号有误');
					return;
				}
				if (password.length < 6) {
					this.$u.toast('密码不可低于六位数');
					return;
				}
				if (code.length !== 4) {
					this.$u.toast('验证码错误');
				}
				let res = await this.$u.api.user.register(this.form)
				this.$u.vuex('user.ticket', res.data.ticket)
				this.$u.toast('创建成功');
				setTimeout(() => {
					uni.navigateTo({
						url: './login'
					})
				}, 700)
			},
			getCodeSrc() {
				if (this.form.mobile.length === 11) {
					this.codeSrc = config.baseUrl + '/home/imgCode/' + this.form.mobile + '?' + Date.now()
				}
			},
		},
		computed: {
			isActive() {
				let {
					phone,
					password,
					code
				} = this.form
				return phone !== '' && password !== '' && code !== ''
			},
		},
		watch: {
			'form.mobile'() {
				this.getCodeSrc()
			}
		},
		onReady() {
			// this.$refs.uForm.setRules(this.rulus);
		},
	}
</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 {
		// 表单校验样式在App。vue
		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;
		}

		.time {
			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: 48rpx;
		color: #d3d3d3;
		font-size: 28rpx;
		text-align: center;

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