<template>
  <co-loadmore ref="loadmore"
      :max-distance="maxDistance"
      :auto-fill="autoFill"
      :distance-index="distanceIndex"
      :show-top-end="showTopEnd"
      :show-bottom-end="showBottomEnd"
      :top-distance="topDistance"
      :top-method="loadTop"
      :top-pull-text="topPullText"
      :top-drop-text="topDropText"
      :top-loading-text="topLoadingText"
      :top-loading-success="topLoadingSuccess"
      :bottom-distance="bottomDistance"
      :bottom-method="loadBottom"
      :bottom-pull-text="bottomPullText"
      :bottom-pull-end-text="bottomPullEndText"
      :bottom-drop-text="bottomDropText"
      :bottom-loading-text="bottomLoadingText"
      :bottom-all-loaded="bottomAllLoaded">
      <slot></slot>
      <!-- 无数据提示 -->
      <empty v-if="!list.length" class="co-flex co-ver co-ac co-jc" style="height: 300px;"></empty>
  </co-loadmore>
</template>

<script type="es6">
  export default {
    name: 'cus-loading',

    props: {
      maxDistance: {
        type: Number,
        default: 0
      },
      autoFill: {
        type: Boolean,
        default: false
      },
      distanceIndex: {
        type: Number,
        default: 2
      },
      showTopEnd: {
        type: Boolean,
        default: true
      },
      topPullText: {
        type: String,
        default: '下拉刷新'
      },
      topDropText: {
        type: String,
        default: '释放更新'
      },
      topLoadingText: {
        type: String,
        default: '加载中...'
      },
      topLoadingSuccess: {
        type: String,
        default: '刷新成功'
      },
      topDistance: {
        type: Number,
        default: 70
      },
      topMethod: {
        type: Function
      },
      showBottomEnd: {
        type: Boolean,
        default: false
      },
      bottomPullText: {
        type: String,
        default: '上拉加载更多'
      },
      bottomPullEndText: {
        type: String,
        default: '暂无更多数据'
      },
      bottomDropText: {
        type: String,
        default: '释放更新'
      },
      bottomLoadingText: {
        type: String,
        default: '加载中...'
      },
      bottomDistance: {
        type: Number,
        default: 70
      },
      bottomMethod: {
        type: Function
      },
      bottomAllLoaded: {
        type: Boolean,
        default: false
      },
      loadMethod: {
        type: Function
      },
      list: {
        type: Array
      }
    },

    created() {
      this.loadTop();
    },

    methods: {
      // 顶部下拉触发
      loadTop() {
        let s = config.pageSize;
        this.loadMethod({
          // 翻页参数
          data: {
            p: 0,
            s: s
          },
          resolve: data => {
            this.$refs.loadmore.onTopLoaded(data.length < s);
            return data;
          }
        });
      },
      // 底部上拉触发
      loadBottom() {
        let list = this.list;
        let s = config.pageSize;
        this.loadMethod({
          // 翻页参数
          data: {
            p: Math.ceil(list.length / s),
            s: s
          },
          resolve: data => {
            this.$refs.loadmore.onBottomLoaded(data.length < s);
            return list.concat(data);
          }
        });
      }
    }
  };
</script>
