<template>
  <div class="main">
    <card title="我的消息">
      <div class="message-card" @click="onClick('3')">
        <div class="message-card-pic">
          <img src="~@/assets/img/message_settings_icon.png" />
        </div>
        <div class="message-card-content">
          <h4>系统消息</h4>
          <!-- <p>紫荆官方发布了一条新消息，点击查看</p> -->
          <span class="num" v-if="total.system">{{ total.system }}</span>
        </div>
      </div>
      <div class="message-card" @click="onClick('1')">
        <div class="message-card-pic">
          <img src="~@/assets/img/message_group_icon.png" />
        </div>
        <div class="message-card-content">
          <h4>组织消息</h4>
          <!-- <p>您创建的组织有新的人员加入请求</p> -->
          <span class="num" v-if="total.group">{{ total.group }}</span>
        </div>
      </div>
      <div class="message-card" @click="onClick('2')">
        <div class="message-card-pic">
          <img src="~@/assets/img/message_activity_icon.png" />
        </div>
        <div class="message-card-content">
          <h4>活动消息</h4>
          <!-- <p>您创建的活动有新的人员加入请求</p> -->
          <span class="num" v-if="total.activity">{{ total.activity }}</span>
        </div>
      </div>
    </card>
  </div>
</template>

<script>
import Card from '@/components/Card'
import * as api from '@/api'
export default {
  name: 'MessageIndex',
  components: { Card },
  data() {
    return {
      unreadList: []
    }
  },
  computed: {
    total() {
      return this.unreadList.reduce(
        (result, item) => {
          const map = { 1: 'group', 2: 'activity', 3: 'system' }
          result[map[item.type]] = item.sum
          return result
        },
        { activity: 0, system: 0, group: 0 }
      )
    }
  },
  methods: {
    onClick(type) {
      this.$router.push({ name: 'messageList', params: { type } })
    },
    getUnreadMessage() {
      api.getUnreadMessage().then(response => {
        this.unreadList = response
      })
    }
  },
  beforeMount() {
    this.getUnreadMessage()
  }
}
</script>

<style lang="scss" scoped>
.message-card {
  display: flex;
  padding-bottom: 20px;
  margin-bottom: 20px;
  border-bottom: 1px solid #f1f1f1;
}
.message-card-pic {
  width: 40px;
  height: 40px;
  img {
    width: 100%;
    height: 100%;
    object-fit: contain;
  }
}
.message-card-content {
  position: relative;
  flex: 1;
  margin-left: 10px;
  h4 {
    font-size: 14px;
    color: #333;
    line-height: 40px;
  }
  p {
    margin-top: 6px;
    font-size: 12px;
    color: #999;
  }
  .num {
    position: absolute;
    right: 0;
    top: 50%;
    transform: translateY(-50%);
    width: 20px;
    height: 20px;
    font-size: 12px;
    color: #fff;
    line-height: 20px;
    text-align: center;
    background-color: #AF1B40;
    border-radius: 50%;
    overflow: hidden;
  }
}
</style>
