<template>
  <tips-custom>
    <template #default>
      <el-button type="primary" text @click="readAll">全部标记为已读</el-button>
    </template>
  </tips-custom>
  <el-card shadow="hover">
    <div>
      <el-empty description="暂无数据" v-if="data.length === 0"></el-empty>
      <div class="flex flex-col" v-else>
        <el-collapse @change="change" accordion>
          <el-collapse-item v-for="i in data" :name="i.id" :key="i.id">
            <template #title>
              <div class="flex flex-center flex-justify-between full-width">
                <div
                  class="padding-left flex flex-align-center flex-justify-start"
                >
                  <div
                    class="dot"
                    :style="i.readFlag === 0 ? '' : 'background-color: #d6d6d6'"
                  />
                  <span>{{ i.titile }}</span>
                </div>
                <span class="padding-right">{{ i.createTime }}</span>
              </div>
            </template>
            <div class="full-width flex flex-justify-start padding">
              <span v-if="i.msgCategory === 1" class="ml-20">{{
                i.msgContent
              }}</span>
              <msg2 v-else-if="i.msgCategory === 2" :info="i" />
            </div>
          </el-collapse-item>
        </el-collapse>
        <div class="full-width flex flex-justify-between mt-20">
          <div></div>
          <div class="flex flex-center">
            <span class="mr-15">共{{ total }}条</span>
            <el-pagination
              layout="prev, pager, next"
              :total="total"
              :page-size="page.size"
              @current-change="changePage"
              v-model:current-page="page.current"
            />
          </div>
        </div>
      </div>
    </div>
  </el-card>
</template>

<route>
{
name: '通知中心',
meta: { 'back':true, 'show':false}
}
</route>
<script>
import msg2 from '@/views/msg/component/msg2.vue'
import tipsCustom from '@/components/tips-custom/index.vue'

export default {
  components: {
    msg2,
    tipsCustom
  },
  data() {
    return {
      data: [],
      page: {
        current: 1,
        size: 15
      },
      total: ''
    }
  },
  created() {
    this.list()
  },
  methods: {
    list() {
      this.$api.msg.list(this.page).then(res => {
        if (res.code === 200) {
          this.data = res.data.records
          this.total = res.data.total
        }
      })
    },
    changePage(val) {
      this.page.current = val
      this.list()
    },
    readAll() {
      this.$api.msg.readAll().then(res => {
        if (res.code === 200) {
          this.$message.success(res.msg)
          this.list()
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    change(row) {
      const tmp = this.data.find(ele => ele.id === row)
      if (tmp && tmp.readFlag === 1) {
        return
      }
      this.$api.msg.markRead({ id: row }).then(res => {
        if (res.code === 200) {
          this.data = this.data.map(ele => {
            if (ele.id === row) {
              ele.readFlag = 1
              this.$bus.emit('read')
            }
            return ele
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.dot {
  background-color: red;
  width: 10px;
  height: 10px;
  margin-right: 10px;
  border-radius: 10px;
}

:deep(.el-collapse-item__header):hover {
  background-color: #efefef;
}
</style>
