<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"
    >
      <WaterList :list="list">
        <template #left="{ item }">
          <Item :item="item"></Item>
        </template>
        <template #right="{ item }">
          <Item :item="item"></Item>
        </template>
      </WaterList>
    </van-list>
  </van-pull-refresh>
</template>

<script setup>
import { ref, defineProps } from "vue";
import { queryLivePlan } from "@/api/index";
import WaterList from "@/components/WaterList.vue";
import Item from "./Item.vue";

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

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 queryLivePlan({
    type: 2,
    size: 10,
    current: current.value,
    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,
    tagId: props.tagId,
    weight:
      refreshing.value || !list.value.length
        ? ""
        : list.value[list.value.length - 1].weight,
  });
  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></style>
