<template>
	<view class="content">
		<view class="bg"></view>
		<view class="top">
			<image src="../../static/logo.png"></image>
			<text>青测</text>
		</view>
		<view class="login box_shadow">
			<view class="title">{{$t("login.title")}}</view>
			<view class="toggle cblue" @click="handleToggle">
				<uni-icons class="iconfont icon-qiehuan1 cblue mr-5"></uni-icons>切换登录
			</view>
			<view class="editem">
				<view class="item">
					<view class="text">手机号/邮箱</view>
					<view class="input">
						<input v-model="phone" placeholder="" />
					</view>
				</view>
				<!-- 验证码登录 -->
				<view class="item " v-if="isCode">
					<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 class="item " v-else>
					<view class="text">密码</view>
					<view class="input yanzhengma">
						<input class="uni-input" v-model="password" />
						<text class="forget code" @click="handlePassword('forgetPassword')">忘记密码？</text>
					</view>
				</view>
			</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 class="btnlogin" @click="handleTelLogin">{{$t("login.btn")}}</view>
			<view class="btngroup" @click="handlePassword('register')">
				<text>{{$t("login.register")}}</text>
			</view>
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				phone: "",
				code: "",
				password: "",
				disabled: false,
				second: 60,
				value: 1,
				isCode: true,
			}
		},
		methods: {
			checkboxChange(e) {
				this.value = e.detail.value.length ? e.detail.value[0] : "0"
			},
			// 登录、
			handleTelLogin() {
				let that = this
				let phoneType = that.phone.indexOf("@") !== -1
				if (that.value == 0) {
					that.$tools.msg("请先确认勾选协议")
					return
				}
				if (!phoneType && !(/^1[3456789]\d{9}$/.test(that.phone))) {
					that.$tools.msg("请输入正确的手机号")
					return
				}
				if (phoneType && !(/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(that.phone))) {
					that.$tools.msg("请输入正确的邮箱")
					return
				}
				if (that.isCode && !that.code) {
					that.$tools.msg(that.$t("login.codeTips"))
					return
				}
				if (!that.isCode && !that.password) {
					that.$tools.msg('请输入正确密码')
					return
				}
				this.$model.getonlogin({
					data: that.phone,
					validate_data: that.isCode ? that.code : that.password,
					validate_type: that.isCode ? 'code' : 'password'
				}).then(res => {
					console.log("data", res.data)
					if (res.code != 0) {
						that.$tools.msg(res.msg)
						return
					}
					that.$tools.msg("登录成功,进入程序中")
					uni.setStorageSync('token', res.data.token)
					uni.setStorageSync('aan_id', res.data.aan_id)
					setTimeout(function() {
						uni.reLaunch({
							url: "/pages/index/index?type=1"
						})
					}, 1000)
				}).catch(err => {})
			},
			// 获取验证码
			handleCode() {
				let that = this
				let phoneType = that.phone.indexOf("@") !== -1
				if (!phoneType && !(/^1[3456789]\d{9}$/.test(that.phone))) {
					that.$tools.msg("请输入正确的手机号")
					return
				}
				if (phoneType && !(/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(that.phone))) {
					that.$tools.msg("请输入正确的邮箱")
					return
				}
				// 
				that.$model.getSendCode({
					data: that.phone,
					type: "login"
				}).then(res => {
					console.log(res)
					if (res.code != 0) {
						that.$tools.msg(res.msg)
						return
					}
					that.disabled = true
					let interval = setInterval(() => {
						--that.second
					}, 1000)
					setTimeout(() => {
						clearInterval(interval)
						that.disabled = false
						that.second = 60
					}, 60000)
				}).catch(err => {})
			},
			handleToggle() {
				this.phone = ""
				this.isCode = !this.isCode
			},
			handlePassword(text) {
				uni.navigateTo({
					url: "/pages/login/forgetPassword?type=" + text
				})
			},
			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: $maincolor;
	}

	.top {
		width: 100%;
		display: flex;
		flex-wrap: wrap;
		align-items: center;
		justify-content: center;
		position: relative;
		top: 70px;
		z-index: 99;
		margin-bottom: 15px;

		image {
			width: 70px;
			height: 70px;
			margin: auto;
			margin-bottom: 10px;
			border-radius: 50%;
		}

		text {
			display: block;
			width: 100%;
			text-align: center;
		}
	}

	.login {
		width: 86%;
		height: auto;
		background: #fff;
		border-radius: 10px;
		padding: 10px 0;
		background-color: #fff;
		z-index: 99;
		position: absolute;
		left: 7%;
		top: 28%;
		box-shadow: 0px 1px 5px 2px #dfe2e1fc;

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

		.toggle {
			position: absolute;
			right: 15px;
			top: 20px;
		}

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


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

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

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

				input {
					height: 40px;
					line-height: 40px;
					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: 40px;
				border-radius: 5px;
				text-align: center;
				position: absolute;
				right: 0px;
				top: 0px;
				bottom: 0;
				z-index: 99;
			}

			.forget {
				width: 80px;
				background: #fff;
				color: $textcolor;
			}
		}

		.btngroup {
			height: 35px;
			line-height: 35px;
			display: flex;
			margin: 0 15px;
			justify-content: center;

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

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

	.xieyi {
		font-size: 12px;
		color: $textcolor;
		margin-left: 10px;

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