<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());
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);
      });
      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: "/home/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;
});

const navType: any = ref("");
watch(
  () => navType.value,
  (newValue: any, oldValue: any) => {
    if (!process.server && oldValue != undefined) {
      getMsgListFun();
    }
  },
  { immediate: true }
);
let InquiryStatus = ref([
  {
    label: "全部",
    value: "",
  },
  {
    label: "询价中",
    value: "-2",
  },
  {
    label: "已更新",
    value: "-1",
  },
  {
    label: "已关闭",
    value: "0",
  },
]);

const navClick = (keys: any) => {
  navType.value = keys;
  loading.value = true;
  query.value.pageNum = 1;
  isEnd.value = false;
  pageData.value = [];
  window.scrollTo({
    top: 0,
    behavior: "smooth", // 平滑滚动
  });
  navigateTo({
    path: "/admin/Inquiry",
    query: { type: keys },
    replace: true,
  });
};
</script>
<template>
  <div class="admin-boxs-bg">
    <div class="admin-wrap Inquiry-wrap">
      <el-affix :offset="43">
        <div class="storehouse-nav-box">
          <ul class="storehouse-nav">
            <li v-for="(item) in InquiryStatus" :key="item.value" @click="navClick(item.value)" :class="{'current':navType == item.value}">
              {{ item.label }}
            </li>
          </ul>
        </div>
      </el-affix>
      <div class="Inquiry-con" v-loading="loading">
        <ul class="item-ul">
          <li class="item-li" v-for="(item,idx) in pageData" :key="idx" @click="linkTo()">
            <h3>型号型号<span>已关闭</span></h3>
            <div class="time item-div">
              <span class="label">提交时间</span>
              <span class="val">2025-10-25 00:00:00</span>
            </div>
            <div class="number item-div">
              <span class="label">数量</span>
              <span class="val">500</span>
            </div>
            <div class="brand item-div">
              <span class="label">品牌</span>
              <span class="val">合科泰</span>
            </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>
  </div>
</template>
<style scoped lang="scss">
.Inquiry-wrap {
  background: #f7f7f7;
  :deep(.el-loading-mask) {
    top: 150px;
  }
  .Inquiry-con {
    padding: 5px 12px 1px;
  }
  .el-affix {
    :deep(.el-affix--fixed) {
      top: 44px !important;
      .storehouse-nav {
        background: #fff;
        box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
      }
    }
  }
  .storehouse-nav-box {
    width: 100%;
    height: 38px;
  }
  .storehouse-nav {
    display: flex;
    flex-direction: row;
    height: 38px;
    padding: 0 15px;
    li {
      font-size: 14px;
      height: 38px;
      line-height: 41px;
      position: relative;
      padding: 0 10px;
      min-width: max-content;
      &.current {
        color: $mainColor;
      }
      i {
        padding: 0 5px;
        height: 16px;
        color: #fff;
        background: $mainColor;
        position: absolute;
        font-size: 10px;
        border-radius: 30px;
        top: 0px;
        right: -8px;
        min-width: 16px;
        display: flex;
        align-items: center;
        justify-content: center;
        font-style: unset;
      }
    }
  }
}
.item-li {
  background: #fff;
  padding: 12px 12px 7px 12px;
  border-radius: 4px;
  position: relative;
  & + .item-li {
    margin-top: 12px;
  }
  .item-div {
    line-height: 28px;
    font-size: 14px;
    color: #999;
    display: flex;
    justify-content: space-between;
    .val {
      color: #333;
    }
  }
  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: 26px;
    font-weight: bold;
    color: #333;
    display: flex;
    justify-content: space-between;
    span {
      font-weight: 400;
      color: #666;
      padding: 0px 10px;
      background: #e8e8e8;
      border-radius: 20px;
      font-size: 12px;
      display: flex;
      align-items: center;
    }
  }
}
</style> 
