<template>
	<view class="content">
		<view class="bg"></view>
		<view class="top">
			<image src="../../static/logo2.png"></image>
			<text>Reedaw</text>
		</view>
		<view class="login box_shadow">
			<view class="title">登录</view>
			<view class="toggle cblue" @click="handleToggle">
				切换登录
			</view>
			<view class="editem">
				<view class="item">
					<view class="text">手机号/邮箱</view>
					<view class="input">
						<input v-model="phone" />
					</view>
				</view>
				<!-- 验证码登录 -->
				<view class="item " v-if="isCode">
					<view class="text">验证码</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后重发':'获取验证码'}}
						</button>
					</view>
				</view>
				<!-- 密码登录 -->
				<view class="item " v-else>
					<view class="text">密码</view>
					<view class="input">
						<input class="uni-input" v-model="password" />
					</view>
				</view>
				<view class="forget " v-if="!isCode">
					<text @click="handlePassword('forgetPassword')">忘记密码?</text>
				</view>
			</view>

			<view class="xieyi">
				<checkbox-group @change="checkboxChange" class="group">
					<label>
						<checkbox :value="1" style="transform:scale(0.7)" />阅读并同意
						<!-- <text @click.stop @click="handleUserXieyi" class="blue">《用户协议》</text>和 -->
						<text @click.stop @click="handlexieyi" class="blue">《隐私协议》</text>
					</label>
				</checkbox-group>
			</view>
			<view class="btnlogin" @click="handleTelLogin">登录</view>
			<view class="btngroup" @click="handlePassword('register')">
				<text>注册</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() {
			//  #ifdef  MP-WEIXIN
			this.login()
			// #endif
		},
		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("请输入验证码")
					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)
					that.$tools.msg(res.msg)
					if (res.code != 0) return
					that.$tools.msg("登录成功")
					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
				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 => {})
			},
			// 
			handleIsTel() {
				if (this.value == 0) {
					this.$tools.msg("请先确认勾选协议")
					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)
						that.$tools.handleUserList()
						setTimeout(function() {
							uni.reLaunch({
								url: "/pages/index/index"
							})
						}, 1000)
					})
				}
			},
			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>