<template>
	<view class="content">
		<view class="bg"></view>
		<view class="top">
			<image></image>
			<text>轻厨记</text>
		</view>
		<view class="login box_shadow">
			<view class="title">{{$t("titleLogin")}}</view>
			<view class="toggle cblue" @click="handleToggle">
				{{$t("titleToggleLogin")}}
			</view>
			<view class="editem">
				<view class="item">
					<view class="text">{{$t("titleAccountText")}}</view>
					<view class="input">
						<input v-model="phone" />
					</view>
				</view>
				<!-- 验证码登录 -->
				<view class="item " v-if="isCode">
					<view class="text">{{$t("titleCode")}}</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+$t("titleSendCodeRetry"):$t("titleSendCode")}}
						</button>
					</view>
				</view>
				<!-- 密码登录 -->
				<view class="item " v-else>
					<view class="text">{{$t("titlePassword")}}</view>
					<view class="input">
						<input class="uni-input" v-model="password" />
					</view>
				</view>
				<view class="forget " v-if="!isCode">
					<text @click="handlePassword('forgetPassword')">{{$t("titleForgotPassword")}}</text>
				</view>
			</view>

			<view class="xieyi">
				<checkbox-group @change="checkboxChange" class="group">
					<label>
						<checkbox :value="1" style="transform:scale(0.7)" />{{$t("titleAgreementText")}}
						<!-- <text @click.stop @click="handleUserXieyi" class="blue">《用户协议》</text>和 -->
						<text @click.stop @click="handlexieyi" class="blue">《{{$t("titleAgreementContntText")}}》</text>
					</label>
				</checkbox-group>
			</view>
			<view class="btnlogin" @click="handleTelLogin">{{$t("titleLogin")}}</view>
			<view class="btngroup" @click="handlePassword('register')">
				<text>{{$t("titleRegister")}}</text>
			</view>
		</view>

		<!-- #ifdef MP-WEIXIN -->
		<!-- <view class="wxbtn">
			<button open-type="getPhoneNumber" @getphonenumber="getPhoneNumber" v-if="value==1">
				<view>
					<image src="../../static/phone.png"></image>
				</view>
				<text>手机号快捷登录</text>
			</button>
			<button v-else @click="handleIsTel">
				<view>
					<image src="../../static/phone.png"></image>
				</view>
				<text>手机号快捷登录</text>
			</button>
		</view> -->
		<!--  #endif -->
	</view>
</template>

<script>
	export default {
		data() {
			return {
				phone: "",
				code: "",
				password: "",
				disabled: false,
				second: 60,
				value: 0,
				isCode: true,
				loginCode: "",
			}
		},
		onLoad() {
			let that = this
			uni.setNavigationBarTitle({
				title: that.$t('titleLogin')
			})
			this.login()
		},
		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(that.$t("verifyAgreement"))
					return
				}
				if (!phoneType && !(/^1[3456789]\d{9}$/.test(that.phone))) {
					that.$tools.msg(that.$t("verifyMobileCorrect"))
					return
				}
				if (phoneType && !(/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(that.phone))) {
					that.$tools.msg(that.$t("verifyEmailCorrect"))
					return
				}
				if (that.isCode && !that.code) {
					that.$tools.msg(that.$t("verifyCode"))
					return
				}
				if (!that.isCode && !that.password) {
					that.$tools.msg(that.$t("verifyPassword"))
					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)
					that.$tools.msg(res.msg)
					if (res.code != 0) return
					that.$tools.msg(that.$t("msgLoginSuccess"))
					uni.setStorageSync('token', res.data.token)
					that.$store.dispatch("getHomeConfig")
					setTimeout(function() {
						uni.reLaunch({
							url: "/pages/index/index"
						})
					}, 2000)
				}).catch(err => {})
			},
			// 获取验证码
			handleCode() {
				let that = this
				let phoneType = that.phone.indexOf("@") !== -1
				if (!phoneType && !(/^1[3456789]\d{9}$/.test(that.phone))) {
					that.$tools.msg(that.$t("verifyMobileCorrect"))
					return
				}
				if (phoneType && !(/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(that.phone))) {
					that.$tools.msg(that.$t("verifyEmailCorrect"))
					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 => {})
			},
			// 获取code
			login() {
				let that = this
				uni.login({
					success(res) {
						if (res.code) {
							if (res.errMsg = "login:ok") {
								that.loginCode = res.code
							}
						}
					}
				})
			},
			// 授权
			getPhoneNumber(res) {
				const that = this
				if (res.detail.errMsg == 'getPhoneNumber:ok') {
					this.$model.getRegisterPhone({
						code: that.loginCode,
						encryptedData: res.detail.encryptedData,
						iv: res.detail.iv,
					}).then(res => {
						if (res.code != 0) return
						that.value = 1
						uni.setStorageSync('token', res.data.token)
						that.$store.dispatch("getHomeConfig")
						setTimeout(function() {
							uni.reLaunch({
								url: "/pages/index/index"
							})
						}, 1000)
					})
				}
			},
			handleIsTel() {
				if (this.value == 0) {
					this.$tools.msg(that.$t("verifyAgreement"))
					return
				}
			},
			// 切换登录
			handleToggle() {
				this.phone = ""
				this.isCode = !this.isCode
			},
			handlePassword(text) {
				uni.navigateTo({
					url: "/pageTwo/login/forgetPassword?type=" + text
				})
			},
			handlexieyi() {
				let that = this
				uni.navigateTo({
					url: "/pageTwo/webview/webview?url=https://tc.pcxbc.com/kitchenscale_all/privacy_index.html"
				})
			},
			handleUserXieyi() {
				let that = this
			}
		}
	}
</script>

<style scoped lang="scss">
	@import "@/assets/login.scss";

	.content {
		width: 100%;
		height: 100vh;
	}

	.xieyi {
		font-size: 28rpx;
		color: #999;
		margin-left: 20rpx;

		text {
			color: $maincolor;
		}
	}

	.href {
		width: auto;
	}

	.wxbtn {
		width: 100%;
		position: absolute;
		margin-top: 60rpx;
		top: 80%;

		icon {
			font-size: 50rpx;
			color: #28c445;
		}


		text {
			display: block;
			// width: 100%;
			margin-top: 5px;
			font-size: 24rpx;
			// color: #666;
			text-align: center;
			border-bottom: 1px solid blue;
			color: blue;
		}

		button {
			line-height: initial;
			display: flex;
			flex-wrap: wrap;
			padding: 0;
			justify-content: center;
		}

		button::after {
			display: none;
		}

		view {
			width: 100%;
		}

		image {
			width: 60rpx;
			height: 60rpx;
			border-radius: 50%;
		}
	}
</style>