<template>
	<view class="normal-login-container">
		<view class="logo-content align-center justify-center flex">
			<!-- <image style="width: 100rpx;height: 100rpx;" :src="globalConfig.appInfo.logo" mode="widthFix">
	    </image> -->
			<text class="title">DP车间管理系统</text>
		</view>
		<view class="login-form-content">
			<view class="input-item flex align-center">
				<view class="iconfont icon-user icon"></view>
				<input v-model="loginForm.username" class="input" type="text" placeholder="请输入账号" 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="请输入密码" maxlength="20" />
			</view>
			<!-- 记住密码功能 -->
			<view class="remember-psw">
				<checkbox-group @change="checkboxChange">
					<checkbox id="chkRem" type="checkbox" :checked="rememberPsw" @tap="rememberPsw = !rememberPsw" color="#09CC86" />
					记住账号密码
				</checkbox-group>
			</view>

			<view class="action-btn"><button @click="handleLogin" class="login-btn cu-btn block bg-blue lg round">登录</button></view>
		</view>
	</view>
</template>

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

export default {
	data() {
		return {
			loginForm: {
				username: '',
				password: '',
				code: '',
				uuid: ''
			},
			rememberPsw: true //复选框状态 默认勾选
		};
	},
	mounted() {
		//记住密码
		//缓存的账号
		const HBusername = uni.getStorageSync('HBusername');
		//缓存的密码
		const HBpassword = uni.getStorageSync('HBpassword');
		console.log('缓存的账号:' + HBusername);
		console.log('缓存的密码:' + HBpassword);
		//有缓存就赋值给文本没有就清空
		if (HBusername && HBpassword) {
			this.loginForm.username = HBusername;
			this.loginForm.password = HBpassword;
		} else {
			this.loginForm.username = '';
			this.loginForm.password = '';
		}
		//自动登录
	},
	created() {
		//this.getCode()
	},
	methods: {
		checkboxChange: function(e) {
			console.log('复选框状态:' + e.detail.value.length);
			if (e.detail.value.length == 1) {
				//获取缓存的账号 赋值
				uni.getStorageSync('HBusername', this.user.username);
				uni.getStorageSync('HBpassword', this.user.password);
			} else {
				//销毁
				uni.removeStorageSync('HBusername');
				uni.removeStorageSync('HBpassword');
			}
		},
		// 登录方法
		async handleLogin() {
			if (this.loginForm.username === '') {
				this.$modal.msgError('请输入您的账号');
			} else if (this.loginForm.password === '') {
				this.$modal.msgError('请输入您的密码');
			} else {
				this.$modal.loading('登录中，请耐心等待...');
				this.pwdLogin();
			}
		},
		// 密码登录
		async pwdLogin() {
			this.$store
				.dispatch('Login', this.loginForm)
				.then(res => {
					this.$modal.closeLoading();
					this.loginSuccess();
				})
				.catch(error => {
					this.$modal.closeLoading();
					this.$modal.msgError('用户名或密码错误！');
				});
		},
		// 登录成功后，处理函数
		loginSuccess(result) {
			if (this.rememberPsw) {
				uni.setStorageSync('HBusername', this.loginForm.username);
				uni.setStorageSync('HBpassword', this.loginForm.password);
			} else {
				//销毁缓存
				uni.removeStorageSync('HBusername');
				uni.removeStorageSync('HBpassword');
			}

			// 设置用户信息
			this.$store.dispatch('GetInfo').then(() => {
				this.$tab.reLaunch('/pages/index/index');
			});
		}
	}
};
</script>

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

.normal-login-container {
	width: 100%;

	.logo-content {
		width: 100%;
		font-size: 21px;
		text-align: center;
		padding-top: 15%;

		image {
			border-radius: 4px;
		}

		.title {
			margin-left: 10px;
		}
	}

	.login-form-content {
		text-align: center;
		margin: 20px auto;
		margin-top: 15%;
		width: 80%;

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

			.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;
		}

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

	.easyinput {
		width: 100%;
	}
}

.login-code-img {
	height: 45px;
}
</style>
