import { createSubscribe } from 'applet-page-component';
Component(createSubscribe({
  onReachBottom() {
    this.fetchList({
      pageIndex: this.data.pageIndex + 1
    });
  }

})({
  props: {
    onService: () => [new Error('请传入接口')],
    style: '',
    className: ''
  },
  data: {
    list: [],
    pageIndex: 0,
    loading: false,
    noData: false
  },

  didMount() {
    this.fetchList();
  },

  methods: {
    // 获取列表 after: 放到前面还是后面
    async fetchList({
      pageIndex = 1,
      pageSize = 10,
      ...other
    } = {}, after = true, clear = false) {
      if (this.data.loading) {
        return;
      }

      if (clear) {
        this.setData({
          loading: true,
          noData: false,
          list: []
        });
      } else {
        this.setData({
          loading: true,
          noData: false
        });
      }

      const {
        parameter
      } = this.data;
      const [err, result] = await this.props.onService({
        pageSize,
        pageNum: pageIndex,
        ...other,
        ...parameter
      });
      this.setData({
        loading: false
      });

      if (err) {
        return;
      }

      const {
        list: newList
      } = result;
      const {
        list
      } = this.data;
      this.setData({
        pageIndex,
        noData: Number(result.pagination.total) === 0 && list.length === 0
      });
      this.$spliceData({
        list: [after ? list.length : 0, 0, ...newList]
      });
    }

  }
}));