<template>
  <div class="callback-container">
    <el-card class="callback-box">
      <div v-if="loading" class="loading-content">
        <el-icon class="is-loading" size="48"><Loading /></el-icon>
        <h2>正在处理登录...</h2>
        <p>请稍候，系统正在验证您的身份</p>
      </div>
      
      <div v-else-if="error" class="error-content">
        <el-icon size="48" color="#F56C6C"><Warning /></el-icon>
        <h2>登录失败</h2>
        <p>{{ error }}</p>
        <el-button type="primary" @click="$router.push('/login')">
          重新登录
        </el-button>
      </div>
      
      <div v-else class="success-content">
        <el-icon size="48" color="#67C23A"><SuccessFilled /></el-icon>
        <h2>登录成功</h2>
        <p>正在跳转到系统首页...</p>
      </div>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ElMessage } from 'element-plus'
import { useAuthStore } from '@/stores/auth'

const router = useRouter()
const route = useRoute()
const authStore = useAuthStore()

const loading = ref(true)
const error = ref('')

onMounted(async () => {
  try {
    // 从URL参数获取token
    const token = route.query.token as string
    const errorParam = route.query.error as string
    
    if (errorParam) {
      error.value = '授权失败: ' + errorParam
      loading.value = false
      return
    }
    
    if (!token) {
      error.value = '未收到访问令牌'
      loading.value = false
      return
    }
    
    // 保存token
    authStore.setToken(token)
    
    // 获取用户信息
    await authStore.fetchUserInfo()
    
    loading.value = false
    
    ElMessage.success('登录成功')
    
    // 延迟跳转，让用户看到成功提示
    setTimeout(() => {
      router.push('/dashboard')
    }, 1000)
    
  } catch (err: any) {
    console.error('处理登录回调失败:', err)
    error.value = err.message || '登录处理失败'
    loading.value = false
  }
})
</script>

<style scoped>
.callback-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.callback-box {
  width: 400px;
  text-align: center;
  padding: 40px;
}

.loading-content,
.error-content,
.success-content {
  padding: 20px;
}

.loading-content h2,
.error-content h2,
.success-content h2 {
  margin: 20px 0 10px 0;
  color: #333;
  font-size: 20px;
  font-weight: 600;
}

.loading-content p,
.error-content p,
.success-content p {
  margin: 10px 0;
  color: #666;
  font-size: 14px;
}

.error-content .el-button {
  margin-top: 20px;
}
</style>