<template>
	<view class="content">
		<view class="logo">
			<image style="height:104rpx;width:104rpx" src="/static/czzn-img/login/logo.png" mode="aspectFit" />
			<view class="app-name">{{$t('login.e')}}</view>
		</view>
		<!-- 账号密码登录 -->
		<template v-if="loginType == 1">
			<view class="inp_box">
				<input type="text" v-model="loginForm.username"  @blur="getTenantUsername"
					:placeholder="this.$t('login.placeholder')" />
			</view>
			<view class="inp_box">
				<input type="password" v-model="loginForm.password"
					:placeholder="this.$t('login.passwordPlaceholder')" />
			</view>
		</template>
		<!-- 阅读同意/忘记密码 -->
		<view class="agree-forget">
			<view class="agree">
				<uni-data-checkbox selectedColor="#3275F5" multiple v-model="agree" checked=true :localdata="option">
				</uni-data-checkbox>
				<span>{{ $t("login.agree") }}</span>
			</view>
			<!-- <span class="forget" @click="toReset" v-if="loginType == 1">{{ $t('login.forgetPassword') }}</span> -->
		</view>

		<!-- 登录按钮 -->
		<view class="btn-wrap">
			<u-button type="primary" shape="circle" :disabled="!agree[0]"
				@click="loginType == 1 ? onAccountLogin() : onPhoneLogin()">{{ $t('login.login') }}</u-button>
		</view>
	</view>
</template>

<script>
	import {
		userLogin,
		getTenantByUsername,
		getUserInfo
	} from "../../api/login"
	import {
		encryption,
		getTenantCacheList
	} from '../../utils/util'
	import {
		mapMutations
	} from 'vuex';
	export default {
		data() {
			return {
				time: 0,
				timer: undefined,
				isGetCode: false,
				loginType: 1,
				agree: [1],
				option: [{
					text: '',
					value: 1,
				}],
				currentLanguage: '',
				canLogin: false,
				needCode:false,
				loginForm: {
					username: "",
					password: "",
					code: "",
					randomStr: "blockPuzzle",
				},
				phoneLoginForm: {
					mobile: "",
					code: "",
				},
			}
		},
		onLoad(options) {
			if (uni.getStorageSync('CURRENT_LANG') == "en") {
				this.currentLanguage = 'English';
			} else if (uni.getStorageSync('CURRENT_LANG') == "pt") {
				this.currentLanguage = 'Português';
			} else if (uni.getStorageSync('CURRENT_LANG') == "uz") {
				this.currentLanguage = "O'zbekcha";
			} else if (uni.getStorageSync('CURRENT_LANG') == "ru") {
				this.currentLanguage = "Russian";
			}else {
				this.currentLanguage = '简体中文';
			}
			if (options.username) {
				this.loginForm.username = options.username
				this.phoneLoginForm.mobile = options.username
				this.getTenantUsername()
			}

		},
		methods: {
			...mapMutations({
				setUserInfo: 'user/login'
			}),
			//账号登录
			onAccountLogin() {
				console.log("登录信息、", this.loginForm);
				if (this.noRegisterUserName == this.loginForm.username) {
					uni.showModal({
						content: this.$t('login.noRegister'),
						showCancel: false,
					})
					return
				}
				
				if (this.loginForm.username == '') {
					uni.showToast({
						title: this.$t('login.placeholder'),
						icon: "none",
						duration: 2500,
					})
				} else if (this.loginForm.password == '') {
					uni.showToast({
						title: this.$t('login.passwordPlaceholder'),
						icon: "none",
						duration: 2500,
					})
				} else {
					const userInfo = encryption({
						data: {
							username: this.loginForm.username,
							password: this.loginForm.password,
							delFlag: '0',
							source: 'mini'
						},
						key: 'pigxpigxpigxpigx',
						param: ['password'],
						source: 'mini'
					})
					userLogin(userInfo).then(res => {
						if (res.statusCode === 200) {
							var userId = res.data.user_id
							if (!userId) {
								userId = res.data.user_info.id
							}
							uni.setStorageSync('userId', userId)
							uni.setStorageSync('userName', this.loginForm.username)
							uni.setStorageSync('token', res.data.access_token)
							this.onGetUserInfo(true)
						}
					})
				}
			},
			//获取用户详情
			onGetUserInfo() {
				getUserInfo().then(res => {
					if (res.statusCode == 200 && res.data.code == 0) {
						console.log(res);
						var userType = res.data.data.sysUser.type;
						uni.setStorageSync('userInfo', res.data.data.sysUser)
						uni.setStorageSync('tenantInfo', res.data.data.tenantList)
							getApp().globalData.tabIndex = 'home'
							uni.switchTab({
								url: "/pages/home/home"
							})
						uni.showToast({
							title: this.$t('login.success'),
							icon: "none",
							duration: 2500,
						})
					} else if (res.statusCode != 200 || res.data.code === 1) {
						uni.showToast({
							title: res.data.error || res.data.msg,
							icon: 'none',
							duration: 2000
						})
					}
				})
			}, 
			
			//获取租户
			getTenantUsername() {
				if (
					!this.loginForm.username.length ||
					this.loginForm.username.length == 0
				) {
					return false;
				}
				getTenantByUsername(this.loginForm.username).then(response => {
					let tenantList = response.data.data ? response.data.data : [];
					if(tenantList.length!=0){
						uni.setStorageSync('tenantId', tenantList[0].id);
						uni.setStorageSync('tenantName', tenantList[0].name);
					}else{
						uni.showToast({
							title: "获取租户失败",
							icon: 'none',
							duration: 2000
						});
					}
				});
			},
		}
	}
</script>
<style>
	/* #ifndef APP-NVUE */
	view {
		display: flex;
		box-sizing: border-box;
		flex-direction: column;
	}

	page {
		box-sizing: border-box;
		padding: 0 48rpx 80rpx;
		height: 100%;
	}

	/* #endif*/
</style>
<style lang="scss" scoped>
	@import '../../common/css/login.scss';
</style>