<template>
	<view class="content">
		<view class="bg"></view>
		<view class="top">
			<view class="language">
				<picker mode="selector" @change="changeClickLanguage" :range="languageList" range-key="name"
					:value="levelInd">
					<view>
						{{$t('titleLanguage')}}
						<icon class="iconfont icon-arrow-down"></icon>
					</view>
				</picker>
			</view>
			<view class="logo">
				<image src="../../static/logo2.png"></image>
				<text>Reedaw</text>
			</view>
		</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("infoEmail")}}</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="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>
	</view>
</template>

<script>
	import {
		mapState
	} from "vuex";
	export default {
		data() {
			return {
				phone: "",
				code: "",
				password: "",
				disabled: false,
				second: 60,
				value: 0,
				isCode: true,
				loginCode: ""
			}
		},
		computed: {
			...mapState(["user", 'configInfo', "setLocale"]),
			languageList() {
				let that = this
				let languageList = this.configInfo.language_arr
				that.levelInd = languageList.findIndex(ite => ite.key == this.setLocale)
				return languageList
			}
		},
		onLoad() {
			let that = this
			uni.setNavigationBarTitle({
				title: that.$t('titleLogin')
			})
		},
		methods: {
			// 勾选协议
			checkboxChange(e) {
				this.value = e.detail.value.length ? e.detail.value[0] : "0"
			},
			// 语言切换
			changeClickLanguage(e) {
				let that = this
				let key = that.languageList[e.target.value].key
				let val = that.languageList[e.target.value].val
				that.levelInd = e.target.value
				that.$model.getSetLanguage({
					language: val,
				}).then(res => {
					that.$i18n.locale = key
					uni.setStorageSync('language', key)
					that.$store.commit('changeLocale', key)
					that.$store.dispatch("getHomeConfig")
					that.$tools.handleUserList()
					that.handleTabBarItem()
				})
			},
			handleTabBarItem() {
				let that = this
				uni.setTabBarItem({
					index: 0,
					text: that.$t('titleHome')
				})
				uni.setTabBarItem({
					index: 1,
					text: that.$t('titleCount')
				})
				uni.setTabBarItem({
					index: 2,
					text: that.$t('titleMe')
				})
			},
			// 登录、
			handleTelLogin() {
				let that = this
				if (that.value == 0) {
					that.$tools.msg(that.$t("verifyAgreement"))
					return
				}
				if (!(/^\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
				}
				let val = that.languageList[that.levelInd].val
				this.$model.getonlogin({
					data: that.phone,
					validate_data: that.isCode ? that.code : that.password,
					validate_type: that.isCode ? 'code' : 'password',
					language: val
				}).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)
					uni.setStorageSync('aan_id', res.data.aan_id)
					that.$tools.handleUserList()
					setTimeout(function() {
						uni.reLaunch({
							url: "/pages/index/index"
						})
					}, 1000)
				}).catch(err => {})
			},
			// 获取验证码
			handleCode() {
				let that = this
				if (!(/^\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 => {})
			},
			// 
			handleIsTel() {
				if (this.value == 0) {
					this.$tools.msg(that.$t("verifyAgreement"))
					return
				}
			},
			handleToggle() {
				this.phone = ""
				this.isCode = !this.isCode
			},
			handlePassword(text) {
				uni.navigateTo({
					url: "/body/login/forgetPassword?type=" + text
				})
			},
			handlexieyi() {
				let that = this
				uni.navigateTo({
					url: "/body/webview/webview?url=https://tc.pcxbc.com/reedaw/privacy_policy"
				})
			},
			handleUserXieyi() {
				let that = this
			}
		}
	}
</script>

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

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

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

		text {
			color: $maincolor;
		}
	}

	.href {
		width: auto;
	}

	.wxbtn {
		width: 100%;
		position: absolute;
		margin-top: 30px;
		top: 80%;

		icon {
			font-size: 25px;
			color: #28c445;
		}


		text {
			display: block;
			// width: 100%;
			margin-top: 5px;
			font-size: 12px;
			// 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: 30px;
			height: 30px;
			border-radius: 50%;
		}
	}
</style>