<template>
	<view>
		<u-navbar title="" autoBack placeholder></u-navbar>
		<view class="card_login">
			<view class="card_head_logo">
				<image class="mine_image" src="../static/logo.png" mode=""></image>
				<view class="welcome_title">欢迎注册宝智达气瓶</view>
			</view>
			<u--form labelPosition="left" :model="formList" :rules="rules" ref="uForm">
				<u-form-item label="类型" prop="type" labelWidth="80" borderBottom ref="item1">
					<u-radio-group v-model="formList.type" placement="row">
						<u-radio shape="circle" :customStyle="{marginRight: '20px'}" v-for="(item, index) in radiolist"
							:key="index" :label="item.name" :name="item.value"></u-radio>
					</u-radio-group>
				</u-form-item>
				<u-form-item label="手机号" prop="phone" labelWidth="80" borderBottom ref="item1">
					<u--input v-model="formList.phone" border="none" placeholder="请填写手机号"></u--input>
				</u-form-item>
				<u-form-item label="验证码" prop="verifyCode" labelWidth="80" borderBottom>
					<u--input v-model="formList.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-form-item label="密码" prop="password" labelWidth="80" borderBottom ref="item1">
					<u--input v-model="formList.password" border="none" placeholder="请填写密码"></u--input>
				</u-form-item>
			</u--form>
			<u-code ref="uCode" @change="codeChange" seconds="90" @start="disabled1 = true"
				@end="disabled1 = false"></u-code>
			<view class="title_register" @click="goLogin">去登录</view>
			<view class="card_btn">
				<u-button type="primary" :disabled="disabled" text="注册" @click="getRegister"></u-button>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				radiolist: [{
					name: '私人',
					value: 1,
				}, {
					name: '商户',
					value: 0,
				}],
				formList: {
					type: 1,
					phone: '',
					verifyCode: '',
					password: '',
				},
				tips: '',
				disabled1: false,
				rules: {
					type: {
						type: 'string',
						required: true,
						message: '请选择用户类型',
						trigger: ['blur']
					},
					phone: {
						type: 'string',
						required: true,
						message: '请填写手机号',
						trigger: ['blur']
					},
					verifyCode: {
						type: 'string',
						required: true,
						message: '请填写验证码',
						trigger: ['blur']
					},
					password: {
						type: 'string',
						required: true,
						message: '请填写密码',
						trigger: ['blur']
					},
				},
			}
		},
		computed: {
			disabled() {
				if (this.formList.phone == '' || this.formList.verifyCode == '' || this.formList.password == '') {
					return true;
				} else {
					return false;
				}
			}
		},
		methods: {
			// 注册
			getRegister() {
				this.$refs.uForm.validate().then(res => {
					uni.showLoading();
					this.$api.post('/api/applet/register', {
						phone: this.formList.phone,
						password: this.formList.password,
						verifyCode: this.formList.verifyCode
					}).then((res) => {
						if (res.code == 200) {
							uni.showToast({
								title: res.msg,
								icon: 'none'
							});
						} else {
							uni.showToast({
								title: res.data.msg,
								icon: 'none'
							});
						}
						uni.hideLoading();
					})
				}).catch(errors => {
					// uni.$u.toast('校验失败')
				})
			},
			getCode() {
				if (this.$refs.uCode.canGetCode) {
					// 模拟向后端请求验证码
					if (this.formList.phone) {
						uni.showLoading({
							title: '正在获取验证码'
						})
						// 通知验证码组件内部开始倒计时
						this.$refs.uCode.start();
						this.$api.get('/api/verify-code', {
							phone: this.formList.phone,
						}).then(res => {
							if (res.code == 200) {
								uni.hideLoading();
								// 这里此提示会被this.start()方法中的提示覆盖
								uni.$u.toast('验证码已发送');
							}
						})
					} else {
						uni.$u.toast('请先输入手机号');
					}
				} else {
					uni.$u.toast('倒计时结束后再发送');
				}
			},
			codeChange(text) {
				this.tips = text;
			},
			// 登录
			goLogin() {
				// uni.redirectTo({
				// 	url: '/pages/login'
				// })
				uni.navigateBack({
					delta: 1
				});
			}
		}
	}
</script>

<style lang="scss">
	.welcome_title {
		font-size: 46rpx;
		font-weight: 600;
		margin-bottom: 60rpx;
		margin-top: 10rpx;
	}

	.card_head_logo {
		margin-top: 50rpx;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
	}

	.card_login {
		padding: 20rpx 40rpx;
	}

	.mine_image {
		width: 100rpx;
		height: 100rpx;
	}

	.title_register {
		margin-top: 20rpx;
		display: flex;
		justify-content: flex-end;
		font-size: 28rpx;
		color: #494949;
	}

	.card_hint {
		margin-top: 60rpx;
		display: flex;
		align-items: flex-start;
	}

	.agreement_title {
		font-size: 30rpx;
		color: #333;
	}

	.protocol_title {
		color: #1177ff;
	}

	.card_btn {
		margin-top: 50rpx;
	}

	.card_checkbox {
		margin-top: 5rpx;
		position: relative;
		cursor: pointer;
	}

	.message {
		display: none;
		position: absolute;
		padding: 5px 8px;
		border: 1px solid #494949;
		border-radius: 9px;
		top: -36px;
		font-size: 12px;
		width: 110px;
		background: #494949;
		color: #fff;
		left: -6px;
	}

	.message::after {
		content: '';
		position: absolute;
		left: 8px;
		top: 100%;
		border: 8px solid transparent;
		border-top: 6px solid #494949;
		margin-left: -2px;
	}
</style>