<template>
	<view class="regPage" :style="'background-color: '+theme.bg+';color: '+theme.text">
		<myNav :title="''"></myNav>
		<view class="regBody">
			<!-- 标题 -->
			<text class="regTitle">{{i18n.regist.navTitle}}</text>

			<!-- Tab -->
			<view class="regTabs">
				<view class="regTab regTabActive">
					<text class="font-26 color-white">{{i18n.regist.phone}}</text>
				</view>
			</view>

			<!-- 邮箱 -->
			<view class="regField">
				<text class="regLabel">{{i18n.regist.phone}}</text>
				<view class="regInputBox">
					<input class="regInput" type="text" v-model="email" :placeholder="i18n.regist.phoneTag" />
				</view>
			</view>

			<!-- 手机号（可选） -->
			<view class="regField">
				<text class="regLabel">{{i18n.regist.mobile}}</text>
				<view class="regInputBox">
					<input class="regInput" type="text" v-model="mobile" :placeholder="i18n.regist.mobileTag" />
				</view>
			</view>

			<!-- 密码 -->
			<view class="regField">
				<text class="regLabel">{{i18n.regist.login}}</text>
				<view class="regInputBox">
					<input class="regInput" type="text" v-model="password" password :placeholder="i18n.regist.loginTag" />
				</view>
			</view>

			<!-- 确认密码 -->
			<view class="regField">
				<text class="regLabel">{{i18n.regist.confirm}}</text>
				<view class="regInputBox">
					<input class="regInput" type="text" v-model="password2" password :placeholder="i18n.regist.confirmTag" />
				</view>
			</view>

			<!-- 邀请码 -->
			<view class="regField">
				<text class="regLabel">{{i18n.regist.invitation}}</text>
				<view class="regInputBox" :style="codeFromLink?'opacity:0.6':''">
					<input class="regInput" type="text" v-model="referral_code"
						:disabled="codeFromLink"
						:placeholder="i18n.regist.invitationTag" />
				</view>
			</view>

			<!-- 验证码 -->
			<view class="regField">
				<text class="regLabel">Verification Code</text>
				<view class="regInputBox regInputRow">
					<input class="regInput" type="text" v-model="code" :placeholder="i18n.regist.codeTag" />
					<image class="regCaptchaImg" :src="captchaImg" mode="aspectFit" @click="loadCaptcha()"></image>
					<text class="regCaptchaRefresh" @click="loadCaptcha()">↻</text>
				</view>
			</view>

			<!-- 协议 -->
			<view class="regAgree" @click="agreed=!agreed">
				<view class="regCheckbox" :class="agreed?'regChecked':''">
					<text v-if="agreed" class="font-24 color-white">✓</text>
				</view>
				<text class="font-24 color-999">{{i18n.login.tag}}</text>
				<text class="font-24 regLink" @click.stop="router.push('/pages/my/aboutUs/agreement')">{{i18n.login.tag1}}</text>
			</view>

			<!-- 注册按钮 -->
			<button @click="register" class="cu-btn regBtn">{{i18n.regist.navTitle}}</button>

			<!-- 已有账号 -->
			<view class="regFooter">
				<text class="font-26 color-999">{{i18n.login.noAccount}}</text>
				<text class="font-26 regLink" @click="router.push('/pages/login/index')">{{i18n.login.login}}</text>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				email: '',
				mobile: '',
				password: '',
				password2: '',
				code: '',
				referral_code: '',
				codeFromLink: false,
				flag: true,
				agreed: false,
				theme: '',
				captchaImg: '',
				captchaKey: '',
			}
		},
		computed: {
			i18n() {
				return this.$t('message');
			}
		},
		onLoad(e) {
			// 代理专属注册链接: 自动填入邀请码并锁定
			if (e && e.code) {
				this.referral_code = e.code
				this.codeFromLink = true  // 锁定，不允许用户修改
			}
			this.loadCaptcha()
		},
		onShow() {
			if (uni.getStorageSync("theme") == "dark") {
				this.theme = this.$store.state.darkTheme
			} else {
				this.theme = this.$store.state.lightTheme
			}
		},
		methods: {
			onClose() {
				uni.navigateBack({ delta: 1 })
			},
			register() {
				if (!this.flag) return
				if (this.email == '') {
					this.tos(this.i18n.regist.phoneTag)
					return
				}
				if (this.code == '') {
					this.tos(this.i18n.regist.codeTag)
					return
				}
				if (this.password == '') {
					this.tos(this.i18n.regist.loginTag)
					return
				}
				if (this.password2 == '') {
					this.tos(this.i18n.regist.confirmTag)
					return
				}
				if (this.password != this.password2) {
					this.tos(this.i18n.regist.tos1)
					return
				}
				if (!this.agreed) {
					this.tos(this.i18n.login.tos1)
					return
				}
				uni.showLoading({
					title: this.i18n.regist.loading,
					mask: true
				})
				this.flag = false
				var obj = {
					email: this.email,
					password: this.password,
					code: this.code,
					captcha_key: this.captchaKey,
					referral_code: this.referral_code,
				}
				if (this.mobile) {
					obj.mobile = this.mobile
				}
				this.post('/user/register', obj).then(res => {
					console.log(res)
					uni.hideLoading()
					this.flag = true
					if (res.code == 1) {
						this.tos(res.msg)
						setTimeout(() => {
							uni.reLaunch({
								url: '/pages/login/index?email=' + this.email
							})
						}, 500)
					} else {
						// 注册失败刷新验证码
						this.loadCaptcha()
					}
				})
			},
			loadCaptcha() {
				this.post('/common/captcha', {}).then(res => {
					if (res.code == 1) {
						this.captchaImg = res.data.captcha_img
						this.captchaKey = res.data.captcha_key
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	.regPage {
		min-height: 100vh;
	}

	.regBody {
		padding: 30rpx 50rpx 60rpx;
	}

	.regTitle {
		font-size: 48rpx;
		font-weight: 700;
		color: #111111;
		display: block;
		margin-bottom: 36rpx;
	}

	.regTabs {
		display: flex;
		margin-bottom: 36rpx;
	}

	.regTab {
		height: 60rpx;
		padding: 0 30rpx;
		border-radius: 8rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		background: #D1D3D8;
		margin-right: 20rpx;
	}

	.regTabActive {
		background: #10C7A0;
	}

	.regField {
		margin-bottom: 28rpx;
	}

	.regLabel {
		font-size: 28rpx;
		font-weight: 700;
		color: #333333;
		margin-bottom: 12rpx;
		display: block;
	}

	.regInputBox {
		height: 88rpx;
		background: #F0F4FA;
		border-radius: 10rpx;
		padding: 0 24rpx;
		display: flex;
		align-items: center;
	}

	.regInputRow {
		justify-content: space-between;
	}

	.regInput {
		flex: 1;
		height: 88rpx;
		font-size: 28rpx;
		color: #111111;
	}

	.regCaptchaImg {
		width: 240rpx;
		height: 80rpx;
		margin-left: 18rpx;
		flex: 0 0 auto;
		border-radius: 6rpx;
	}

	.regCaptchaRefresh {
		font-size: 36rpx;
		color: #1A74FF;
		margin-left: 12rpx;
		flex: 0 0 auto;
	}

	.regAgree {
		display: flex;
		align-items: center;
		margin: 30rpx 0;
	}

	.regCheckbox {
		width: 36rpx;
		height: 36rpx;
		border: 2rpx solid #CCCCCC;
		border-radius: 50%;
		margin-right: 14rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.regChecked {
		background: #1A74FF;
		border-color: #1A74FF;
	}

	.regLink {
		color: #1A74FF;
		margin-left: 6rpx;
	}

	.regBtn {
		width: 100%;
		height: 90rpx;
		background: #1A74FF !important;
		border-radius: 10rpx;
		line-height: 90rpx;
		color: #FFFFFF;
		font-size: 32rpx;
		text-align: center;
		margin-top: 10rpx;
	}

	.regFooter {
		text-align: center;
		margin-top: 30rpx;
	}
</style>
