<script setup>
import { ref, onMounted, computed } from 'vue'
import axios from 'axios'

// 响应式数据
const currentTab = ref('ranking') // 'ranking' 或 'news'
const rankingData = ref([])
const loading = ref(false)
const selectedDate = ref('')
const currentPage = ref(1)
const totalPages = ref(1)

// 初始化日期为今天
const initDate = () => {
  const today = new Date()
  selectedDate.value = today.toISOString().split('T')[0]
}

// 获取排行榜数据
const fetchRankingData = async () => {
  loading.value = true
  try {
    const response = await axios.get('http://localhost:5001/api/rank/videos', {
      params: {
        page: currentPage.value,
        limit: 20,
        sort: 'growth',
        start_date: selectedDate.value,
        end_date: selectedDate.value
      }
    })
    
    if (response.data.success) {
      rankingData.value = response.data.data
      totalPages.value = response.data.pagination.pages
    } else {
      console.error('获取数据失败:', response.data.message)
      rankingData.value = []
    }
  } catch (error) {
    console.error('API调用失败:', error)
    rankingData.value = []
  } finally {
    loading.value = false
  }
}

// 获取当前时间
    const getCurrentTime = () => {
      const now = new Date()
      return `${now.getHours().toString().padStart(2, '0')}:${now.getMinutes().toString().padStart(2, '0')}`
    }

    const getRankClass = (rank) => {
      if (rank === 1) return 'rank-first'
      if (rank === 2) return 'rank-second'  
      if (rank === 3) return 'rank-third'
      return 'rank-normal'
    }

// 格式化播放量
const formatPlayCount = (count) => {
  if (!count) return '0'
  if (count >= 100000000) {
    return (count / 100000000).toFixed(1) + '亿'
  } else if (count >= 10000) {
    return (count / 10000).toFixed(1) + '万'
  }
  return count.toString()
}

// 格式化增长数据
const formatGrowth = (item) => {
  const timelineData = item.timeline_data || {}
  const change = timelineData.play_vv_change || 0
  const changeRate = timelineData.play_vv_change_rate || 0
  
  if (change > 0) {
    return `+${formatPlayCount(change)} (${changeRate.toFixed(1)}%)`
  }
  return '暂无数据'
}

// 切换标签页
const switchTab = (tab) => {
  currentTab.value = tab
  if (tab === 'ranking') {
    fetchRankingData()
  }
}

// 日期改变处理
const onDateChange = () => {
  currentPage.value = 1
  fetchRankingData()
}

// 页面加载时初始化
onMounted(() => {
  initDate()
  fetchRankingData()
})
</script>

<template>
  <div class="app">
    <!-- 主内容区域 -->
    <div class="main-content">
      <!-- 排行榜页面 -->
      <div v-if="currentTab === 'ranking'" class="ranking-page">
        <!-- 标题 -->
        <div class="header">
          <div class="title-container">
            <span class="lightning-icon">⚡</span>
            <h1 class="title">热播总榜</h1>
            <span class="lightning-icon">⚡</span>
          </div>
          <div class="update-time">
            基于实时热度排行 {{ getCurrentTime() }}更新 
            <span class="refresh-icon">🔄</span>
          </div>
        </div>

        <!-- 日期选择 -->
        <div class="date-selector">
          <label for="date-input">选择日期：</label>
          <input 
            id="date-input"
            type="date" 
            v-model="selectedDate" 
            @change="onDateChange"
            class="date-input"
          />
        </div>

        <!-- 加载状态 -->
        <div v-if="loading" class="loading">
          <div class="loading-spinner"></div>
          <p>加载中...</p>
        </div>

        <!-- 榜单内容 -->
        <div v-else class="ranking-list">
          <div 
            v-for="(item, index) in rankingData" 
            :key="item._id || index"
            class="ranking-item"
          >
            <!-- 排名 -->
            <div class="rank-number" :class="getRankClass(index + 1)">
              {{ index + 1 }}
            </div>

            <!-- 海报 -->
            <div class="poster">
              <img 
                :src="item.cover_image_url || '/placeholder-poster.svg'" 
                :alt="item.title || item.mix_name"
                @error="$event.target.src='/placeholder-poster.svg'"
                class="poster-img"
              />
            </div>

            <!-- 内容信息 -->
            <div class="content-info">
              <!-- 剧名 -->
              <h3 class="drama-name">{{ item.title || item.mix_name || '未知剧名' }}</h3>
              
              <!-- 当前播放量 -->
              <div class="play-count">
                <span class="play-label">总播放量：</span>
                <span class="play-value">{{ formatPlayCount(item.play_vv) }}</span>
              </div>
              
              <!-- 简介（省略显示） -->
              <div class="description">
                {{ item.summary || item.title || item.mix_name || '暂无简介' }}
              </div>
            </div>
            
            <!-- 增长数据 -->
            <div class="growth-data">
              <span class="growth-icon">🔥</span>
              <span class="growth-number">{{ formatGrowth(item) }}</span>
            </div>
          </div>

          <!-- 空状态 -->
          <div v-if="rankingData.length === 0" class="empty-state">
            <p>暂无排行榜数据</p>
          </div>
        </div>
      </div>

      <!-- 资讯页面占位 -->
      <div v-else class="news-page">
        <div class="header">
          <h1 class="title">资讯中心</h1>
        </div>
        <div class="coming-soon">
          <p>资讯功能即将上线...</p>
        </div>
      </div>
    </div>

    <!-- 底部导航 -->
    <div class="bottom-nav">
      <div 
        class="nav-item"
        :class="{ active: currentTab === 'news' }"
        @click="switchTab('news')"
      >
        <i class="bi bi-newspaper"></i>
        <span class="nav-text">资讯</span>
      </div>
      
      <div 
        class="nav-item"
        :class="{ active: currentTab === 'ranking' }"
        @click="switchTab('ranking')"
      >
        <i class="bi bi-list-stars"></i>
        <span class="nav-text">排行榜</span>
      </div>
    </div>
  </div>
</template>

<style scoped>
/* 全局样式 */
* {
  box-sizing: border-box;
}

.app {
  min-height: 100vh;
  background: linear-gradient(135deg, #1a1a2e 0%, #16213e 50%, #0f3460 100%);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  padding-bottom: 80px; /* 为底部导航留出空间 */
  color: white;
  padding: 0;
  margin: 0;
}

/* 主内容区域 */
.main-content {
  padding: 20px 16px 80px;
  max-width: 100%;
}

/* 标题区域 */
.header {
  text-align: center;
  margin-bottom: 20px;
}

.title-container {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  margin-bottom: 8px;
}

.lightning-icon {
  font-size: 20px;
  color: #ffd700;
}

.title {
  color: white;
  font-size: 24px;
  font-weight: bold;
  margin: 0;
  text-shadow: 0 2px 4px rgba(0,0,0,0.3);
}

.update-time {
  font-size: 12px;
  color: #999;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
}

.refresh-icon {
  font-size: 12px;
  color: #4CAF50;
}

/* 日期选择器 */
.date-selector {
  background: rgba(255, 255, 255, 0.9);
  padding: 15px;
  border-radius: 12px;
  margin-bottom: 20px;
  display: flex;
  align-items: center;
  gap: 10px;
  box-shadow: 0 4px 12px rgba(0,0,0,0.1);
}

.date-selector label {
  font-weight: 500;
  color: #333;
}

.date-input {
  flex: 1;
  padding: 8px 12px;
  border: 2px solid #e1e5e9;
  border-radius: 8px;
  font-size: 16px;
  background: white;
}

.date-input:focus {
  outline: none;
  border-color: #667eea;
}

/* 加载状态 */
.loading {
  text-align: center;
  padding: 40px 20px;
  color: white;
}

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

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

/* 榜单列表 */
.ranking-list {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.ranking-item {
  background: rgba(255, 255, 255, 0.95);
  border-radius: 16px;
  padding: 15px;
  display: flex;
  align-items: flex-start;
  gap: 15px;
  box-shadow: 0 4px 20px rgba(0,0,0,0.1);
  transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.ranking-item:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 25px rgba(0,0,0,0.15);
}

/* 排名数字 */
.rank-number {
  background: linear-gradient(135deg, #ff6b6b, #ee5a24);
  color: white;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: bold;
  font-size: 14px;
  flex-shrink: 0;
  box-shadow: 0 2px 8px rgba(255,107,107,0.3);
}

/* 海报 */
.poster {
  flex-shrink: 0;
}

.poster-img {
  width: 60px;
  height: 80px;
  object-fit: cover;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.15);
}

/* 内容信息 */
.content-info {
  flex: 1;
  min-width: 0;
}

.drama-name {
  font-size: 16px;
  font-weight: bold;
  color: #2c3e50;
  margin: 0 0 8px 0;
  line-height: 1.3;
}

.growth-info, .play-count {
  display: flex;
  align-items: center;
  margin-bottom: 6px;
  font-size: 13px;
}

.growth-label, .play-label {
  color: #7f8c8d;
  margin-right: 5px;
}

.growth-value {
  color: #e74c3c;
  font-weight: 600;
}

.play-value {
  color: #3498db;
  font-weight: 600;
}

.description {
  color: #7f8c8d;
  font-size: 12px;
  line-height: 1.4;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  margin-top: 8px;
}

/* 空状态 */
.empty-state {
  text-align: center;
  padding: 60px 20px;
  color: rgba(255,255,255,0.8);
  font-size: 16px;
}

/* 资讯页面 */
.news-page .coming-soon {
  text-align: center;
  padding: 100px 20px;
  color: rgba(255,255,255,0.8);
  font-size: 18px;
}

/* 底部导航 */
.bottom-nav {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  display: flex;
  border-top: 1px solid rgba(0,0,0,0.1);
  box-shadow: 0 -2px 20px rgba(0,0,0,0.1);
}

.nav-item {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 12px 8px;
  cursor: pointer;
  transition: all 0.2s ease;
  color: #7f8c8d;
}

.nav-item.active {
  color: #667eea;
  background: rgba(102, 126, 234, 0.1);
}

.nav-item:hover {
  background: rgba(0,0,0,0.05);
}

.nav-icon {
  font-size: 20px;
  margin-bottom: 4px;
}

.nav-text {
  font-size: 12px;
  font-weight: 500;
}

/* 移动端适配 */
@media (max-width: 768px) {
  .main-content {
    padding: 15px;
  }
  
  .title {
    font-size: 20px;
  }
  
  .ranking-item {
    padding: 12px;
    gap: 12px;
  }
  
  .poster-img {
    width: 50px;
    height: 67px;
  }
  
  .drama-name {
    font-size: 15px;
  }
  
  .date-selector {
    padding: 12px;
    flex-direction: column;
    align-items: stretch;
    gap: 8px;
  }
  
  .date-input {
    width: 100%;
  }
}

@media (max-width: 480px) {
  .main-content {
    padding: 10px;
  }
  
  .ranking-item {
    padding: 10px;
    gap: 10px;
  }
  
  .poster-img {
    width: 45px;
    height: 60px;
  }
  
  .rank-number {
    width: 28px;
    height: 28px;
    font-size: 12px;
  }
}
</style>
