<template>
  <div class="message">
    <el-popover placement="bottom" :width="310" trigger="click">
      <template #reference>
        <el-badge :value="msgList.length" class="item" @click="clickMsg">
          <i :class="'iconfont icon-xiaoxi'" class="toolBar-icon"></i>
        </el-badge>
      </template>
      <!-- <el-tabs v-model="activeName"> -->
      <!-- <el-tab-pane label="通知(5)" name="first"> -->
      <div class="message-list">
        <div
          class="message-item"
          v-for="(item, index) in msgList"
          :key="index"
          @click="clickMessage(item)"
        >
          <img :src="item.serviceType === 'tip' ? msg02 : msg01" alt="" class="message-icon" />
          <div class="message-content">
            <span
              class="message-title"
              :class="{ ' text-gray-400': item.messageReads && item.messageReads.length }"
              >{{ item.title }}</span
            >
            <span class="message-date">{{ timeago(item.sendTime) }}</span>
          </div>
        </div>
        <!-- <div class="message-item">
              <img src="@/assets/images/msg02.png" alt="" class="message-icon" />
              <div class="message-content">
                <span class="message-title">一键三连 Geeker-Admin 💙</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>
    <el-popover :visible="visible" placement="bottom" title="收到1条新消息" :width="200">
      <template #reference>
        <el-badge class="item">
          <i :class="'iconfont icon-xiaoxi'" class="toolBar-icon" style="display: none"></i>
        </el-badge>
      </template>
      <div>
        {{ socketStore.msg.title }}
      </div>
    </el-popover>
  </div>
</template>

<script setup>
  import { reactive, ref, watch } from 'vue';
  import useSocketStore from '../../../../stores/modules/websocket';
  import { useUserStore } from '../../../../stores/modules/user';
  import msg02 from '@/assets/images/msg02.png';
  import msg01 from '@/assets/images/msg01.png';
  import { computed, onMounted } from 'vue';
  import { pageMyMesage, readMessage } from '@/api/message';
  import dayjs from 'dayjs';
  import { useRouter } from 'vue-router';
  const router = useRouter();
  const activeName = ref('first');
  const msgList = ref([]);
  const newMsgList = ref([]);
  const visible = ref(false);
  const socketStore = useSocketStore();
  console.log('socketStore', socketStore);
  const query = () => {
    pageMyMesage({
      current: 1,
      readStatus: 'read',
      size: 100,
    }).then((res) => {
      console.log('sadas', res);
      if (res.success && res.result) {
        msgList.value = res.result.content;
      }
    });
  };
  watch(
    () => socketStore.msg,
    (newValue, oldValue) => {
      if (newValue instanceof Array) {
        newMsgList.value.push(newValue);
        visible.value = true;
      }
      query();
    }
  );
  import { getAppEnvConfig } from '@/utils/env';
  const envs = getAppEnvConfig();
  const { userInfo } = useUserStore();
  if (!socketStore.ws && userInfo.id) {
    let url = `ws://8.152.205.9:8080/websocket/${userInfo.id}`;
    console.log('url', url);
    socketStore.connection(url);
  }
  if (socketStore.ws) {
    socketStore.sendMsg({ aa: 44 });
  }
  const clickMsg = () => {
    visible.value = false;
    newMsgList.value = [];
  };
  const clickMessage = (item) => {
    readMessage({
      msgId: item.id,
    }).then((res) => {
      router.push({
        path: '/workplace/todo',
      });
      query();
    });
  };
  const timeago = (time) => {
    const data = new Date(time);
    const dateTimeStamp = data.getTime();
    const minute = 1000 * 60; //把分，时，天，周，半个月，一个月用毫秒表示
    const hour = minute * 60;
    const day = hour * 24;
    const week = day * 7;
    const month = day * 30;
    const year = month * 12;
    const now = new Date().getTime(); //获取当前时间毫秒
    const diffValue = now - dateTimeStamp; //时间差

    let result = '';
    if (diffValue < 0) {
      result = '' + '未来';
    }
    const minC = diffValue / minute; //计算时间差的分，时，天，周，月
    const hourC = diffValue / hour;
    const dayC = diffValue / day;
    const weekC = diffValue / week;
    const monthC = diffValue / month;
    const yearC = diffValue / year;

    if (yearC >= 1) {
      result = ' ' + parseInt(yearC) + '年前';
    } else if (monthC >= 1 && monthC < 12) {
      result = ' ' + parseInt(monthC) + '月前';
    } else if (weekC >= 1 && weekC < 5 && dayC > 6 && monthC < 1) {
      result = ' ' + parseInt(weekC) + '周前';
    } else if (dayC >= 1 && dayC <= 6) {
      result = ' ' + parseInt(dayC) + '天前';
    } else if (hourC >= 1 && hourC <= 23) {
      result = ' ' + parseInt(hourC) + '小时前';
    } else if (minC >= 1 && minC <= 59) {
      result = ' ' + parseInt(minC) + '分钟前';
    } else if (diffValue >= 0 && diffValue <= minute) {
      result = '刚刚';
    }

    console.log(result);
    return result;
  };

  onMounted(() => {
    query();
  });
</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;
    max-height: 460px;
    overflow: auto;
    .message-item {
      display: flex;
      cursor: pointer;
      align-items: center;
      padding: 6px 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;
      }
      .message-content {
        display: flex;
        flex-direction: column;
        .message-title {
          margin-bottom: 5px;
        }
        .message-date {
          font-size: 12px;
          color: var(--el-text-color-secondary);
        }
      }
    }
  }
</style>
