<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后重发':$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 size14" @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">登录</view>
			<view class="btngroup" @click="handlePassword('register')">
				<text>注册</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("请输入验证码")
					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)
					setTimeout(function() {
						uni.reLaunch({
							url: "/pages/home/home?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() {
				
			}
		}
	}
</script>

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

	.content {
		width: 100%;
		height: 100vh;
	}
</style>