<template>
  <div class="quick-login-container">
    <div class="loading-overlay">
      <div class="loading-content">
        <el-loading-spinner class="loading-spinner"></el-loading-spinner>
        <p class="loading-text">登录中...</p>
      </div>
    </div>
  </div>
</template>

<script setup>
import { onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { getTenantIdByName, login } from '@/api/login/login.js'
import { ElLoading, ElMessage } from 'element-plus'

const router = useRouter()
let loading = null

// 测试账号信息
const testAccount = {
  tenantName: '人工智能通识课平台',
  username: 'zdxyz',
  password: 'zdxyz' 
}

// 获取租户 ID
const getTenantId = async (tenantName) => {
  try {
    const res = await getTenantIdByName(tenantName)
    if (res && res.data) {
      return res.data
    } else {
      ElMessage.error('租户验证失败！')
      return null
    }
  } catch (error) {
    ElMessage.error('获取租户信息失败！')
    console.error('获取租户 ID 错误:', error)
    return null
  }
}

// 自动登录函数
const autoLogin = async () => {
  try {
    // 显示全局加载状态
    loading = ElLoading.service({
      lock: true,
      text: '登录中...',
      background: 'rgba(0, 0, 0, 0.7)'
    })

    // 获取租户ID
    const tenantId = await getTenantId(testAccount.tenantName)
    if (!tenantId) {
      // 租户验证失败
      return
    }

    // 执行登录
    const res = await login(
      { 'Tenant-Id': tenantId },
      testAccount
    )

    if (res && res.code === 0) {
      // 登录成功，保存登录状态
      localStorage.setItem('isLoggedIn', 'true')
      localStorage.setItem('token', res.data.accessToken)
      localStorage.setItem('userName', testAccount.username)
      localStorage.setItem('tenantName', testAccount.tenantName)
      localStorage.setItem('password', testAccount.password)
      localStorage.setItem('rememberMe', 'true')
      //课程权限
      localStorage.setItem('courseDataScope', res.data.courseDataScope)
      localStorage.setItem('blocklyDataScope', res.data.blocklyDataScope)

      ElMessage.success('信息校验成功')
      // 跳转到课程界面
      router.push('/ai-general-course')
    } else {
      ElMessage.error(res?.message || '信息校验失败')
      // 如果登录失败，跳转到正常登录页面
      router.push('/login')
    }
  } catch (error) {
    ElMessage.error('信息校验过程中发生错误')
    console.error('信息校验错误:', error)
    // 错误时跳转到登录页面
    router.push('/login')
  } finally {
    // 关闭加载状态
    if (loading) {
      loading.close()
    }
  }
}

// 组件挂载时立即执行自动登录
onMounted(() => {
  autoLogin()
})
</script>

<style scoped>
.quick-login-container {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(to bottom, #001169, #8a78d0);
}

.loading-overlay {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100%;
}

.loading-content {
  text-align: center;
  color: white;
}

.loading-spinner {
  width: 60px;
  height: 60px;
  margin-bottom: 20px;
}

.loading-text {
  font-size: 18px;
  font-weight: 500;
}
</style>