<template>
  <a-popover
    trigger="click"
    placement="bottomRight"
    :autoAdjustOverflow="true"
    :arrowPointAtCenter="true"
    overlayClassName="header-notice-wrapper"
    @visibleChange="handleHoverChange"
    :overlayStyle="{ width: '300px', top: '50px' }"
  >
    <template slot="content">
      <a-spin :spinning="loadding">
        <a-tabs>
          <a-tab-pane :tab="msg1Title" key="1">
            <a-list>
              <a-list-item :key="index" v-for="(record, index) in announcement1">
                <div style="margin-left: 5%; width: 80%">
                  <p>
                    <a @click="showAnnouncement(record)">{{ record.title }}</a>
                  </p>
                  <p style="color: rgba(0, 0, 0, 0.45); margin-bottom: 0">{{ record.createTime }} 发布</p>
                </div>
                <div style="text-align: right">
                  <a-tag @click="showAnnouncement(record)" v-if="record.priority === 'L'" color="blue">一般消息 </a-tag>
                  <a-tag @click="showAnnouncement(record)" v-if="record.priority === 'M'" color="orange"
                    >重要消息
                  </a-tag>
                  <a-tag @click="showAnnouncement(record)" v-if="record.priority === 'H'" color="red"> 紧急消息 </a-tag>
                </div>
              </a-list-item>
              <div style="margin-top: 5px; text-align: center">
                <a-button @click="toMyAnnouncement()" type="dashed" block>查看更多</a-button>
              </div>
            </a-list>
          </a-tab-pane>
          <a-tab-pane :tab="msg2Title" key="2">
            <a-list>
              <a-list-item :key="index" v-for="(record, index) in announcement2">
                <div style="margin-left: 5%; width: 80%">
                  <p>
                    <a @click="showAnnouncement(record)">{{ record.title }}</a>
                  </p>
                  <p style="color: rgba(0, 0, 0, 0.45); margin-bottom: 0">{{ record.createTime }} 发布</p>
                </div>
                <div style="text-align: right">
                  <a-tag @click="showAnnouncement(record)" v-if="record.priority === 'L'" color="blue">一般消息 </a-tag>
                  <a-tag @click="showAnnouncement(record)" v-if="record.priority === 'M'" color="orange"
                    >重要消息
                  </a-tag>
                  <a-tag @click="showAnnouncement(record)" v-if="record.priority === 'H'" color="red"> 紧急消息 </a-tag>
                </div>
              </a-list-item>
              <div style="margin-top: 5px; text-align: center">
                <a-button @click="toMyAnnouncement()" type="dashed" block>查看更多</a-button>
              </div>
            </a-list>
          </a-tab-pane>
        </a-tabs>
      </a-spin>
    </template>
    <span @click="fetchNotice" class="header-notice">
      <a-badge :count="msgTotal">
        <a-icon style="font-size: 16px; padding: 4px" type="bell" />
      </a-badge>
    </span>
    <show-announcement ref="ShowAnnouncement"></show-announcement>
  </a-popover>
</template>

<script>
import { getAction, putAction } from '@/api/manage'
import ShowAnnouncement from './ShowAnnouncement'
// import UserAnnouncementList from '@/views/system/UserAnnouncementList'

export default {
  name: 'HeaderNotice',
  components: {
    ShowAnnouncement
  },
  data() {
    return {
      loadding: false,
      url: {
        selectInventoryLower: '/material/inventoryStatistics/selectInventoryLower',
        listCementByUser: '/sys/annountCement/listByUser',
        editCementSend: '/sys/sysAnnouncementSend/editByAnntIdAndUserId',
        queryById: '/sys/annountCement/queryById'
      },
      hovered: false,
      announcement1: [],
      announcement2: [],
      msg1Count: '0',
      msg2Count: '0',
      msg1Title: '通知(0)',
      msg2Title: '',
      stopTimer: false,
      websock: null,
      lockReconnect: false,
      heartCheck: null
    }
  },
  computed: {
    msgTotal() {
      return parseInt(this.msg1Count) + parseInt(this.msg2Count)
    }
  },
  mounted() {
    this.loadData()
  },
  destroyed: function () {
    // 离开页面生命周期函数
    // this.websocketclose()
  },
  methods: {
    loadData() {
      try {
        // 获取系统消息
        getAction(this.url.listCementByUser)
          .then(res => {
            if (res.code === '200') {
              this.announcement1 = res.value.sysMsgList
              this.msg1Count = res.value.anntMsgTotal
              // this.msg1Title = '通知(' + res.value.anntMsgTotal + ')'
              this.msg1Title = '工作通知(' + res.value.sysMsgTotal + ')'

              this.announcement2 = res.value.anntMsgList
              this.msg2Count = res.value.sysMsgTotal
              // this.msg2Title = '系统消息(' + res.value.sysMsgTotal + ')'
              this.msg2Title = '系统公告(' + res.value.anntMsgTotal + ')'

              // this.$refs.audio.currentTime = 0 //从头开始播放提示音
              // this.$refs.audio.play()
            }
          })
          .catch(() => {
            this.stopTimer = true
          })
      } catch (err) {
        this.stopTimer = true
      }
    },
    fetchNotice() {
      if (this.loadding) {
        this.loadding = false
      }
    },
    showAnnouncement(record) {
      putAction(this.url.editCementSend, { anntId: record.id }).then(res => {
        if (res.code === '200') {
          this.loadData()
        }
      })
      this.hovered = false
      this.$refs.ShowAnnouncement.detail(record)
    },
    toMyAnnouncement() {
      console.log('跳转 系统管理页面')
      // this.$router.push({
      //     path: '/isps/userAnnouncementList',
      //     name: 'isps-userAnnouncement'
      // })
    },
    handleHoverChange(visible) {
      this.hovered = visible
    }
  }
}
</script>

<style lang="css">
.header-notice-wrapper {
  top: 50px !important;
}
</style>
<style lang="scss" scoped>
.header-notice {
  display: inline-block;
  transition: all 0.3s;

  span {
    vertical-align: initial;
  }
}
</style>
