<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-header">
        <div class="logo">
          <bank-outlined />
        </div>
        <h1 class="title">银行管理后台系统</h1>
        <p class="subtitle">专业的银行管理解决方案</p>
      </div>
      
      <a-form
        :model="loginForm"
        :rules="loginRules"
        @finish="handleLogin"
        class="login-form"
        size="large"
      >
        <a-form-item name="username">
          <a-input
            v-model:value="loginForm.username"
            placeholder="请输入用户名"
            :prefix="h(UserOutlined)"
          />
        </a-form-item>
        
        <a-form-item name="password">
          <a-input-password
            v-model:value="loginForm.password"
            placeholder="请输入密码"
            :prefix="h(LockOutlined)"
          />
        </a-form-item>
        
        <a-form-item>
          <a-checkbox v-model:checked="loginForm.remember">
            记住密码
          </a-checkbox>
        </a-form-item>
        
        <a-form-item>
          <a-button
            type="primary"
            html-type="submit"
            :loading="loading"
            block
            size="large"
          >
            登录
          </a-button>
        </a-form-item>
      </a-form>
      
      <div class="login-footer">
        <p>默认账户：admin / 123456</p>
        <p>测试账户：testuser / Test123456</p>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, h } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { message } from 'ant-design-vue'
import { useUserStore } from '@/stores'
import { 
  BankOutlined, 
  UserOutlined, 
  LockOutlined 
} from '@ant-design/icons-vue'

const router = useRouter()
const route = useRoute()
const userStore = useUserStore()

// 登录表单
const loginForm = ref({
  username: '',
  password: '',
  remember: false
})

// 加载状态
const loading = ref(false)

// 表单验证规则
const loginRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 50, message: '用户名长度在3到50个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '密码长度在6到20个字符', trigger: 'blur' }
  ]
}

// 处理登录
const handleLogin = async () => {
  try {
    loading.value = true
    
    const result = await userStore.login(
      loginForm.value.username,
      loginForm.value.password
    )
    
    if (result.success) {
      message.success('登录成功')
      
      // 重定向到目标页面或项目列表
      const redirectPath = route.query.redirect || '/project-list'
      router.push(redirectPath)
    } else {
      message.error(result.message || '登录失败')
    }
  } catch (error) {
    console.error('登录错误:', error)
    message.error(error.message || '登录失败，请检查网络连接')
  } finally {
    loading.value = false
  }
}

// 页面加载时检查是否已登录
if (userStore.isLoggedIn) {
  const redirectPath = route.query.redirect || '/project-list'
  router.push(redirectPath)
}
</script>

<style scoped>
.login-container {
  position: relative;
  width: 100vw;
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #f0f2f5;
  overflow: hidden;
}

.login-box {
  position: relative;
  z-index: 2;
  width: 400px;
  padding: 40px;
  background: #ffffff;
  border-radius: 12px;
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.08);
}

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

.logo {
  font-size: 48px;
  color: #1890ff;
  margin-bottom: 16px;
}

.title {
  font-size: 24px;
  font-weight: 600;
  color: #262626;
  margin: 0 0 8px 0;
}

.subtitle {
  font-size: 14px;
  color: #8c8c8c;
  margin: 0;
}

.login-form {
  margin-bottom: 24px;
}

.login-form .ant-form-item {
  margin-bottom: 20px;
}

.login-form .ant-input,
.login-form .ant-input-password {
  height: 48px;
  border-radius: 8px;
  border: 1px solid #d9d9d9;
  font-size: 16px;
}

.login-form .ant-input:focus,
.login-form .ant-input-password:focus {
  border-color: #1890ff;
  box-shadow: 0 0 0 2px rgba(24, 144, 255, 0.2);
}

.login-form .ant-btn {
  height: 48px;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 600;
  background: #1890ff;
  border-color: #1890ff;
}

.login-form .ant-btn:hover {
  background: #40a9ff;
  border-color: #40a9ff;
}

.login-footer {
  text-align: center;
  padding-top: 16px;
  border-top: 1px solid #f0f0f0;
}

.login-footer p {
  margin: 4px 0;
  font-size: 12px;
  color: #8c8c8c;
}

/* 移除渐变与动画背景，仅保留纯色背景 */

/* 响应式设计 */
@media (max-width: 768px) {
  .login-box {
    width: 90%;
    max-width: 400px;
    padding: 24px;
    margin: 16px;
  }
  
  .title {
    font-size: 20px;
  }
  
  .logo {
    font-size: 40px;
  }
}

@media (max-width: 480px) {
  .login-box {
    padding: 20px;
  }
  
  .title {
    font-size: 18px;
  }
  
  .logo {
    font-size: 36px;
  }
  
  .login-form .ant-input,
  .login-form .ant-input-password,
  .login-form .ant-btn {
    height: 44px;
    font-size: 14px;
  }
}
</style>