<template>
	<view class="normal-login-container container-with-elep">
		<view style="background-color: white; margin-top: 0; border-radius: 20px; padding: 20px;">
			<view class="login-form-content">
				<view class="input-item flex align-center" style="margin: 0px;">
					<view class="iconfont icon-code icon"></view>
					<input cursor-spacing="15" v-model="loginForm.phone" type="text" class="input"
						:placeholder="$languageData.log.phonePh" maxlength="20" />
				</view>
				<view class="input-item flex align-center" style="width: 60%;margin: 0px; margin-top: 20px;">
					<view class="iconfont icon-code icon"></view>
					<input v-model="loginForm.verCode" cursor-spacing="15" type="number" class="input"
						:placeholder="$languageData.log.codePh" maxlength="4" />
					<view class="login-code">
						<u-button @click="sendCode" class="login-code-img" color="#144F9B"
							:text="count < 1 ? $languageData.log.sendCode : count + 's'"></u-button>
					</view>
				</view>
				<view class="input-item flex align-center">
					<view class="iconfont icon-password icon"></view>
					<input cursor-spacing="15" v-model="loginForm.password" type="password" class="input"
						:placeholder="$languageData.log.passwordPh" maxlength="20" />
				</view>
				<view class="input-item flex align-center">
					<view class="iconfont icon-password icon"></view>
					<input cursor-spacing="15" v-model="loginForm.rePasswd" class="input" type="password"
						:placeholder="$languageData.log.pwdRepPh" maxlength="30" />
				</view>
				<view class="action-btn">
					<button @click="handleLogin" style="background-color: #FDBE44; color: #322C20;"
						class="login-btn cu-btn block bg-blue lg">{{$languageData.overview.submit}}</button>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getCodeImg
	} from '@/api/login'

	export default {
		data() {
			return {
				timer: null,
				count: 0,
				languageData: null,
				codeUrl: "",
				captchaEnabled: true,
				// 用户注册开关
				register: true,
				globalConfig: getApp().globalData.config,
				loginForm: {
					phone: '',
					username: "",
					password: "",
					// username: "",
					// password: "",
					code: "",
					uuid: ''
				}
			}
		},
		created() {
			// alert(uni.getLocale())
			this.getCode()
			this.languageData = this.$languageData;
		},
		methods: {
			sendCode() {
				if (this.count > 0)
					return;
				this.count = 60;
				this.timer = setInterval(() => {
					// setTimeout(()=>{
					// 	this.count-=1;
					// },1000)
					this.count -= 1;
					if (this.count < 1) clearInterval(this.timer)
				}, 1000)
			},
			// 获取图形验证码
			getCode() {
				getCodeImg().then(res => {
					this.captchaEnabled = res.data.captchaEnabled === undefined ? true : res.data.captchaEnabled
					if (this.captchaEnabled) {
						this.codeUrl = 'data:image/gif;base64,' + res.data.img
						this.loginForm.uuid = res.data.uuid
					}
				})
			},
			// 登录方法
			async handleLogin() {
				if (this.loginForm.username === "") {
					this.$modal.msgError(this.languageData.log.usernamePh)
				} else if (this.loginForm.password === "") {
					this.$modal.msgError(this.languageData.log.passwordPh)
				} else if (this.loginForm.code === "" && this.captchaEnabled) {
					this.$modal.msgError(this.languageData.log.codePh)
				} else {
					this.$modal.loading(this.languageData.log.loginWait)
					this.pwdLogin()
				}
			},
			// 密码登录
			async pwdLogin() {
				this.$store.dispatch('Login', this.loginForm).then(() => {
					this.$modal.closeLoading()
					this.loginSuccess()
				}).catch(() => {
					if (this.captchaEnabled) {
						this.getCode()
					}
				})
			},
			// 登录成功后，处理函数
			loginSuccess(result) {
				// 设置用户信息
				this.$store.dispatch('GetInfo').then(res => {
					this.$tab.reLaunch('/pages/home/index')
				})
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #ffffff;
	}

	.normal-login-container {
		width: 100%;
		height: 100vh;
		overflow: hidden;

		.logo-content {
			width: 100%;
			font-size: 21px;
			padding-top: 5%;
			display: flex;
			flex-direction: column;
			justify-content: left;

			image {
				align-self: center;
				border-radius: 4px;
			}

			.title {
				text-align: center;
				// margin-left: 2em;
			}
		}

		.login-form-content {
			text-align: center;
			margin: 0px auto;
			margin-top: 5%;
			width: 100%;

			.input-item {
				margin: 20px auto;
				background-color: #f5f6f7;
				height: 45px;
				border-radius: 4px;

				.icon {
					font-size: 38rpx;
					margin-left: 10px;
					color: #999;
				}

				.input {
					width: 100%;
					font-size: 14px;
					line-height: 20px;
					text-align: left;
					padding-left: 15px;
				}

			}

			.login-btn {
				margin-top: 40px;
				height: 45px;
			}

			.reg {
				margin-top: 15px;
			}

			.xieyi {
				color: #333;
				margin-top: 10px;
			}

			.login-code {
				height: 38px;
				float: right;

				.login-code-img {
					height: 38px;
					position: absolute;
					margin-left: 10px;
					width: 200rpx;
				}
			}
		}
	}
</style>