<template>
  <div>
    <TopNav @tab-change="handleTabChange" />
    <div class="top-placeholder"></div>

    <van-pull-refresh v-model="isRefreshing" @refresh="onRefresh">
      <van-list
        v-model="isLoading"
        :finished="isFinished"
        finished-text="没有更多了"
        @load="onLoad"
      >
        <div v-if="currentTab === 'recommend'" class="video-container">
          <VideoCard
            v-for="item in dataList"
            :key="item.videoId"
            :video="item"
            @click="$router.push(`/video/${item.videoId}`)"
          />
        </div>
        <div v-else class="hot-list">
          <div
            v-for="item in hotList"
            :key="item.videoId"
            class="hot-item"
            @click="$router.push(`/video/${item.videoId}`)"
          >
            <van-card :title="item.title" :thumb="item.coverUrl">
              <template #desc>
                <div class="card-desc">
                  <p class="author">UP主：{{ item.user ? item.user.screenName : '未知' }}</p>
                  <p class="stats">{{ item.view }}播放 · {{ item.pubDateStr || '刚刚' }}</p>
                </div>
              </template>
              <template #footer>
                <van-icon name="ellipsis" size="18" color="#999" />
              </template>
            </van-card>
          </div>
        </div>
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script>
import TopNav from '@/components/TopNav.vue'
import VideoCard from '@/components/VideoCard.vue'
import { getHotVideo, videoRecommend } from '@/api/vod'

export default {
  name: 'Home',
  components: {
    TopNav,
    VideoCard
  },
  data() {
    return {
      currentTab: 'recommend', // 默认为推荐
      nextId: 0,
      hotList: [],
      dataList: [],
      isLoading: false,
      isFinished: false,
      isRefreshing: false
    }
  },
  methods: {
    handleTabChange(type) {
      this.currentTab = type
      this.onRefresh()
    },
    // 模拟从后端获取数据
    async onLoad() {
      if (this.currentTab === 'hot') {
        this.dataList = []
        this.nextId = 0
        getHotVideo().then((resp) => {
          if (resp.code === 0) {
            this.hotList = resp.data
          } else {
            this.$toast(resp.msg)
          }
        })
        this.isFinished = true
        return
      }

      videoRecommend(this.nextId)
        .then((resp) => {
          if (resp.code === 0) {
            const respData = resp.data
            if (respData.length === 0) {
              this.$toast('已经到底啦~~~')
              return
            }

            if (this.isRefreshing) {
              this.dataList = []
              this.isRefreshing = false
            }

            this.dataList.push(...respData)
            this.isLoading = false

            if (this.dataList.length >= 1000) {
              this.isFinished = true
            }
            this.nextId++
          } else {
            this.$toast('获取数据失败, 请重新刷新页面')
          }
        })
        .catch((error) => {
          this.$toast(error.message)
        })
    },
    onRefresh() {
      this.isFinished = false
      this.isLoading = true
      this.onLoad()
    }
  }
}
</script>

<style scoped lang="less">
.top-placeholder {
  height: 94px;
}

.video-container {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  padding: 8px 12px;
  background-color: #f4f4f4;

  // 关键：控制 VideoCard 在两列布局中的宽度
  /deep/ .video-card {
    width: 48.5%; // 留出中间的间隙
  }
}

// 热门模式样式
.hot-list {
  background-color: #fff;

  .hot-item {
    border-bottom: 1px solid #f0f0f0;

    ::v-deep .van-card {
      background-color: #fff;
      padding: 12px;

      .van-card__title {
        font-size: 14px;
        font-weight: 500;
        line-height: 1.4;
        max-height: 40px;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
        overflow: hidden;
      }

      .van-card__thumb {
        width: 120px;
        height: 75px;
        border-radius: 4px;
        overflow: hidden;
      }
    }
  }

  .card-desc {
    margin-top: 8px;
    font-size: 12px;
    color: #999;

    p {
      margin: 2px 0;
    }
    .author {
      color: #666;
    }
  }
}
</style>
