<template>
  <div>
    <van-list
      class="load-list"
      v-model="loading"
      @load="onLoad"
      :finished="finished"
      offset="10"
      :immediate-check="false"
      v-if="data.length"
    >
      <slot :list="data"></slot>
    </van-list>
    <van-empty :description="description" v-else />
  </div>
</template>

<script>
export default {
  name: 'loadList',
  props: {
    url: {
      type: String,
      default: '',
    },
    description: {
      type: String,
      default: '',
    },
    params: {
      type: Object,
      default: () => {
        return {};
      },
    },
  },
  data() {
    return {
      loading: false,
      finished: false,
      defaultParams: {
        pageNum: 1,
        pageSize: 50,
      },
      data: [],
      total: 0,
    };
  },
  mounted() {
  },
  methods: {
    onLoad() {
      this.defaultParams.pageNum++;
      this.list(true);
      if (this.data.length >= this.total) {
        this.finished = true;
      }
    },
    list(bool = false) {
      if (Object.keys(this.params).length) {
        this.data = [];
        Object.assign(this.defaultParams, this.params);
      }
      this.$axios
        .$post(this.url, this.defaultParams)
        .then((res) => {
          if (res.success) {
            this.data.push(...res.data.list);
            this.total = res.data.total;
            if (bool) this.loading = false;
          }
        });
    },
  },
};
</script>

<style lang="less" scoped></style>
