<template>
  <div class="list">
    <van-list v-model="loading" :finished="finished" @load="onLoad" v-if="list.length">
      <my-group-item v-for="item in list" :data="item" :key="item.id"></my-group-item>
    </van-list>
    <template v-else>
      <slot name="empty">
        <div class="empty">暂时没有内容哦，去别处看看吧～</div>
      </slot>
    </template>
  </div>
</template>

<script>
import * as api from '@/api/index'
import MyGroupItem from '@/components/MyGroupItem'
export default {
  components: { MyGroupItem },
  data() {
    return {
      list: [],
      loading: false,
      finished: false,
      page: { offset: 0, limit: 10 }
    }
  },
  methods: {
    getList() {
      this.loading = true
      api
        .getJoinedGroupes(this.page)
        .then(response => {
          if (response.length) {
            this.list = this.list.concat(response)
          }
          this.loading = false
          this.finished = response.length < this.page.limit
        })
        .catch(() => {
          this.loading = false
          this.finished = true
        })
        .finally(() => {
          if (this.finished) {
            this.$emit('finished', this.list)
          }
        })
    },
    onLoad() {
      this.page.offset = this.page.offset + this.page.limit
      this.getList()
    }
  },
  beforeMount() {
    this.getList()
  }
}
</script>
