<template>
  <div class="header-notification">
    <el-popover placement="bottom" width="400" trigger="hover">
      <el-badge slot="reference" :hidden="notifications.length === 0" is-dot class="item">
        <svg-icon class-name="notification-icon" icon-class="bell-fill" />
      </el-badge>
      <div class="notification-header">站内消息</div>
      <el-scrollbar class="notification-item-list" style="height:400px;">
        <div v-show="!notifications.length" class="no-notifications">
          您目前没有新的未读通知消息
        </div>
        <div v-for="notification in notifications" :key="notification.id" class="notification-item">
          <!-- <div class="notification-item-title">
            <i class="el-icon-tickets"></i>
            {{notification.typeName}}
          </div>-->
          <div class="notification-item-content">
            {{ notification.content }}
          </div>
          <div class="bottom clearfix">
            <span class="time">{{ notification.creationTime | datetime }}</span>
            <el-button type="text" class="button" style="float:right;" @click="markAsRead(notification)">标记已读</el-button>
          </div>
        </div>
      </el-scrollbar>
      <div>
        <el-button type="text" @click="handleMore">查看更多</el-button>
      </div>
    </el-popover>
  </div>
</template>

<script>
export default {
  name: 'HeaderNotification',
  props: {
    items: {
      type: Array,
      default() {
        return []
      }
    }
  },
  data() {
    return {
      notifications: this.items
    }
  },
  watch: {
    items(val) {
      this.notifications = val
    }
  },
  methods: {
    markAsRead(notification) {
      this.notifications = this.notifications.filter(t => t.id !== notification.id)
      this.$emit('markAsRead', notification)
    },
    handleMore() {
      this.$router.push({ name: 'notification' })
    }
  }
}
</script>

<style lang="scss" scoped>
.header-notification {
  font-size: 0 !important;

  .item {
    line-height: 20px;
    ::v-deep .el-badge__content {
      padding: 0px 2px;
    }
  }

  .notification-icon {
    cursor: pointer;
    font-size: 21px;
    vertical-align: middle;
  }
}
</style>

<style lang="scss">
.el-popover {
  .notification-item-list {
    .el-scrollbar__wrap {
      overflow-x: auto;
    }

    .no-notifications {
      height: 400px;
      width: 100%;
      vertical-align: middle;
      line-height: 400px;
      text-align: center;
    }
  }

  .notification-header {
    font-weight: bold;
    padding-bottom: 8px;
    border-bottom: 1px solid #ddd;

    .notification-num {
      color: #fb6767;
    }
  }

  .notification-item {
    padding-top: 8px;
    border-bottom: 1px solid #ddd;

    .notification-item-title {
      font-weight: bold;
    }

    .notification-item-content {
      margin-top: 5px;
    }

    .time {
      display: inline-block;
      margin-top: 10px;
    }
  }
}
</style>
