<template>
  <div>
    <div class="con-title">{{ $t('message.title') }}</div>
    <div class="con-box">
      <el-collapse accordion v-model="activeName" @change="handleChange" v-if="msgList.length">
        <template v-for="(item, index) in msgList">
          <el-collapse-item v-bind:key="index" :name="index">
            <template slot="title">
              <el-badge :is-dot="!item.isShow" class="item">{{ item.title || $t('message.itemTitle') }}</el-badge>
            </template>
            <div v-html="item.text"></div>
          </el-collapse-item>
        </template>
      </el-collapse>
      <div class="empty" v-else>{{ $t('message.tip') }}</div>
      <el-pagination
        background
        layout="prev, pager, next"
        :current-page.sync="page.currentPage"
        :page-size="page.limit"
        :total="page.total"
        :hide-on-single-page="true"
        @current-change="handleCurrentChange"
        style="margin: 0 auto; padding: 40px 0; text-align: center"
      ></el-pagination>
    </div>
  </div>
</template>

<script>
import cAction from '@action'

export default {
  data() {
    return {
      activeName: null,
      msgList: [],
      page: {
        offset: 0,
        total: 0,
        currentPage: 1,
        limit: 10
      }
    }
  },
  beforeMount() {
    this.getData()
  },
  methods: {
    handleChange(val) {
      if (typeof val === 'number' && this.msgList[val].isShow === 0) {
        cAction.Other.setMyMsg(this.msgList[val].id).then(json => {
          this.getData()
          cAction.Other.getNavMsg().then(data => {
            this.$store.commit('myMsg', data.num)
          })
        })
      }
    },
    getData() {
      const params = {
        offset: this.page.offset,
        limit: this.page.limit
      }
      cAction.Other.getMyMsg(params).then(response => {
        this.msgList = response.list
        this.page.total = parseInt(response.count)
      })
    },
    handleCurrentChange() {
      this.page.offset = (this.page.currentPage - 1) * this.page.limit
      this.getData()
    }
  }
}
</script>

<style lang="scss">
.el-collapse-item__header {
  line-height: 15px;
}

.empty {
  padding: 100px;
  font-size: 18px;
  color: #5f6368;
  text-align: center;
}
</style>
