<!-- 智能仓储管理系统登录页面 -->

<template>
  <div class="login-container">
    <!-- 背景装饰 -->
    <div class="background-decoration" />

    <!-- 主内容区域 -->
    <div class="login-content">
      <!-- 左侧信息区 -->
      <div class="login-left">
        <div class="system-info">
          <h1 class="system-title">智能仓储管理系统</h1>
          <div class="feature-list">
            <div class="feature-item">
              <i class="fas fa-check-circle" />
              <span>高效的库存管理</span>
            </div>
            <div class="feature-item">
              <i class="fas fa-check-circle" />
              <span>智能化物料追踪</span>
            </div>
            <div class="feature-item">
              <i class="fas fa-check-circle" />
              <span>实时数据分析</span>
            </div>
          </div>
        </div>
      </div>

      <!-- 右侧登录表单 -->
      <div class="login-right">
        <div class="login-form-wrapper">
          <div class="form-header">
            <h2 class="form-title">欢迎登录</h2>
            <p class="form-subtitle">请输入您的账号信息</p>
          </div>

          <div class="form-content">
            <!-- 用户名输入框 -->
            <div class="form-item">
              <a-input v-model:value="username" placeholder="请输入用户名" size="large" @keyup.enter="handleLogin">
                <template #prefix>
                  <UserOutlined class="input-icon" />
                </template>
              </a-input>
            </div>

            <!-- 密码输入框 -->
            <div class="form-item">
              <a-input-password v-model:value="password" placeholder="请输入密码" size="large" @keyup.enter="handleLogin">
                <template #prefix>
                  <LockOutlined class="input-icon" />
                </template>
              </a-input-password>
            </div>

            <!-- 登录按钮 -->
            <a-button type="primary" block size="large" class="login-button" :loading="loading" @click="handleLogin">
              登录
            </a-button>

            <!-- 忘记密码链接 -->
            <div class="form-footer">
              <a href="#" class="forgot-link" @click.prevent="handleForgotPassword">
                忘记密码？
              </a>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 底部版权信息 -->
    <!-- <footer class="login-footer">
      © 2023 智能仓储管理系统. 保留所有权利.
    </footer> -->
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import { message } from 'ant-design-vue';
import { loginApi } from '../api/login.js';
import { getFormattedDateTime } from '../common/Common.js';
import { UserOutlined, LockOutlined } from '@ant-design/icons-vue';

const router = useRouter();

// 表单数据
const username = ref('');
const password = ref('');
const loading = ref(false);

// 登录处理函数
const handleLogin = async () => {
    if (!username.value || !password.value) {
        message.error('请输入完整的登录信息');
        return;
    }

    loading.value = true;
    try {
        const params = {
            username: username.value,
            password: password.value,
            accountDateTime: getFormattedDateTime(),
            languageId: 'zh-CN',
        };
        const formData = new FormData();
        formData.append('userName', params.username);
        formData.append('password', params.password);
        formData.append('accountDateTime', params.accountDateTime);
        formData.append('languageId', params.languageId);
        const res = await loginApi(formData);
        if (res.errorCode === 0) {
            if (res.data) {
                localStorage.setItem('#token', res.data.token);
                localStorage.setItem('#accountId', res.data.accountId);
                localStorage.setItem('#LoginInfo', JSON.stringify(res.data));
            }
            message.success('欢迎登录，' + res.data.userName);
            const redirectUrl = window.location.href.split('redirectUrl=')[1];

            if (redirectUrl) {
                window.location = decodeURIComponent(redirectUrl);
            } else {
                router.push('/home');
            }
        } else {
            message.warning(res.errorMessage);
        }
    } catch (error) {
        message.error('登录失败');
    } finally {
        loading.value = false;
    }
};

// 忘记密码处理函数
const handleForgotPassword = () => {
    message.info('请联系管理员重置密码');
};
</script>

<style scoped>
/* 登录容器 */
.login-container {
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  position: relative;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

/* 背景装饰 */
.background-decoration {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  /* background-image: url('./background.svg'); */
  background-repeat: no-repeat;
  background-position: center;
  background-size: cover;
  opacity: 0.1;
  pointer-events: none;
}

/* 主内容区域 */
.login-content {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px 20px;
  position: relative;
  z-index: 1;
}

/* 左侧信息区 */
.login-left {
  flex: 1;
  max-width: 500px;
  padding: 0 40px;
  color: white;
}

.system-info {
  animation: fadeInLeft 0.8s ease-out;
}

.system-title {
  font-size: 42px;
  font-weight: 700;
  margin-bottom: 16px;
  line-height: 1.2;
  color: #fff
}

.system-subtitle {
  font-size: 18px;
  opacity: 0.9;
  margin-bottom: 40px;
  font-weight: 300;
}

.feature-list {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.feature-item {
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 16px;
  opacity: 0.95;
}

.feature-item i {
  font-size: 20px;
  color: #4ade80;
}

/* 右侧登录表单 */
.login-right {
  flex: 0 0 450px;
  padding: 0 20px;
}

.login-form-wrapper {
  background: white;
  border-radius: 16px;
  padding: 48px 40px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
  animation: fadeInRight 0.8s ease-out;
}

.form-header {
  text-align: center;
  margin-bottom: 32px;
}

.form-title {
  font-size: 28px;
  font-weight: 700;
  color: #1f2937;
  margin-bottom: 8px;
}

.form-subtitle {
  font-size: 14px;
  color: #6b7280;
}

.form-content {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.form-item {
  margin-bottom: 4px;
}

.input-icon {
  color: #667eea;
  font-size: 16px;
}

.login-button {
  height: 48px;
  font-size: 16px;
  font-weight: 600;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: none;
  border-radius: 8px;
  margin-top: 8px;
  transition: all 0.3s ease;
}

.login-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 16px rgba(102, 126, 234, 0.4);
}

.form-footer {
  text-align: center;
  margin-top: 8px;
}

.forgot-link {
  color: #667eea;
  font-size: 14px;
  text-decoration: none;
  transition: color 0.3s ease;
}

.forgot-link:hover {
  color: #764ba2;
  text-decoration: underline;
}

/* 底部版权 */
.login-footer {
  text-align: center;
  padding: 20px;
  color: white;
  font-size: 14px;
  opacity: 0.8;
  position: relative;
  z-index: 1;
}

/* 动画效果 */
@keyframes fadeInLeft {
  from {
    opacity: 0;
    transform: translateX(-30px);
  }

  to {
    opacity: 1;
    transform: translateX(0);
  }
}

@keyframes fadeInRight {
  from {
    opacity: 0;
    transform: translateX(30px);
  }

  to {
    opacity: 1;
    transform: translateX(0);
  }
}

/* 响应式设计 */
@media (max-width: 1024px) {
  .login-left {
    display: none;
  }

  .login-right {
    flex: 0 0 100%;
    max-width: 450px;
  }
}

@media (max-width: 640px) {
  .login-form-wrapper {
    padding: 32px 24px;
  }

  .form-title {
    font-size: 24px;
  }

  .login-right {
    padding: 0 16px;
  }
}

/* Ant Design 样式覆盖 */
:deep(.ant-input-affix-wrapper) {
  border-radius: 8px;
  border: 1px solid #e5e7eb;
  padding: 12px 16px;
}

:deep(.ant-input-affix-wrapper:focus),
:deep(.ant-input-affix-wrapper-focused) {
  border-color: #667eea;
  box-shadow: 0 0 0 2px rgba(102, 126, 234, 0.1);
}

:deep(.ant-input) {
  font-size: 15px;
}

:deep(.ant-input-password) {
  border-radius: 8px;
}
</style>
