<script setup lang="ts">
let pageTitle: any = ref("HOTKING-个人中心-系统消息");
useHead(() => ({
  title: pageTitle.value,
}));
const user = useUserStore();
user.value.adminTitle = "系统消息";
import empty from "@/assets/img/empty.svg";
import { useAll } from "~/api/pubAll/service";
const {
  messagePage,batchMessageIsRead
} = useAll();
import type msgListQuery from "~/api/msg/msgQuery";
import { useMsg } from "~/api/msg/msgService";
const { getMsgList, getMsgSummary } = useMsg();
import type msgType from "~/domain/Msg";
import { DefaultQuery, pageSizes } from "~/domain/query";
const adminListStore = useAdminListStore();
const route = useRoute();
let loading = ref(false);
let isEnd = ref(false);
let isFirst = ref(true);
let isFetching: any = ref(false);
let total = ref(0);
let open:any = ref({
  detail:false
})
let detailInfo:any = ref({})
const pageData: any = ref([]);
const defaultSort = { prop: "createTime", isAsc: "descending" };
const defaultQuery = () => {
  const q = new DefaultQuery({
    orderByColumn: defaultSort.prop,
    isAsc: defaultSort.isAsc,
  }) as msgListQuery;
  q.pageSize = 10;
  return q;
};
const query: any = ref({"pageNum":1,"pageSize":10,"status":""});
watch(
  () => user.value.msgLoad,
  (newValue: any, oldValue: any) => {
    if (user.value.msgLoad) {
      //getMsgListFun()
      pageData.value.forEach((item: any) => {
        item.readStatus = "done";
      });
    }
  },
  { immediate: true }
);
async function getMsgListFun() {
  if (isFetching.value) return;
  isFetching.value = true;
  const q = JSON.parse(JSON.stringify(query.value));
  const res: any = await messagePage(q);
  if (res.httpCode == 200) {
    setTimeout(() => {
      res?.data?.records?.forEach((item: msgType) => {
        pageData.value.push(item);
      });
      query.value.pageNum++;
      total.value = res.data.total;
      loading.value = false;
      if (
        res?.data?.records.length == 0 ||
        pageData.value.length == res.data.total ||
        res?.data?.records.length < query.value.pageSize
      ) {
        isEnd.value = true;
      }
    }, 300);
  }
  setTimeout(() => {
    isFirst.value = false;
  }, 300);
  setTimeout(() => {
    isFetching.value = false;
  }, 500);
}
async function batchMessageIsReadFun(item: any) {
  open.value.detail = true
  detailInfo.value = item
  if(item.status==1) return 
  const res: any = await batchMessageIsRead(item.id);
  if(res.httpCode == 200){
    item.status = 1
  }
}
onMounted(() => {
  const previousRoute: any = route.meta.previousRoute || "";
  if (
    adminListStore.value.msgListData.data &&
    previousRoute.indexOf("/detail") > -1
  ) {
    isFirst.value = false;
    query.value = adminListStore.value.msgListData.query;
    pageData.value = adminListStore.value.msgListData.data;
    isEnd.value = adminListStore.value.msgListData.loading;
    loading.value = false;
    setTimeout(() => {
      window.scrollTo({
        top: adminListStore.value.msgListData.scrollPosition,
        left: 0, // 水平滚动距离
      });
    }, 10);
  } else {
    loading.value = true;
    getMsgListFun();
  }
  window.addEventListener("scroll", onScroll);
});
onUnmounted(() => {
  window.removeEventListener("scroll", onScroll);
});
// 滚动事件处理函数
const isBottom = ref(false); // 用于标记是否到底部
const onScroll = debounce((event: any) => {
  if (isFirst.value) {
    return;
  }
  const scrollTop = window.scrollY || document.documentElement.scrollTop; // 当前滚动位置
  const clientHeight = document.documentElement.clientHeight; // 可视区域高度
  const scrollHeight = document.documentElement.scrollHeight; // 整个文档高度
  // 判断是否滚动到底部（允许 10px 的偏差）
  isBottom.value = scrollTop + clientHeight >= scrollHeight - 100;
  if (isBottom.value) {
    if (
      !loading.value &&
      !isEnd.value &&
      user.value.isLogin &&
      !isFetching.value
    ) {
      getMsgListFun();
    }
  }
}, 500); // 设置200ms的防抖时间

onBeforeUnmount(() => {
  adminListStore.value.msgListData.data = pageData.value;
  adminListStore.value.msgListData.query = query.value;
  adminListStore.value.msgListData.scrollPosition =
    document.documentElement.scrollTop;
  adminListStore.value.msgListData.loading = isEnd.value;
});
</script>
<template>
  <div class="admin-boxs-bg">
    <div class="admin-wrap msg-wrap" v-loading="loading">
      <ul class="message-ul">
        <li class="message-li" v-for="item in pageData" :key="item.messageId" @click="batchMessageIsReadFun(item)" :class="{'unread':item.status==0}">
          <h3>{{ item.title }}</h3>
          <div class="message-describe" v-html="item.message"></div>
          <p>{{item.sendTime}}</p>
        </li>
      </ul>
      <img class="loading" src="@/assets/img/ico/loading.gif" v-if="!isEnd && user.isLogin && !isFirst && pageData.length>0" />
      <template v-if="pageData.length<1 && isEnd">
        <el-empty description=" " :image="empty" :image-size="350">
          <div class="noContent">
            没有数据
          </div>
        </el-empty>
      </template>
      <p class="loading-text" v-else-if="isEnd && user.isLogin">--&nbsp;已经到底了&nbsp;--</p>
    </div>
  </div>
  <el-dialog class="announcement-dialog" v-model="open.detail" width="80%" destroy-on-close append-to-body :z-index="2200" :show-close='false' :close-on-press-escape="false" :close-on-click-modal="false" :align-center='true'>
    <div class="announcement-wrap">
      <h3>
        {{detailInfo.title}}
      </h3>
      <div class="con mb12">
        {{detailInfo.message}}
      </div>
      <div class="btn-wrap">
        <el-button class="btn_main" @click="open.detail = false">
          我知道了
        </el-button>
      </div>
    </div>
  </el-dialog>
</template>
<style scoped lang="scss">
.msg-wrap {
  padding: 12px 12px 1px;
  background: #f7f7f7;
}
.message-li {
  background: #fff;
  padding: 12px;
  border-radius: 4px;
  position: relative;
  & + .message-li {
    margin-top: 12px;
  }
  .message-describe,
  h3 {
    overflow: hidden;
    text-overflow: ellipsis;
    word-wrap: break-word;
    word-break: break-all;
    -webkit-line-clamp: 2;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    font-size: 12px;
    line-height: 20px;
    color: #999;
  }
  h3 {
    -webkit-line-clamp: 1;
    font-size: 14px;
    margin-bottom: 5px;
    position: relative;
    line-height: 22px;
    font-weight: bold;
    padding-left: 5px;
  }
  p {
    font-size: 12px;
    line-height: 20px;
    color: #999;
    margin-top: 5px;
  }
  &.unread {
    h3 {
      color: #333;
    }
    .message-describe {
      color: #666;
    }
    &::before {
      content: "";
      width: 6px;
      height: 6px;
      background: $mainColor;
      border-radius: 50%;
      position: absolute;
      top: 19px;
      left: 8px;
    }
  }
}
</style> 
