<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-header">
        <div class="logo-wrapper">
          <img src="@/assets/qingluan.png" alt="Logo" class="login-logo" />
        </div>
        <h2 class="login-title">婚恋管理系统</h2>
        <p class="login-subtitle">Marriage Management System</p>
      </div>
      
      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
        @submit.prevent="handleLogin"
      >
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="请输入用户名"
            size="large"
            prefix-icon="User"
            clearable
          />
        </el-form-item>
        
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="请输入密码"
            size="large"
            prefix-icon="Lock"
            show-password
            @keyup.enter="handleLogin"
          />
        </el-form-item>
        
        <el-form-item>
          <el-button
            type="primary"
            size="large"
            :loading="loading"
            class="login-button"
            @click="handleLogin"
          >
            {{ loading ? '登录中...' : '登录' }}
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { ElMessage } from 'element-plus'

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

const loginFormRef = ref(null)
const loading = ref(false)

const loginForm = reactive({
  username: '',
  password: ''
})

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

const handleLogin = async () => {
  if (!loginFormRef.value) return
  
  try {
    await loginFormRef.value.validate()
    loading.value = true
    
    const success = await userStore.login(loginForm.username, loginForm.password)
    
    if (success) {
      ElMessage.success('登录成功')
      router.push('/')
    } else {
      ElMessage.error('登录失败，请检查用户名和密码')
    }
  } catch (error) {
    console.error('登录验证失败:', error)
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
/* ==================== 登录容器 ==================== */
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #a8c0ff 0%, #3f2b96 50%, #a8c0ff 100%);
  background-size: 200% 200%;
  animation: gradientShift 15s ease infinite;
  padding: var(--spacing-xl);
  position: relative;
  overflow: hidden;
}

@keyframes gradientShift {
  0% {
    background-position: 0% 50%;
  }
  50% {
    background-position: 100% 50%;
  }
  100% {
    background-position: 0% 50%;
  }
}

/* 背景动画装饰 */
.login-container::before {
  content: '';
  position: absolute;
  width: 200%;
  height: 200%;
  top: -50%;
  left: -50%;
  background: radial-gradient(circle, rgba(255, 255, 255, 0.1) 1px, transparent 1px);
  background-size: 50px 50px;
  animation: backgroundMove 20s linear infinite;
  pointer-events: none;
}

@keyframes backgroundMove {
  0% {
    transform: translate(0, 0);
  }
  100% {
    transform: translate(50px, 50px);
  }
}

/* 登录框 */
.login-box {
  width: 100%;
  max-width: 480px;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 24px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.15),
              0 0 0 1px rgba(255, 255, 255, 0.5) inset;
  padding: 48px 40px;
  backdrop-filter: blur(20px);
  animation: slideInUp 0.6s ease-out;
  position: relative;
  z-index: 1;
  overflow: hidden;
}

.login-box::after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 4px;
  background: linear-gradient(90deg, #a8c0ff, #3f2b96, #a8c0ff);
  background-size: 200% 100%;
  animation: shimmer 3s linear infinite;
}

@keyframes shimmer {
  0% {
    background-position: -200% 0;
  }
  100% {
    background-position: 200% 0;
  }
}

@keyframes slideInUp {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes fadeInDown {
  from {
    opacity: 0;
    transform: translateY(-20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* 登录头部 */
.login-header {
  text-align: center;
  margin-bottom: var(--spacing-3xl);
  animation: fadeInDown 0.8s ease-out;
}

.logo-wrapper {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 120px;
  height: 120px;
  margin: 0 auto var(--spacing-lg);
  background: linear-gradient(135deg, rgba(255, 255, 255, 0.2) 0%, rgba(255, 255, 255, 0.05) 100%);
  border-radius: 50%;
  padding: 20px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1),
              inset 0 2px 8px rgba(255, 255, 255, 0.3);
  backdrop-filter: blur(10px);
  border: 2px solid rgba(255, 255, 255, 0.2);
  position: relative;
  overflow: hidden;
}

.logo-wrapper::before {
  content: '';
  position: absolute;
  top: -50%;
  left: -50%;
  width: 200%;
  height: 200%;
  background: radial-gradient(circle, rgba(255, 255, 255, 0.3) 0%, transparent 70%);
  animation: rotate 8s linear infinite;
}

@keyframes rotate {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}

.login-logo {
  width: 100%;
  height: 100%;
  object-fit: contain;
  filter: drop-shadow(0 4px 12px rgba(0, 0, 0, 0.15));
  animation: float 3s ease-in-out infinite;
  position: relative;
  z-index: 1;
}

@keyframes float {
  0%, 100% {
    transform: translateY(0px) scale(1);
  }
  50% {
    transform: translateY(-8px) scale(1.02);
  }
}

@keyframes pulse {
  0%, 100% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.05);
  }
}

.login-title {
  font-size: 32px;
  font-weight: 700;
  color: var(--text-primary);
  margin: 0 0 var(--spacing-sm) 0;
  background: linear-gradient(135deg, #3f2b96 0%, #a8c0ff 50%, #3f2b96 100%);
  background-size: 200% 100%;
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  animation: textShimmer 3s ease-in-out infinite;
  letter-spacing: 1px;
}

@keyframes textShimmer {
  0%, 100% {
    background-position: 0% 50%;
  }
  50% {
    background-position: 100% 50%;
  }
}

.login-subtitle {
  font-size: var(--font-sm);
  color: var(--text-tertiary);
  margin: 0;
  letter-spacing: 1px;
  text-transform: uppercase;
}

/* 登录表单 */
.login-form {
  margin-top: var(--spacing-xl);
  animation: fadeInUp 1s ease-out;
}

/* 表单项增强 */
.login-form :deep(.el-form-item) {
  margin-bottom: 12px;
}

/* 登录按钮容器增加上间距 */
.login-form :deep(.el-form-item:last-child) {
  margin-top: 28px;
  margin-bottom: 0;
}

.login-form :deep(.el-input__wrapper) {
  padding: var(--spacing-base) var(--spacing-lg);
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
  border: 1px solid rgba(168, 192, 255, 0.3);
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.9);
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.login-form :deep(.el-input__wrapper:hover) {
  box-shadow: 0 4px 20px rgba(63, 43, 150, 0.2);
  border-color: rgba(168, 192, 255, 0.6);
  transform: translateY(-2px);
}

.login-form :deep(.el-input__wrapper.is-focus) {
  box-shadow: 0 6px 24px rgba(63, 43, 150, 0.3);
  border-color: rgba(168, 192, 255, 0.8);
  background: rgba(255, 255, 255, 1);
}

.login-form :deep(.el-input__inner) {
  font-size: var(--font-md);
  color: var(--text-primary);
}

.login-form :deep(.el-input__prefix) {
  color: var(--text-tertiary);
  font-size: 18px;
}

/* 登录按钮 */
.login-button {
  width: 100%;
  height: 52px;
  font-size: 16px;
  font-weight: 600;
  background: linear-gradient(135deg, #3f2b96 0%, #a8c0ff 100%);
  border: none;
  border-radius: 12px;
  box-shadow: 0 6px 20px rgba(63, 43, 150, 0.4),
              0 0 0 0 rgba(168, 192, 255, 0.4);
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  position: relative;
  overflow: hidden;
  letter-spacing: 1px;
}

.login-button::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.3), transparent);
  transition: left 0.5s;
}

.login-button:hover::before {
  left: 100%;
}

.login-button:hover {
  transform: translateY(-3px);
  box-shadow: 0 8px 28px rgba(63, 43, 150, 0.5),
              0 0 0 4px rgba(168, 192, 255, 0.2);
  background: linear-gradient(135deg, #4a3ba8 0%, #b8d0ff 100%);
}

.login-button:active {
  transform: translateY(-1px);
  box-shadow: 0 4px 16px rgba(63, 43, 150, 0.4);
}

/* ==================== 响应式设计 ==================== */

/* 平板设备 */
@media (max-width: 768px) {
  .login-container {
    padding: var(--spacing-lg);
  }

  .login-box {
    max-width: 100%;
    padding: var(--spacing-2xl);
  }

  .logo-wrapper {
    width: 100px;
    height: 100px;
    padding: 15px;
  }

  .login-title {
    font-size: var(--font-2xl);
  }

  .login-button {
    height: 44px;
  }
}

/* 小屏幕移动设备 */
@media (max-width: 480px) {
  .login-box {
    padding: var(--spacing-xl);
  }

  .login-header {
    margin-bottom: var(--spacing-xl);
  }

  .login-form :deep(.el-form-item) {
    margin-bottom: var(--spacing-lg);
  }
}

/* 超宽屏幕 */
@media (min-width: 1920px) {
  .login-box {
    max-width: 500px;
    padding: var(--spacing-3xl) 48px;
  }
}
</style>

