<template>
	<view class="content">
		<view class="bg"></view>
		<!-- 语言切换 -->
		<!-- #ifdef APP-PLUS -->
		<view class="language">
			<view class="nowlanguage" @click="Islanguage =!Islanguage">
				{{nowlanguage}}
				<uni-icons type="down" size="18"></uni-icons>
			</view>
			<view class="languageList" v-if="Islanguage">
				<view class="item" v-for="(ite,ind) in languageList" :class="[nowlanguage==ite.name?'active':'']"
					@click="handleSetlanguage(ite)">
					{{ite.name}}
					<image src="../../static/dui1.png" v-if="nowlanguage==ite.name"></image>
				</view>
			</view>
		</view>
		<!-- #endif -->
		<view class="top">
			<image src="../../static/logo2.png"></image>
			<text>Reedaw</text>
		</view>
		<view class="login box_shadow">
			<view class="title">{{$t("common.titleLogin")}}</view>
			<view class="toggle cblue" @click="handleToggle">
				{{$t("common.titleToggleLogin")}}
			</view>
			<view class="editem">
				<view class="item">
					<view class="text">{{$t("common.titleAccountText")}}
					</view>
					<view class="input">
						<input v-model="phone" :placeholder="$t('tips.verifyRecord')" />
					</view>
				</view>
				<!-- 验证码登录 -->
				<view class="item " v-if="isCode">
					<view class="text">{{$t("common.titleCode")}}</view>
					<view class="input yanzhengma">
						<input class="uni-input" v-model="code" :placeholder="$t('common.titleCode')" />
						<button class="code" type="none" @click="handleCode" v-model="code"
							:disabled="disabled">{{second<60 ? second:$t("common.titleSendCode")}}
						</button>
					</view>
				</view>
				<!-- 密码登录 -->
				<view class="item " v-else>
					<view class="text">{{$t("common.titlePassword")}}</view>
					<view class="input">
						<input class="uni-input" v-model="password" :placeholder="$t('common.titlePassword')" />
					</view>
				</view>
				<view class="forget " v-if="!isCode">
					<text @click="handlePassword('forgetPassword')">{{$t("common.titleForgotPassword")}}?</text>
				</view>
			</view>

			<view class="xieyi">
				<checkbox-group @change="checkboxChange" class="group">
					<label>
						<checkbox :value="1" style="transform:scale(0.7)" />{{$t("common.titleAgreementText")}}
						<text @click.stop @click="handlexieyi"
							class="blue">《{{$t("common.titleAgreementContntText")}}》</text>
					</label>
				</checkbox-group>
			</view>
			<view class="btnlogin" @click="handleTelLogin">{{$t("common.titleLogin")}}</view>
			<view class="btngroup" @click="handlePassword('register')">
				<text>{{$t("common.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>
	import {
		mapState
	} from "vuex";
	export default {
		data() {
			return {
				phone: "",
				code: "",
				password: "",
				disabled: false,
				second: 60,
				value: 0,
				isCode: true,
				loginCode: "",
				Islanguage: false,
				language: "",
				nowlanguage: "",
				componentKey: 1
			}
		},
		computed: {
			...mapState(["languageList"]),
		},
		onLoad() {
			let that = this
			that.language = uni.getStorageSync('language') ? uni.getStorageSync('language') : uni.getLocale()
			that.nowlanguage = that.languageList.find(ite => ite.key == that.language).value
			console.log("language", that.language, uni.getStorageSync('language'), uni.getLocale())
			//  #ifdef  MP-WEIXIN
			that.login()
			// #endif
		},
		onReady() {
			let that = this
			that.language = uni.getStorageSync('language') ? uni.getStorageSync('language') : uni.getLocale()
			that.nowlanguage = that.languageList.find(ite => ite.key == that.language).value
		},
		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("tips.verifyAgreement"))
					return
				}
				if (that.language == 'zh-Hans' && !phoneType && !(/^1[3456789]\d{9}$/.test(that.phone))) {
					that.$tools.msg(that.$t("tips.verifyAccount"))
					return
				}
				if (that.language == 'zh-Hans' && phoneType && !(/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(that
						.phone))) {
					that.$tools.msg(that.$t("tips.verifyAccount"))
					return
				}
				if (that.language != 'zh-Hans' && !(/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(that.phone))) {
					that.$tools.msg(that.$t("tips.verifyEmailCorrect"))
					return
				}
				if (that.isCode && !that.code) {
					that.$tools.msg(that.$t("tips.verifyCode"))
					return
				}
				if (!that.isCode && !that.password) {
					that.$tools.msg(that.$t("tips.verifyPassword"))
					return
				}
				this.$model.getonlogin({
					data: that.phone,
					validate_data: that.isCode ? that.code : that.password,
					validate_type: that.isCode ? 'code' : 'password',
					language: that.language
				}).then(res => {
					console.log("data", res.data)
					that.$tools.msg(res.msg)
					if (res.code != 0) return
					that.$tools.msg(that.$t("tips.msgLoginSuccess"))
					uni.setStorageSync('token', res.data.token)
					uni.setStorageSync('aan_id', res.data.aan_id)
					setTimeout(function() {
						uni.$emit('login-sucesss');
					}, 1000)
				}).catch(err => {})
			},
			// 获取验证码
			handleCode() {
				let that = this
				let phoneType = that.phone.indexOf("@") !== -1
				if (that.language == 'zh-Hans' && !phoneType && !(/^1[3456789]\d{9}$/.test(that.phone))) {
					that.$tools.msg(that.$t("tips.verifyAccount"))
					return
				}
				if (that.language == 'zh-Hans' && phoneType && !(/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(that
						.phone))) {
					that.$tools.msg(that.$t("tips.verifyAccount"))
					return
				}
				if (that.language != 'zh-Hans' && !(/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(that.phone))) {
					that.$tools.msg(that.$t("tips.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(this.$t("tips.verifyAgreement"))
					return
				}
			},
			// 获取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)
						uni.setStorageSync('aan_id', res.data.aan_id)
						setTimeout(function() {
							uni.$emit('login-sucesss');
						}, 1000)
					})
				}
			},
			// 语言切换
			handleSetlanguage(ite) {
				let that = this
				uni.setLocale(ite.key)
				that.language = ite.key
				that.nowlanguage = ite.value
				that.Islanguage = false
				uni.setStorageSync('language', ite.key)
			},
			handleToggle() {
				this.phone = ""
				this.code = ""
				this.password = ""
				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=http://tc.pcxbc.com/technology/privacy_index"
				})
			},
			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;
			background: #fff;
			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%;
		}
	}

	.language {
		position: fixed;
		top: 25px;
		z-index: 999;
		left: 15px;
		width: 80%;

		.nowlanguage {
			font-size: 14px;
			background: #fff;
			height: 30px;
			border-radius: 5px;
			text-align: center;
			display: flex;
			width: 120px;
			align-items: center;
			justify-content: space-around;
		}

		.languageList {
			background: #fff;
			line-height: 45px;
			width: 120px;
			border-radius: 10px;
			padding: 0 10px;
			position: absolute;
			top: 42px;

			view {
				border-bottom: 1px solid #dfdfdf;
				display: flex;
				align-items: center;
				justify-content: space-between;
			}

			image {
				width: 24px;
				height: 24px;
			}

			.active {
				color: $btncolor;
				font-weight: bold;
			}
		}

		.languageList::after {
			content: "";
			position: absolute;
			border-left: 8px solid transparent;
			border-right: 8px solid transparent;
			border-bottom: 8px solid #fff;
			left: calc(50% - 8px);
			top: -8px;
		}
	}
</style>