<template>
  <mescroll-vue
    ref="mescrollRef"
    @init="mescrollInit"
    :down="downOptions"
    :up="upOptions"
  >
    <list-item v-for="(item, index) of list" :key="index" :itemData="item" />
  </mescroll-vue>
</template>

<script>
import ListItem from "./list-item.vue";
import { MescrollMixin, MescrollMoreItemMixin } from "@/mixins";
export default {
  mixins: [MescrollMixin, MescrollMoreItemMixin],
  components: { ListItem },
  props: {
    index: Number,
    tab: Object,
  },
  data() {
    return {
      list: [],
      downOptions: {
        autoShowLoading: false,
        auto: false,
        callback: this.downCallback,
      },
      upOptions: {
        auto: false,
        callback: this.upCallback,
      },
    };
  },
  methods: {
    initData() {
      this.requestData({ num: 1, size: 10 });
      this.list = [];
      this.mescroll.setPageNum(2);
    },
    requestData(page) {
      if (page.num === 1) {
        this.$showLoading();
      }
      this.$api({
        apiName: "getCouponList",
        postData: {
          param: this.tab.value,
          page: page.num,
          size: page.size,
        },
        success: (res) => {
          let arr = res.marketingCoupons;
          if (page.num === 1) {
            this.list = arr;
          } else {
            this.list = this.list.concat(arr);
          }
          this.mescroll.endBySize(arr.length, res.count);
        },
        fail: (err) => {
          this.mescroll.endErr();
        },
        complete: () => {
          if (page.num === 1) {
            this.$hideLoading();
          }
          this.mescroll.endDownScroll();
        },
      });
    },
    downCallback() {
      this.mescroll.resetUpScroll();
    },
    upCallback(page) {
      this.requestData(page);
    },
  },
};
</script>

