<template>
  <div class="login-container">
    <div class="login-bg">
      <div class="bg-shape bg-shape-1"></div>
      <div class="bg-shape bg-shape-2"></div>
      <div class="bg-shape bg-shape-3"></div>
    </div>
    
    <div class="login-card">
      <div class="login-header">
        <div class="logo">
          <el-icon :size="48" color="#667eea">
            <Notebook />
          </el-icon>
        </div>
        <h1 class="title">考培练系统</h1>
        <p class="subtitle">让学习更高效，让进步看得见</p>
      </div>

      <el-form ref="formRef" :model="loginForm" :rules="rules" class="login-form">
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="请输入用户名"
            size="large"
            clearable
          >
            <template #prefix>
              <el-icon><User /></el-icon>
            </template>
          </el-input>
        </el-form-item>

        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="请输入密码"
            size="large"
            show-password
            @keyup.enter="handleLogin"
          >
            <template #prefix>
              <el-icon><Lock /></el-icon>
            </template>
          </el-input>
        </el-form-item>

        <el-form-item>
          <div class="login-options">
            <el-checkbox v-model="loginForm.remember">记住我</el-checkbox>
            <el-link type="primary" :underline="false" @click="forgotPassword">
              忘记密码？
            </el-link>
          </div>
        </el-form-item>

        <el-form-item>
          <el-button
            type="primary"
            size="large"
            class="login-btn"
            :loading="loading"
            @click="handleLogin"
          >
            登 录
          </el-button>
        </el-form-item>

        <div class="other-login">
          <el-divider>其他登录方式</el-divider>
          <div class="social-icons">
            <div class="social-icon" @click="socialLogin('wechat')">
              <el-icon :size="20"><ChatDotRound /></el-icon>
            </div>
            <div class="social-icon" @click="socialLogin('qq')">
              <el-icon :size="20"><Connection /></el-icon>
            </div>
            <div class="social-icon" @click="socialLogin('github')">
              <el-icon :size="20"><Link /></el-icon>
            </div>
          </div>
        </div>

        <div class="register-link">
          还没有账号？
          <el-link type="primary" :underline="false" @click="goRegister">
            立即注册
          </el-link>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import type { FormInstance, FormRules } from 'element-plus'
import { login } from '@/api/auth'
import { authManager } from '@/utils/auth'

const router = useRouter()
const formRef = ref<FormInstance>()
const loading = ref(false)

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

// 表单验证规则
const rules = reactive<FormRules>({
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '用户名长度在 3 到 20 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能少于 6 个字符', trigger: 'blur' }
  ]
})

/**
 * 登录
 */
const handleLogin = async () => {
  if (!formRef.value) return
  
  await formRef.value.validate(async (valid) => {
    if (valid) {
      loading.value = true
      
      try {
        // 调用真实的登录API
        const response = await login({
          username: loginForm.username,
          password: loginForm.password
        })
        
        if (response.code === 200) {
          // 使用authManager保存认证信息
          authManager.setAccessToken(response.data.token.access_token)
          authManager.setRefreshToken(response.data.token.refresh_token)
          
          // 添加缺少的字段
          const userInfo = {
            ...response.data.user,
            created_at: response.data.user.created_at || new Date().toISOString(),
            updated_at: response.data.user.updated_at || new Date().toISOString()
          }
          authManager.setCurrentUser(userInfo)

          ElMessage.success('登录成功')

          // 跳转到用户默认页面或指定的重定向页面
          const redirect = new URLSearchParams(window.location.search).get('redirect') || authManager.getDefaultRoute()
          router.push(redirect)
        } else {
          ElMessage.error(response.message || '登录失败')
        }
      } catch (error: any) {
        console.error('登录错误:', error)
        ElMessage.error(error.message || '登录失败，请稍后重试')
      } finally {
        loading.value = false
      }
    }
  })
}

/**
 * 忘记密码
 */
const forgotPassword = () => {
  ElMessage.info('请联系管理员重置密码')
}

/**
 * 社交登录
 */
const socialLogin = (type: string) => {
  ElMessage.info(`${type} 登录功能开发中`)
}

/**
 * 去注册
 */
const goRegister = () => {
  ElMessage.info('注册功能开发中')
}
</script>

<style lang="scss" scoped>
.login-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  position: relative;
  overflow: hidden;

  .login-bg {
    position: absolute;
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;

    .bg-shape {
      position: absolute;
      border-radius: 50%;
      filter: blur(40px);
      opacity: 0.6;
      animation: float 20s infinite ease-in-out;

      &-1 {
        width: 400px;
        height: 400px;
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        top: -200px;
        right: -100px;
      }

      &-2 {
        width: 300px;
        height: 300px;
        background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
        bottom: -150px;
        left: -100px;
        animation-delay: -5s;
      }

      &-3 {
        width: 350px;
        height: 350px;
        background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        animation-delay: -10s;
      }
    }
  }

  .login-card {
    background: rgba(255, 255, 255, 0.95);
    backdrop-filter: blur(10px);
    border-radius: 20px;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
    padding: 48px;
    width: 420px;
    position: relative;
    z-index: 1;

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

      .logo {
        margin-bottom: 16px;
      }

      .title {
        font-size: 28px;
        font-weight: 600;
        color: #333;
        margin-bottom: 8px;
      }

      .subtitle {
        font-size: 14px;
        color: #666;
      }
    }

    .login-form {
      .login-options {
        display: flex;
        justify-content: space-between;
        align-items: center;
        width: 100%;
      }

      .login-btn {
        width: 100%;
        height: 44px;
        font-size: 16px;
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        border: none;
        border-radius: 8px;
        letter-spacing: 2px;
      }

      .other-login {
        margin-top: 32px;

        :deep(.el-divider__text) {
          color: #999;
          font-size: 13px;
        }

        .social-icons {
          display: flex;
          justify-content: center;
          gap: 24px;
          margin-top: 24px;

          .social-icon {
            width: 44px;
            height: 44px;
            border: 1px solid #e4e7ed;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            cursor: pointer;
            transition: all 0.3s ease;

            &:hover {
              border-color: #667eea;
              color: #667eea;
              transform: translateY(-2px);
            }
          }
        }
      }

      .register-link {
        text-align: center;
        margin-top: 24px;
        font-size: 14px;
        color: #666;
      }
    }
  }
}

@keyframes float {
  0%, 100% {
    transform: translateY(0) rotate(0deg);
  }
  33% {
    transform: translateY(-30px) rotate(120deg);
  }
  66% {
    transform: translateY(30px) rotate(240deg);
  }
}

// 响应式
@media (max-width: 768px) {
  .login-container {
    padding: 20px;

    .login-card {
      width: 100%;
      max-width: 400px;
      padding: 32px 24px;
    }
  }
}
</style>