<template>
	<view class="login-container">
		<image class="logo" src="/static/login.png"></image>
		<view class="login-card">
			<text class="login-title">欢迎登录</text>
			<u-input v-model="username" placeholder="请输入用户名" class="input-field"></u-input>
			<u-input v-model="password" placeholder="请输入密码" type="password" class="input-field"></u-input>
			<u-button type="primary" @click="handleLogin" class="login-button">登录</u-button>
		</view>
	</view>
</template>

<script>
	import request from '@/utils/request';
	export default {
		data() {
			return {
				username: '',
				password: ''
			}
		},
		methods: {
			formatTime(timestamp) {
				const date = new Date(timestamp);
				const year = date.getFullYear();
				const month = String(date.getMonth() + 1).padStart(2, '0');
				const day = String(date.getDate()).padStart(2, '0');
				const hours = String(date.getHours()).padStart(2, '0');
				const minutes = String(date.getMinutes()).padStart(2, '0');
				const seconds = String(date.getSeconds()).padStart(2, '0');
				return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
			},
			async handleLogin() {
				if (!this.username || !this.password) {
					uni.showToast({
						title: '请输入用户名和密码',
						icon: 'none'
					});
					return;
				}

				try {
					const res = await request({
						url: '/admin-api/system/auth/login',
						method: 'POST',
						data: {
							username: this.username,
							password: this.password
						}
					});
					console.log(res);


					if (res.data.code === 200) {
						// 存储 token
						uni.setStorageSync('HANDLE_KEY', res.data.data.accessToken);
						uni.switchTab({
							url: '/pages/tabbar/video/video'
						});
					} else {
						uni.showToast({
							title: res.data.msg || '登录失败',
							icon: 'none'
						});
					}
				} catch (error) {
					console.error('登录失败:', error);
					uni.showToast({
						title: '网络错误',
						icon: 'none'
					});
				}
			}
		}
	}
</script>

<style>
	.login-container {
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		height: 100vh;
		background-image: url('/static/pic02.jpg');
		background-size: cover;
		background-position: center;
	}

	.logo {
		height: 150px;
		width: 150px;
		margin-bottom: 30px;
	}

	.login-card {
		width: 80%;
		padding: 20px;
		background-color: rgba(255, 255, 255, 0.9);
		border-radius: 10px;
		box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
		transform: translateY(10%);
	}

	.login-title {
		font-size: 24px;
		font-weight: bold;
		margin-bottom: 20px;
		display: block;
		text-align: center;
	}

	.input-field {
		margin-bottom: 15px;
	}

	.login-button {
		margin-top: 15px;
	}

	.login-title {
		font-size: 24px;
		font-weight: bold;
		margin-bottom: 20px;
		display: block;
		text-align: center;
	}

	.input-field {
		margin-bottom: 15px;
	}

	.login-button {
		margin-top: 15px;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}
</style>