<script setup lang="ts">
import { ref, onMounted } from 'vue'
import OnlineIndex from '@/views/troubleshoot/OnlineIndex.vue'
import OfflineIndex from '@/views/troubleshoot/OfflineIndex.vue'
import http from '@/utils/http'

const offlineMode = ref<boolean | null>(null)  // null = loading
const loading = ref(true)

async function checkMode() {
  try {
    const res = await http.get('/api/health')
    offlineMode.value = res.data?.offline_mode === true
  } catch {
    // 默认在线模式
    offlineMode.value = false
  } finally {
    loading.value = false
  }
}

onMounted(() => {
  checkMode()
})
</script>

<template>
  <div class="troubleshoot-entry">
    <!-- 加载中 -->
    <div v-if="loading" class="loading-mask">
      <div class="loading-spinner" />
      <p>正在加载...</p>
    </div>

    <!-- 在线模式（有 AI 接入） -->
    <OnlineIndex v-else-if="offlineMode === false" />

    <!-- 离线模式 -->
    <OfflineIndex v-else />
  </div>
</template>

<style scoped lang="scss">
.troubleshoot-entry {
  min-height: 100vh;
}

.loading-mask {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  gap: 16px;

  p {
    font-size: 16px;
    opacity: 0.9;
  }
}

.loading-spinner {
  width: 40px;
  height: 40px;
  border: 3px solid rgba(255, 255, 255, 0.3);
  border-top-color: white;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}
</style>
