<template>
	<mobile-frame>
		<view class="main">
			<view class="one">
				<uni-forms ref="form" :modelValue="form" :rules="rules" label-width="auto">
					<uni-forms-item label="电子邮箱" name="email">
						<uni-easyinput type="text" v-model="form.email" placeholder="请输入电子邮箱" />
					</uni-forms-item>
					<uni-forms-item label="邮箱验证码" name="code">
						<view class="yzm">
							<view class="l">
								<uni-easyinput type="number" v-model="form.code" placeholder="请输入您的邮箱验证码" />
							</view>
							<view class="r">
								<button type="default" size="mini" @tap="sendCount">{{time_count==0?'验证码':time_count}}</button>
							</view>
						</view>
					</uni-forms-item>
				</uni-forms>
				<view class="btn">
					<button type="primary" @click="onSubmit('form')" size="small">立即绑定</button>
				</view>
			</view>
		</view>

	</mobile-frame>
</template>

<script>
	export default {
		data() {
			return {
				user: {},
				form: {},
				rules: {
					email: {
						rules: [{
							required: true,
							errorMessage: '请输入电子邮箱',
						}]
					},
					code: {
						rules: [{
							required: true,
							errorMessage: '请输入邮箱验证码',
						}]
					},
				},
				// 倒计时
				time_count: 0
			};
		},
		onLoad: function() {
			const that = this;
			that.watchLogin();
		},
		methods: {
			watchLogin() {
				const that = this;
				uni.getStorage({
					key: 'token',
					success: function(res) {
						let user = that.$jwt(res.data);
						if (user) that.$set(that, `user`, user);
					},
					fail: function(err) {
						console.log(err);
					}
				})
			},
			async sendCount() {
				const that = this;
				let form = that.form;
				let user = that.user;
				if (form && form.email) {
					let res = await that.$api(`/user/toBindEmail`, 'POST', {
						id: user._id,
						email: form.email
					})
					if (res.errcode == '0') {
						uni.showToast({
							title: '发送成功'
						})
						that.$set(that, `time_count`, 60);
						that.timeDown();
					}
				} else {
					uni.showToast({
						title: '输入错误，请重新输入!',
						icon: 'none'
					})
				}
			},
			// 倒计时
			timeDown() {
				const that = this;
				var times = setInterval(() => {
					that.time_count--;
					if (that.time_count <= 0) {
						clearInterval(times);
					}
				}, 1000);
			},
			// 提交保存
			onSubmit(ref) {
				const that = this;
				let user = that.user;
				that.$refs[ref].validate().then(async params => {
					params.id = user._id;
					const res = await that.$api(`/user/checkBindEmail`, 'POST', params);
					if (res.errcode == '0') {
						const arr = await that.$api(`/user/${params.id}`, 'POST', params);
						if (arr.errcode == '0') {
							uni.showToast({
								title: '绑定邮箱成功',
								icon: 'none'
							})
							uni.navigateBack({
								delta: 1
							})
						} else {
							uni.showToast({
								title: arr.errmsg,
								icon: 'none'
							})
						}
					} else {
						uni.showToast({
							title: res.errmsg,
							icon: 'none'
						})
					}
				})
			},
		}
	}
</script>
<style lang="scss">
	.main {
		display: flex;
		flex-direction: column;
		width: 100vw;
		height: 100vh;

		.one {
			padding: 2vw;

			.btn {
				text-align: center;

				button {
					margin: 0 2vw 2vw 2vw;
					background-color: var(--ff0Color);
					color: var(--fffColor);
				}

				.name {
					color: var(--f85Color);
					font-size: var(--font14Size);
				}
			}

			.Tips {
				font-size: var(--font12Size);
				color: var(--f85Color);
			}
		}
	}

	.uni-forms-item {
		margin-bottom: 6vw !important;
		display: flex;
		flex-direction: row;
	}

	.yzm {
		display: flex;
		flex-direction: row;
		justify-content: space-between;

		.l {
			flex-grow: 1;
		}

		.r {
			width: 20vw;

			button {
				width: 100%;
				line-height: 2.8;
				padding: 0 1vw;
			}
		}
	}
</style>
