<template>
	<view class="login">
		<view class="login-content">
			<view class="login-title">账号登录</view>
			<uni-forms ref="formRef" :modelValue="formData" :rules="formRules" class="login-form">
				<view class="form-title">姓名</view>
				<uni-forms-item name="username">
					<uni-easyinput class="pub-input" trim="all" v-model="formData.username" placeholder="请输入姓名"></uni-easyinput>
				</uni-forms-item>
				<view class="form-title">密码</view>
				<uni-forms-item name="password">
					<uni-easyinput type="password" class="pub-input" trim="all" v-model="formData.password" placeholder="请输入密码"></uni-easyinput>
				</uni-forms-item>
			</uni-forms>
			<view class="pub-button is-bg large" @click="submit">登录</view>
		</view>	
	</view>
</template>

<script lang="ts" setup>
	import { ref, reactive } from 'vue'
	import http from '@/utils/request';
	import { encrypt, decrypt } from "@/lib/gmCrypt"

	const formData = ref({
		username: '',
		password: '',
	})
	const formRef = ref()
	const formRules = reactive({
		username: {
			rules: [{
				required: true,
				errorMessage: '账号不能为空'
			}]
		},
		password: {
			rules: [{
				required: true,
				errorMessage: '密码不能为空'
			}]
		},
	})


	const submit = async () => {
		// 校验表单
		formRef.value.validate().then(async res => {
			// 提交请求
			try {
				  const transitData = {
				      username: formData.value.username,
				      password: encrypt(formData.value.password),
				    }
					// cloud/api/admin-api/system/auth/login
				http.post("admin-api/system/auth/login",transitData).then(data=>{
					console.log('提示：', data);
					uni.navigateTo({
						url: '../index/index?token=' + data.accessToken+'&isLogin=true'
					});
				})
			} catch (e) {
				console.log('表单错误信息：', e);
			}
		}).catch(err => {
			console.log('表单错误信息：', err);
		})
	}
</script>

<style lang="scss" scoped>
	.login {
		height: 100vh;
		width: 100%;
		background-image: url("~@/static/logo_bg.png");
		background-repeat: no-repeat;
		background-position: center;
		width: 100%;
		background-size: cover;
		display: flex;
		justify-content: center;
		align-items: center;
		justify-content: center;
		position: relative;

		.login-content {
			position: relative;
			top: -80px;
			background: #FFFFFF;
			border-radius: 4rpx;
			width: calc(100% - 128rpx);
			height: 642rpx;
			border-radius: 12px;
			padding: 40rpx 32rpx 16rpx;
			box-sizing: border-box;

			.login-title {
				text-align: center;
				font-size: 36rpx;
				line-height: 50rpx;
			}
			.login-form {
				margin-top: 10px;
			}

			.pub-input {
				:deep(.uni-easyinput__content) {
					border-color: #E8E9EC !important;
					border-radius: 4rpx;

					.uni-easyinput__content-input {
						height: 88rpx;
						font-size: 32rpx;
						line-height: 44rpx;
						color: #363A44;

						.uni-input-placeholder {
							color: #B3B5B9;
							font-size: 32rpx;
							line-height: 44rpx;
						}
					}
				}


			}

			.form-title {
				font-size: 32rpx;
				color: #363A44;
				margin-bottom: 16rpx;
				line-height: 44rpx;
			}

			.pub-button {
				margin-top: 50rpx;
				min-width: 120rpx;
				height: 72rpx;
				font-size: 32rpx;
				line-height: 44rpx;
				border-radius: 36rpx;
				padding: 0 32rpx;
				display: flex;
				justify-content: center;
				align-items: center;
				color: #1E92F0;
				border: 1px solid #1E92F0;
			}
		}
	}
</style>