<template>
  <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
    <van-list
      v-model:loading="loading"
      v-model:error="loadError"
      :finished="finished"
      finished-text="没有更多了"
      error-text="加载错误，请点击重新加载"
      @load="onLoad"
    >
      <LiveItem v-for="(item, index) in list" :key="index" :item="item" />
    </van-list>
  </van-pull-refresh>
</template>

<script setup>
import { ref } from "vue";
import { useRoute } from "vue-router";
import LiveItem from "./components/LiveItem.vue";
import { queryColumnVideo } from "@/api/column";

const route = useRoute();
const list = ref([]);
const loading = ref(false);
const loadError = ref(false);
const finished = ref(false);
const refreshing = ref(false);
const current = ref(1);

const onRefresh = () => {
  current.value = 1;
  // 清空列表数据
  finished.value = false;

  // 重新加载数据
  // 将 loading 设置为 true，表示处于加载状态
  loading.value = true;
  onLoad();
};
const onLoad = async () => {
  // 异步更新数据
  let ret = await queryColumnVideo({
    columnId: route.query.id,
    type: 1,
    size: 10,
    current: current.value,
    lastStartTime:
      refreshing.value || !list.value.length
        ? ""
        : list.value[list.value.length - 1].startTime,
    lastAuditTime:
      refreshing.value || !list.value.length
        ? ""
        : list.value[list.value.length - 1].auditTime,
    lastCreateTime:
      refreshing.value || !list.value.length
        ? ""
        : list.value[list.value.length - 1].createTime,
    lastStatus:
      refreshing.value || !list.value.length
        ? ""
        : list.value[list.value.length - 1].liveStatus,
  });
  if (ret.code === 0) {
    if (refreshing.value) {
      list.value = [];
      refreshing.value = false;
    }
    current.value++;
    list.value = list.value.concat(ret.data.list);
    finished.value = !ret.data.hasNext;
  } else {
    loadError.value = true
  }
  loading.value = false;
};
</script>
<style scoped lang="scss"></style>
