<template>
	<view class="content">
		<view class="bg"></view>
		<view class="login box_shadow">
			<view class="title">{{$t("login.title")}}</view>
			<view class="editem">
				<view class="item">
					<view class="text">{{$t("login.phone")}}</view>
					<view class="input">
						<input v-model="phone" placeholder="" />
					</view>
				</view>
				<view class="item ">
					<view class="text">{{$t("login.code")}}</view>
					<view class="input yanzhengma">
						<input class="uni-input" v-model="code" />
						<button class="code" type="none" @click="handleCode" v-model="code"
							:disabled="disabled">{{second<60 ? second+'S后重发':$t("login.sendcode")}}
						</button>
					</view>

				</view>
			</view>
			<view class="btngroup">
				<text>{{$t("login.ForgotPassword")}}</text>
				<text>{{$t("login.register")}}</text>
			</view>
			<view class="btnlogin" @click="handleTelLogin">{{$t("login.btn")}}</view>
			<view class="xieyi">
				<checkbox-group @change="checkboxChange" class="group">
					<label>
						<checkbox :value="1" style="transform:scale(0.7)" />{{$t("login.agreement")}}
						<text @click="handlexieyi" @click.stop>{{$t("login.agreementContnt")}}</text>
					</label>
				</checkbox-group>
			</view>
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				phone: "",
				code: "",
				disabled: false,
				second: 60,
				value: 0,
			}
		},
		methods: {
			checkboxChange(e) {
				this.value = e.detail.value.length ? e.detail.value[0] : "0"
			},
			// 登录、
			handleTelLogin() {
				let that = this
				if (this.value == 0) {
					that.$tools.msg(that.$t("login.valtips"))
					return
				}
				if (!this.phone) {
					that.$tools.msg(that.$t("login.phonetip"))
					return
				}
				if (!(/^1[3456789]\d{9}$/.test(that.phone))) {
					that.$tools.msg(that.$t("login.phonetipsTwo"))
					return
				}
				if (!that.code) {
					that.$tools.msg(that.$t("login.codeTips"))
					return
				}
				this.$model.getRegister({
					code: that.code,
					phone: that.phone,
					sessionId: uni.getStorageSync('sessionid'),
				}).then(res => {
					if (res.code != 0) {
						that.$tools.msg(res.message)
						return
					}
					uni.setStorageSync('token', res.data.token)
					uni.setStorageSync('iswxphone', res.data.iswxphone)
					uni.setStorageSync('refreshtoken', res.data.refreshtoken)
					uni.setStorageSync('sessionid', res.data.sessionid)
					uni.reLaunch({
						url: "/pages/index/index"
					})
				}).catch(err => {})
			},
			// 获取验证码
			handleCode() {
				let that = this
				if (!that.phone) {
					that.$tools.msg(that.$t("login.phonetip"))
					return
				}
				if (!(/^1[3456789]\d{9}$/.test(that.phone))) {
					that.$tools.msg(that.$t("login.phonetipsTwo"))
					return
				}
				// 
				that.$model.getSendCode({
					phone: that.phone
				}).then(res => {
					console.log(res)
					if (res.code != 0) {
						that.$tools.msg(res.message)
						return
					}
					that.disabled = true
					let interval = setInterval(() => {
						--that.second
					}, 1000)
					setTimeout(() => {
						clearInterval(interval)
						that.disabled = false
						that.second = 60
					}, 60000)
				}).catch(err => {})
			},
			handlexieyi() {
				uni.navigateTo({
					url: "/pages/index/index"
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	.content {
		width: 100%;
		height: 100vh;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.bg {
		position: absolute;
		top: 0;
		width: 100%;
		height: 50vh;
		z-index: 9;
		background : linear-gradient(to top, #477EFF 40%, #39D9C9);
	}

	.login {
		width: 70%;
		height: auto;
		background: #fff;
		border-radius: 10px;
		padding: 20px;
		background-color: #fff;
		z-index: 99;
		margin-left: calc(10% - 40px);
		box-shadow: 0px 1px 5px 2px #dfe2e1fc;

		.title {
			text-align: left;
			color: #333;
			font-size: 40rpx;
			font-weight: bold;
			margin-bottom: 15px;
		}

		.editem {
			position: relative;
			display: flex;
			align-items: center;
			font-size: 28rpx;
			justify-content: space-between;
			flex-wrap: wrap;


			.item {
				width: 100%;
				margin-bottom: 15px;

				.text {
					font-size: 14px;
					margin-bottom: 15px;
				}

				.input {
					height: 40px;
					line-height: 40px;
					display: flex;
					position: relative;
					border: #dfdfdf 1px solid;
					border-radius: 10px;
					padding: 0 10px;
				}

				input {
					height: 45px;
					line-height: 45px;
					position: absolute;
					left: 10px;
					right: 0px;
					z-index: 88;
					font-size: 14px;
				}

				.yanzhengma {
					input {
						right: 120px;
						font-size: 28rpx;
					}
				}
			}

			.code {
				width: 110px;
				background: #dfdfdf;
				font-size: 12px;
				margin: 0;
				line-height: 45px;
				border-radius: 5px;
				text-align: center;
				position: absolute;
				right: 0px;
				top: 0px;
				bottom: 0;
				z-index: 99;
			}
		}

		.btngroup {
			width: 100%;
			height:35px;
			line-height: 35px;
			display: flex;
			justify-content: space-between;

			text {
				display: block;
				color: $textcolor;
			}
		}

		.btnlogin {
			width: 100%;
			margin: 15px 0;
			height: 42px;
			line-height: 42px;
			background: $btncolor;
			font-weight: 700;
			border-radius: 15px;
			text-align: center;
			color: #fff !important;
		}
	}

	.xieyi {
		font-size: 14px;
		margin-top: 15px;
		color: $textcolor;

		text {
			border-bottom: 1px solid $textcolor;
		}
	}
</style>