<template>
  <div>
    <div class="con-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 || '系统通知' }}</el-badge>
            </template>
            <div v-html="item.text"></div>
          </el-collapse-item>
        </template>
      </el-collapse>
      <div class="empty" v-else>您没有收到任何公告</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: {
        page: 1,
        total: 0,
        page_size: 20
      }
    }
  },
  beforeMount() {
    this.getData()
  },
  methods: {
    handleChange(val) {
      if (typeof val === 'number' && this.msgList[val].isShow === 0) {
        cAction.Other.setMsgWmp(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)
        this.$store.commit('myMsg', response.countNum)
      })
    },
    handleCurrentChange() {
      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>
