<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"
    >
      <CircleItem  
        v-for="(item, index) in list"
        :key="index"
        :item="item"/>
    </van-list>
  </van-pull-refresh>
</template>

<script setup>
import { ref, defineProps } from "vue";
import { getCircleList } from "@/api/circle";
import CircleItem from "./components/CircleItem.vue";

const props = defineProps({
  id: {
    type: String,
  },
});

console.log(props);

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 getCircleList({
    id: props.id,
    lastId: refreshing.value || !list.value.length
        ? ""
        : list.value[list.value.length - 1].id,
    lastPublishTime: refreshing.value || !list.value.length
        ? ""
        : list.value[list.value.length - 1].auditTime,
    lastWeight: refreshing.value || !list.value.length
        ? ""
        : list.value[list.value.length - 1].isRecommend,  
    size: 10, 
  });
  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>
