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

<script>
import MessageItem from '@/components/MessageItem'
import * as api from '@/api'
export default {
  name: 'MessageList',
  components: { MessageItem },
  data() {
    return {
      list: [],
      loading: false,
      finished: false,
      requestParams: { offset: 0, limit: 20, type: '' }
    }
  },
  computed: {
    type() {
      return this.$route.params.type
    },
    title() {
      const typeMap = { 1: '组织消息', 2: '活动消息', 3: '系统消息' }
      return typeMap[this.type]
    }
  },
  metaInfo() {
    return {
      title: this.title
    }
  },
  methods: {
    getList() {
      this.loading = true
      this.requestParams.type = this.type
      api
        .getMessageList(this.requestParams)
        .then(response => {
          if (response.length) {
            this.list = this.list.concat(response)
          }
          this.loading = false
          this.finished = response.length < this.requestParams.limit
        })
        .catch(() => {
          this.loading = false
          this.finished = true
        })
    },
    onLoad() {
      this.requestParams.offset = this.requestParams.offset + this.requestParams.limit
      this.getList()
    },
    // 刷新
    refresh() {
      this.requestParams.offset = 0
      this.list = []
      this.getList()
    }
  },
  beforeMount() {
    this.getList()
  }
}
</script>

<style lang="scss" scoped>
.main-message {
  min-height: 100vh;
  background-color: #f6f8f9;
  overflow: hidden;
}
.message-item {
  margin: 10px;
}
</style>