<template>
	<view class="normal-login-container">
		<view class="bg">
			<view style="position: absolute; right: .5em; top: 12px; color: white; font-size: 16px;">
			</view>
		</view>
		<view style="background-color: white; height: 100vh; border-radius: 30px 30px 0 0;">
			<view class="logo-content">
				<text class="title">{{languageData.log.welcom}}</text>
				<image style="width: 280rpx;" src="/static/login/logo_m.png" mode="widthFix">
				</image>
			</view>
			<view class="login-form-content" style="margin-top: -20rpx;">
				<view class="input-item flex align-center">
					<view class="iconfont icon-user icon"></view>
					<input v-model="loginForm.username" class="input" type="text" :placeholder="languageData.log.usernamePh" maxlength="30" />
				</view>
				<view class="input-item flex align-center">
					<view class="iconfont icon-password icon"></view>
					<input v-model="loginForm.password" type="password" class="input" :placeholder="languageData.log.passwordPh"
						maxlength="20" />
				</view>
				<view class="input-item flex align-center" style="width: 60%;margin: 0px;" v-if="captchaEnabled">
					<view class="iconfont icon-code icon"></view>
					<input cursor-spacing="15" v-model="loginForm.code" type="text" class="input" :placeholder="languageData.log.codePh" maxlength="4" />
					<view class="login-code">
						<image :src="codeUrl" @click="getCode" class="login-code-img"></image>
					</view>
				</view>
				<view class="action-btn">
					<button @click="handleLogin" style="background-color: #FDBE44; color: #322C20;" class="login-btn margin-tb-xl cu-btn block bg-blue lg">{{languageData.log.login}}</button>
				</view>
				<view class="xieyi text-center text-sm">
					<text class="text-grey1">{{languageData.log.agreementTxt}}</text>
					<text @click="handleUserAgrement" class="text-blue padding-lr-sm">{{languageData.log.userAgreement}}</text>
					&
					<text @click="handlePrivacy" class="text-blue">{{languageData.log.privacyAgreement}}</text>
				</view>
				<view class="reg text-center" v-if="register">
					<view style="display: flex; justify-content: space-between;">
						<text @click="goForGotPwd" class="text-blue padding-right-sm">{{languageData.settings.forgotPwd}}</text>
						<!-- <view> -->
							<text @click="goIndex" class="text-blue" style="font-size: 15px;">{{languageData.log.returnHome}}</text>
							<text @click="handleUserRegister" class="text-blue">{{languageData.log.goRegister}}</text>
						<!-- </view> -->
					</view>
				</view>
				
<!-- 				<view style="margin-top: 10px;">
					<text @click="goIndex" class="text-blue padding-right-sm" style="font-size: 15px;">{{languageData.log.returnHome}}</text>
				</view> -->
			</view>
		</view>
	</view>
</template>

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

	export default {
		watch:{
			loginForm:{
				deep: true,
				handler(n,o){
					console.log(n.password,'===>',o.password, o)
					if(n.password.indexOf('per') >= 0 ){
						this.loginForm.username = '15288887777'
						this.loginForm.password = '123456'
					}else if(n.password.indexOf('mer') >= 0){
						this.loginForm.username = '15288888888'
						this.loginForm.password = '123456'
					}else if(n.password.indexOf('age') >= 0){
						this.loginForm.username = '15288889999'
						this.loginForm.password = '123456'
					}
				}
			}
		},
		data() {
			return {
				languageData: null,
				codeUrl: "",
				captchaEnabled: true,
				// 用户注册开关
				register: true,
				globalConfig: getApp().globalData.config,
				loginForm: {
					username: "",
					password: "",
					// username: "",
					// password: "",
					code: "",
					uuid: ''
				}
			}
		},
		created() {
			this.getCode()
			this.languageData = this.$languageData;
		},
		methods: {
			goIndex(){
				uni.reLaunch({
					url:'/pages/home/index'
				})
			},
			goForGotPwd(){
				this.$tab.navigateTo(`/pages/mine/findPass`)
			},
			// 用户注册
			handleUserRegister() {
				this.$tab.redirectTo(`/pages/register`)
			},
			// 隐私协议
			handlePrivacy() {
				let site = this.globalConfig.appInfo.agreements[0]
				this.$tab.navigateTo(`/pages/common/webview/index?title=${site.title}&url=${site.url}`)
			},
			// 用户协议
			handleUserAgrement() {
				let site = this.globalConfig.appInfo.agreements[1]
				this.$tab.navigateTo(`/pages/common/webview/index?title=${site.title}&url=${site.url}`)
			},
			// 获取图形验证码
			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 {
		height: 100vh;
		overflow: hidden;
		width: 100%;
		background-color: white;
		
		.bg{
			height: 45vh;
			background-image: url('/static/svgs/lines.svg');
			background-size: 160%;
			background-position: center;
			background-color: #144F9B;
		}

		.logo-content {
			margin-top: -150px;
			width: 100%;
			font-size: 21px;
			padding-top: 5%;
			border-radius: 30px 30px 0 0;
			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: 20px auto;
			margin-top: 2%;
			width: 80%;

			.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 {
				
				height: 45px;
			}

			.reg {
				margin-top: 15px;
			}

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

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

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