<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 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);
const pageData: Ref<msgType[]> = 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: Ref<msgListQuery> = ref(defaultQuery());
watch(
  () => user.value.msgLoad,
  (newValue: any, oldValue: any) => {
    if (user.value.msgLoad) {
      //getMsgListFun()
      pageData.value.forEach((item: any) => {
        item.readStatus = "done";
      });
    }
  },
  { immediate: true }
);
function setTime(message: any, todayStart: any, now: any) {
  // 将 createTime 转为 Date 对象
  const createDate: any = new Date(message.createTime);

  // 计算时间差（以天为单位）
  const timeDiff = Math.floor(
    (todayStart -
      new Date(
        createDate.getFullYear(),
        createDate.getMonth(),
        createDate.getDate()
      )) /
      (1000 * 60 * 60 * 24)
  );

  // 根据时间差新增标签
  if (timeDiff === 0) {
    message.timeLabel = "今天";
  } else if (timeDiff === 1) {
    message.timeLabel = "昨天";
  } else if (timeDiff === 2) {
    message.timeLabel = "2天前";
  } else if (timeDiff >= 3 && timeDiff < 7) {
    message.timeLabel = "3天前";
  } else if (timeDiff >= 7) {
    message.timeLabel = "7天前";
  }
}
async function getMsgListFun() {
  if (isFetching.value) return;
  isFetching.value = true;
  const q = JSON.parse(JSON.stringify(query.value));
  const res: any = await getMsgList(q);
  if (res.code == 200) {
    const now: any = new Date(); // 当前时间
    const todayStart: any = new Date(
      now.getFullYear(),
      now.getMonth(),
      now.getDate()
    ); // 今天零点时间戳
    setTimeout(() => {
      res.rows?.forEach((item: msgType) => {
        pageData.value.push(item);
        setTime(item, todayStart, now);
      });
      query.value.pageNum++;
      total.value = res.total;
      loading.value = false;
      if (
        res.rows.length == 0 ||
        pageData.value.length == res.total ||
        res.rows.length < query.value.pageSize
      ) {
        isEnd.value = true;
      }
    }, 300);
  }
  setTimeout(() => {
    isFirst.value = false;
  }, 300);
  setTimeout(() => {
    isFetching.value = false;
  }, 500);
}
function linkTo() {
  navigateTo({
    path: "/admin/support/create",
  });
}
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 support-wrap" v-loading="loading">
      <ul class="item-ul">
        <li class="item-li" v-for="(item,idx) in pageData" :key="idx" @click="linkTo()">
          <h3>商品质量差-退货退款<span>2025-10-50 10:00</span></h3>
          <div class="support-describe">投诉内容投诉内容投诉内容投诉内容投诉内容投诉内容投诉内容投诉内容投诉内容投诉内容投诉内容投诉内容投诉内容投诉内容投诉内容投诉内容投诉内容投诉内容投诉内容投诉内容投诉内容投诉内容投诉内容投诉内容投诉内容投诉内容投诉内容投诉内容投诉内容投诉内容投诉内容投诉内容投诉内容投诉内容投诉内容投诉内容投诉内容投诉内容投诉内容投诉内容投诉内容投诉内容投诉内容</div>
          <div class="other">
            <p>关联订单：1749899555154</p>
            <p>关联商品：稳压二极管 ZMM2V2</p>
          </div>
        </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>
</template>
<style scoped lang="scss">
.support-wrap {
  padding: 12px 12px 1px;
  background: #f7f7f7;
}
.item-li {
  background: #fff;
  padding: 12px;
  border-radius: 4px;
  position: relative;
  & + .item-li {
    margin-top: 12px;
  }
  .support-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: #666;
  }
  h3 {
    -webkit-line-clamp: 1;
    font-size: 14px;
    margin-bottom: 5px;
    position: relative;
    line-height: 22px;
    font-weight: bold;
    color: #333;
    display: flex;
    justify-content: space-between;
    span {
      font-weight: 400;
      color: #666;
    }
  }
  p {
    font-size: 12px;
    line-height: 20px;
    color: #999;
    margin-top: 5px;
  }
}
</style> 
