<template>
  <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
    <van-list
      v-model:loading="loading"
      :finished="finished"
      v-model:error="loadError"
      finished-text="没有更多了"
      error-text="加载错误，请点击重新加载"
      @load="onLoad"
    >
      <OrdersItem
        v-for="(item, index) in list"
        :key="index"
        :item="item"
        :index="index"
      />
      <Empty text="暂无数据～" v-if="!list.length" />
    </van-list>
  </van-pull-refresh>
</template>

<script setup>
import { ref } from "vue";
import Empty from "@/components/Empty.vue";
import OrdersItem from "./components/OrdersItem.vue";
import { queryUserOrderList } from "@/api/index";

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 queryUserOrderList({
    current: current.value,
    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>
