<template>
  <div class="message">
    <el-popover placement="bottom" :width="310" trigger="click">
      <template #reference>
        <el-badge v-if="newNotice > 0" :value="newNotice" class="item">
          <i :class="'iconfont icon-xiaoxi'" class="toolBar-icon"></i>
        </el-badge>
        <i v-else :class="'iconfont icon-xiaoxi'" class="toolBar-icon"></i>
      </template>
      <el-tabs v-model="activeName">
        <el-tab-pane :label="`通知(${newNotice})`" name="first">
          <div class="message-list">
            <template v-for="(item, index) in newsList" :key="index">
              <div class="message-item">
                <img src="@/assets/images/msg01.png" alt="" @click="onNewsClick(index)" class="message-icon" />
                <div class="message-content">
                  <span class="message-title">{{ item.message }}</span>
                  <span class="message-date">{{ item.time }}</span>
                </div>
                <span v-if="item.read" class="el-tag el-tag--success el-tag--mini read">已读</span>
                <span v-else class="el-tag el-tag--danger el-tag--mini read">未读</span>
              </div>
            </template>
            <!-- <div class="message-item">
              <img src="@/assets/images/msg02.png" alt="" class="message-icon" />
              <div class="message-content">
                <span class="message-title">一键三连 eco 💙🧡</span>
                <span class="message-date">一小时前</span>
              </div>
            </div>
            <div class="message-item">
              <img src="@/assets/images/msg03.png" alt="" class="message-icon" />
              <div class="message-content">
                <span class="message-title">一键三连 eco 💚</span>
                <span class="message-date">半天前</span>
              </div>
            </div>
            <div class="message-item">
              <img src="@/assets/images/msg04.png" alt="" class="message-icon" />
              <div class="message-content">
                <span class="message-title">一键三连 eco 💜</span>
                <span class="message-date">一星期前</span>
              </div>
            </div>
            <div class="message-item">
              <img src="@/assets/images/msg05.png" alt="" class="message-icon" />
              <div class="message-content">
                <span class="message-title">一键三连 eco 💛</span>
                <span class="message-date">一个月前</span>
              </div>
            </div> -->
          </div>
        </el-tab-pane>
        <el-tab-pane label="消息(0)" name="second">
          <div class="message-empty">
            <img src="@/assets/images/notData.png" alt="notData" />
            <div>暂无消息</div>
          </div>
        </el-tab-pane>
        <el-tab-pane label="待办(0)" name="third">
          <div class="message-empty">
            <img src="@/assets/images/notData.png" alt="notData" />
            <div>暂无代办</div>
          </div>
        </el-tab-pane>
      </el-tabs>
    </el-popover>
  </div>
</template>

<script setup lang="ts">
import useNoticeStore from '@/stores/modules/notice'
const noticeStore = storeToRefs(useNoticeStore())
// const { readAll } = useNoticeStore()
const activeName = ref('first')
const newNotice = ref(0)
// 定义变量内容
const state = reactive({
  loading: false
})
const newsList = ref([]) as any
//点击消息，写入已读
const onNewsClick = (item: any) => {
  newsList.value[item].read = true
  //并且写入pinia
  noticeStore.state.value.notices = newsList.value
}
/**
 * 初始化数据
 * @returns
 */
const getTableData = async () => {
  state.loading = true
  newsList.value = noticeStore.state.value.notices
  state.loading = false
}

onMounted(() => {
  nextTick(() => {
    getTableData()
  })
})
//用深度监听 消息
watch(
  () => noticeStore.state.value.notices,
  newVal => (newNotice.value = newVal.filter((item: any) => !item.read).length),
  { deep: true }
)
</script>

<style scoped lang="scss">
.message-empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 260px;
  line-height: 45px;
}
.message-list {
  display: flex;
  flex-direction: column;
  min-height: 260px;
  .message-item {
    display: flex;
    align-items: center;
    padding: 20px 0;
    border-bottom: 1px solid var(--el-border-color-light);
    &:last-child {
      border: none;
    }
    .message-icon {
      width: 40px;
      height: 40px;
      margin: 0 20px 0 5px;
      cursor: pointer;
    }
    .message-content {
      display: flex;
      flex-direction: column;
      margin-right: 20px;
      .message-title {
        margin-bottom: 5px;
      }
      .message-date {
        font-size: 12px;
        color: var(--el-text-color-secondary);
      }
    }
  }
}
</style>
