<script lang="ts" setup>
import { ref, onMounted, onUnmounted } from 'vue'
import PinnedBids from './components/PinnedBids.vue'
import AiRecommendations from './components/AiRecommendations.vue'

const activeTab = ref('pinned')
const pinnedBidsRef = ref<InstanceType<typeof PinnedBids>>()
const aiRecommendationsRef = ref<InstanceType<typeof AiRecommendations>>()
let refreshTimer: number | null = null

const tabs = [
  { id: 'pinned', label: '置顶项目' },
  { id: 'ai', label: 'AI 推荐' }
]

const handleRefresh = () => {
  if (activeTab.value === 'pinned' && pinnedBidsRef.value) {
    pinnedBidsRef.value.loadPinnedBids()
  } else if (activeTab.value === 'ai' && aiRecommendationsRef.value) {
    aiRecommendationsRef.value.loadRecommendations()
  }
}

const startAutoRefresh = () => {
  // 每5分钟（300000毫秒）自动刷新
  refreshTimer = window.setInterval(() => {
    handleRefresh()
  }, 300000)
}

const stopAutoRefresh = () => {
  if (refreshTimer !== null) {
    clearInterval(refreshTimer)
    refreshTimer = null
  }
}

onMounted(() => {
  startAutoRefresh()
})

onUnmounted(() => {
  stopAutoRefresh()
})
</script>

<template>
  <div class="app-container">
    <div class="tabs">
      <button
        v-for="tab in tabs"
        :key="tab.id"
        :class="['tab-button', { active: activeTab === tab.id }]"
        @click="activeTab = tab.id"
      >
        {{ tab.label }}
      </button>
      <button class="refresh-button" @click="handleRefresh">
        🔄 刷新
      </button>
    </div>

    <div class="tab-content">
      <PinnedBids v-if="activeTab === 'pinned'" ref="pinnedBidsRef"/>
      <AiRecommendations v-else-if="activeTab === 'ai'" ref="aiRecommendationsRef"/>
    </div>
  </div>
</template>

<style>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
  background: #f5f5f5;
}

.app-container {
  min-height: 100vh;
  background: #f5f5f5;
}

.tabs {
  display: flex;
  background: #fff;
  border-bottom: 1px solid #e0e0e0;
  padding: 0 20px;
}

.tab-button {
  padding: 16px 24px;
  background: none;
  border: none;
  border-bottom: 2px solid transparent;
  font-size: 14px;
  font-weight: 500;
  color: #666;
  cursor: pointer;
  transition: all 0.2s ease;
}

.tab-button:hover {
  color: #333;
  background: #f9f9f9;
}

.tab-button.active {
  color: #3498db;
  border-bottom-color: #3498db;
}

.tab-content {
  padding: 20px;
}

.refresh-button {
  margin-left: auto;
  padding: 8px 16px;
  background: #3498db;
  color: #fff;
  border: none;
  border-radius: 4px;
  font-size: 14px;
  cursor: pointer;
  transition: all 0.2s ease;
}

.refresh-button:hover {
  background: #2980b9;
}

.refresh-button:active {
  transform: scale(0.98);
}

.placeholder {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 400px;
  color: #999;
  font-size: 16px;
}
</style>
