<template>
	<view class="amms-page">
		<cu-custom :isBack="true" :showUser="false" :showChange="false">
			<block v-slot:content>登录</block>
		</cu-custom>
		
		<!-- <image :src="loginImg" class="i-login"></image> -->
		<view class="icon-view">
			<image :src="appIcon" class="app-icon"></image>
		</view>
		<transition name="slide-fade">
			<view class="flex flex-direction user-view">
				<view class="flex align-center padding-tb border-b-ddd margin-bottom-xs" style="width: 100%;">
					<image :src="userImg" class="i-user margin-right"></image>
					<input type="text" v-model="loginForm.username" placeholder="用户名" style="width: 100%;"/>
				</view>
				<view class="flex align-center padding-tb border-b-ddd margin-bottom-xs">
					<image :src="passwordImg" class="i-password margin-right"></image>
					<input class="uni-input" placeholder="密码" :password="!showPassword" v-model="loginForm.password" style="width: 100%;"/>
					<uni-icons v-if="!showPassword" type="eye" size="30" @click="changePassword"></uni-icons>
					<uni-icons v-else type="eye-filled" size="30" @click="changePassword" color="#166ce5"></uni-icons>
					<!-- <input type="text" :password="true" v-model="loginForm.password" placeholder="密码" /> -->
				</view>
				<view class="flex align-center padding-bottom-sm border-b-ddd" style="padding-top: 20rpx">
					<image :src="codeImg" class="i-password margin-right" style="margin-top: 10rpx"></image>
					<input type="text" v-model="loginForm.code" placeholder="验证码" style="margin-top: 10rpx" />
					<image :src="src" class="i-captcha" @tap="createCode()"></image>
				</view>
				<view class="margin-top-xl"><button class="amms-main-button margin-top-xl" @tap="login">
						登录
					</button></view>
				<!-- <view class="flex justify-between margin-top-sm main-sub-color font26">
					<text>注册</text>
					<text @tap="forgetPassword">忘记密码？</text>
				</view> -->
			</view>
		</transition>
	</view>
</template>

<script>
	import apiMapper from '../../apiMapper/index';
	import {
		request,
		loginRequest,
		ipPrefix
	} from '../../utils/request';
	import {
		deepCloneNew,
		randomLenNum
	} from '../../common/index.js';
	
	export default {
		data() {
			return {
				appIcon: '../../static/img/personalCenter/appIcon.png',
				loginImg: '../../static/img/login/i-login.png',
				userImg: '../../static/img/login/i-user.png',
				codeImg: '../../static/img/login/i-code.png',
				passwordImg: '../../static/img/login/i-password.png',
				loginForm: {
					username: '',
					password: '',
					grant_type: 'password',
					scope: 'server',
					code: '',
					randomStr: ''
				},
				showPassword: false,
				captcha: '',
				src: ''
			};
		},
		mounted() {
			this.createCode();
		},
		methods: {
			// 生成图形验证码
			createCode() {
				let randomStr = randomLenNum(6);
				this.loginForm.randomStr = randomStr;
				let urlPrefix = !!uni.getStorageSync('urlPrefix') && uni.getStorageSync('urlPrefix') != 'local' ? uni
					.getStorageSync('urlPrefix') : ipPrefix;
				let src = urlPrefix + '/mroapi/code1?randomStr=' + randomStr;
				this.src = src;
			},
			// 忘记密码
			forgetPassword() {
				uni.showToast({
					title: '请联系管理员找回密码。',
					icon: 'none',
					duration: 2000
				});
			},
			changePassword () {
				this.showPassword = !this.showPassword;
			},
			checkPassword () {
				let reg = /^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[^a-zA-Z0-9]).{8,}$/;
				if (!reg.test(this.loginForm.password)) {
					uni.setStorageSync('simplePwd', true)
				} else {
					uni.setStorageSync('simplePwd', false)
				}
			},
			// 普通登录
			login() {
				let txt = '';
				if (!this.loginForm.username) {
					txt = '请输入帐号';
				} else if (!this.loginForm.password) {
					txt = '请输入密码';
				} else if (!this.loginForm.code) {
					txt = '请输入验证码';
				}
				if (!!txt) {
					uni.showToast({
						title: txt,
						icon: 'none'
					});
					return;
				}
				// 简单密码检查
				this.checkPassword()
				uni.showLoading({
					mask: true,
					title: '登录中'
				});
				loginRequest({
						method: 'GET',
						url: 'auth-login',
						data: this.loginForm
					})
					.then(res => {
						// 重新刷新验证码
						this.createCode();
						this.loginForm.code = '';
						uni.setStorageSync('loginType', 'normal');
						uni.setStorageSync('access_token', res.access_token);
						uni.setStorageSync('refresh_token', res.refresh_token);
						uni.setStorageSync('license', res.license);
						uni.setStorageSync('username', res.username);
						uni.setStorageSync('userid', res.user_id);
						let _loginDate = new Date().getTime()
						uni.setStorageSync('loginDate', _loginDate);
						// 获取授权打印机列表
						this.getAuthPrinter();
						// 获取人员属性
						this.getUserAttributesFun(res.username);
						this.getUserInfo();
						setTimeout(() => {
							uni.hideLoading();
							uni.navigateTo({
								url: '/pages/home/homeIndex'
							});
						}, 300);
						
					})
					.catch(err => {
						uni.showToast({
							title: err.data.msg,
							icon: 'none'
						});
						uni.hideLoading();
						// 重新刷新验证码
						this.createCode();
						this.loginForm.code = '';
					});
			},
			
			getUserInfo() {
				request({
						url: 'getUserInfo',
						method: 'get'
					})
					.then(res => {
						if (res.code == 0) {
							let permissions = res.data.permissions || [];
							const list = {};
							for (let i = 0; i < permissions.length; i++) {
								list[permissions[i]] = true;
							}
							uni.setStorageSync('permissions', list);
							uni.setStorageSync('phone', res.data.sysUser.phone);
						}
					})
					.catch(err => {});
			},
			// 获取人员属性
			getUserAttributesFun(username) {
				let vm = this;
				request({
						method: 'GET',
						url: 'getUserAttributes',
						data: {
							userName: username
						}
					})
					.then(res => {
						if (res.code == 0) {
							if (!!res.data && res.data.length > 0) {
								uni.setStorageSync('deptName', res.data[0].orgName);
								// 设置工厂
								uni.setStorageSync('factoryList', res.data[0].factoryList);
								if (!!res.data[0].defaultFactory) {
									this.setDefaultFactory(res.data[0].defaultFactory);
								} else {
									uni.setStorageSync('userFactory', '');
									uni.setStorageSync('userFactoryId', '');
								}
								// 设置仓库
								if (!!res.data[0].warehouseList) {
									this.setWarehouseList(res.data[0].warehouseList, res.data[0].defaultWarehouse);
								}
								// 设置航站
								uni.setStorageSync('terminalList', res.data[0].terminalList);
								if (!!res.data[0].defaultTerminal) {
									uni.setStorageSync('userTerminal', res.data[0].defaultTerminal.tag);
									uni.setStorageSync('userTerminalValue', res.data[0].defaultTerminal.value);
								} else {
									uni.setStorageSync('userTerminal', '');
									uni.setStorageSync('userTerminalValue', '');
								}
								// 设置库存地
								uni.setStorageSync('stockLocationList', res.data[0].stockLocationList);
								if (!!res.data[0].defaultStockLocation) {
									uni.setStorageSync('userStock', res.data[0].defaultStockLocation.tag);
									uni.setStorageSync('userStockValue', res.data[0].defaultStockLocation.value);
								} else {
									uni.setStorageSync('userStock', '');
									uni.setStorageSync('userStockValue', '');
								}
							}
						}
					})
					.catch(res => {
						console.log(res);
					});
			},
			getAuthPrinter() {
				request({
						url: 'auth_printer',
						method: 'get'
					})
					.then(res => {
						if (res.code == 0) {
							uni.setStorageSync('userPrinterList', res.data);
							let lists = deepCloneNew(res.data);
							let blnDefaultPrinter = false;
							lists.forEach((item, index) => {
								if (item.defaultPrinter == true) {
									uni.setStorageSync('userDefaultPrinter', item);
									blnDefaultPrinter = true;
								}
							});
							if (!blnDefaultPrinter) {
								uni.setStorageSync('userDefaultPrinter', '');
							}
						}
					})
					.catch(err => {
						console.log(err);
					});
			},
			// 设置默认工厂
			setDefaultFactory(defaultFactory) {
				let vm = this;
				request({
						method: 'GET',
						url: 'getFactoryDict'
					})
					.then(res => {
						if (res.code == 0) {
							let ele = res.data.find(item => item.id == defaultFactory);
							if (!!ele) {
								uni.setStorageSync('userFactory', ele.label);
								uni.setStorageSync('userFactoryId', defaultFactory);
							}
						}
					})
					.catch(res => {
						console.log(res);
					});
			},
			// 设置默认仓库
			setWarehouseList(datas, defaultWarehouse) {
				let vm = this;
				request({
						method: 'GET',
						url: 'getWarehouseDict'
					})
					.then(res => {
						if (res.code == 0) {
							let warehouseList = [];
							datas.forEach(element => {
								let temp = res.data.find(item => item.id == element);
								warehouseList.push(temp);
							});
							uni.setStorageSync('warehouseList', warehouseList);
							if (!!defaultWarehouse) {
								let ele = res.data.find(item => item.id == defaultWarehouse);
								if (!!ele) {
									uni.setStorageSync('userWarehouse', ele.label);
									uni.setStorageSync('userWarehouseId', defaultWarehouse);
								}
							} else {
								uni.setStorageSync('userWarehouse', '');
								uni.setStorageSync('userWarehouseId', '');
							}
						}
					})
					.catch(res => {
						console.log(res);
					});
			}
		}
	};
</script>

<style lang="less" scoped>
	.i-login {
		width: 100%;
		height: 540rpx;
	}

	.icon-view {
		// margin-top: 200rpx;
		margin-top: 100rpx;
		width: 100%;
		text-align: center;
	}

	.app-icon {
		width: 200rpx;
		height: 200rpx;
	}

	.user-view {
		padding: 80rpx;

		.i-user,
		.i-password {
			width: 40rpx;
			height: 40rpx;
			flex-shrink: 0;
		}
	}

	.i-captcha {
		width: 150rpx;
		height: 70rpx;
	}
</style>
