<template>
	<view class="content">
		<view class="head">
			<text>登录</text>
		</view>
		<view class="login-wrap">
			<view class="username item">
				<text>账号：</text>
				<uni-easyinput :inputBorder="false" class="login_input" type="text" v-model="username" placeholder="请输入账号" />
			</view>
			<view class="password item">
				<text>密码：</text>
				<uni-easyinput :inputBorder="false" class="login_input" type="password" v-model="password" placeholder="请输入密码">
				</uni-easyinput>
			</view>
			<view class="tip">
				<label>
					<checkbox style="transform:scale(0.7)" class="check" value="cb" checked="true" />
					<text>我已阅读并同意《数字管家用户协议》和《数字管家隐私政策》，且已明确知晓平台仅限企业内部用户使用。			</text>
				</label>
			</view>
			<button type="primary" @click="loginSubmit" class="submit-BT">登录</button>
		</view>
	</view>
</template>

<script>
	import store from '@/store/index.js'
	import { login } from '@/api/system/user.js';
	import { setToken } from '@/libs/auth.js';	
	export default {
		data() {
			return {
				username: "",
				password: "",
			}
		},
		onLoad({username,password}) {
			if(username){
				this.username=username;
				this.password=password;
			}
		},
		methods: {		
			loginSubmit() {	
				let username = (this.username).trim();
				let password = (this.password).trim();
				if (username.length < 1) {
					uni.showToast({
						title: '请填写账号',
						icon: "error"
					})
					return;
				}
				if (password.length < 1) {
					uni.showToast({
						title: '请填写密码',
						icon: "error"
					})
					return;
				}
				login({
					username,
					password
				}).then((res) => {
					let token = res.data.accessToken;
					let accountInfo = JSON.stringify(res.data.user);
					const userId=res.data.user.userId;
					setToken(token);
					uni.setStorageSync('accountInfo', accountInfo);
					store.commit('setToken', token);
					uni.reLaunch({
						url: '/pages/index/index'
					})				
				}).catch((res)=>{
					console.log(res)
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		height: 100vh;
		background-color: #F5F6F8;
		.head {
			font-size: 50upx;
			text-align: center;
			padding: 40upx 0;
			letter-spacing: 2upx;
		}

		.login-wrap {
			width: 90%;
			margin: 50upx auto 0;
			box-sizing: border-box;
			padding: 20upx;
			border-radius: 8upx;
			.item {
				display: flex;
				justify-content: flex-start;
				align-items: center;
				height: 100upx;
				font-size: 32upx;
				line-height: 100upx;
				background-color: #fff;
				border-radius: 60rpx;
				margin-top: 40rpx;
				padding-left: 30rpx;
				box-sizing: border-box;
				input {
					font-size: 32upx;
					height: 100upx;
					border: 0;
				}

				.login_input {
					width: 580upx;
					border: none;
				}
			}
			.tip {
				padding: 48rpx 0 0 56rpx;
				font-size: 28upx;
				color: #323233;
				position: relative;
				.check{
					position: absolute;
					left: 8rpx;
					top: 44rpx;
				}
			}

			.submit-BT {
				height: 88rpx;	
				line-height: 88rpx;
				border-radius: 50rpx;
				margin-top: 50upx;
				background-color:#2A5BEE;
				padding: 0;
			}
		}
	}
</style>
