<template>
  <div class="main">
    <!-- <card :title="title"> -->
    <card>
      <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>
    </card>
  </div>
</template>

<script>
import Card from '@/components/Card'
import MessageItem from '@/components/MessageItem'
import * as api from '@/api'
export default {
  name: 'MessageList',
  components: { Card, 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.types
      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>
