<script setup lang="ts">
let pageTitle: any = ref("HOTKING-个人中心-收货地址");
useHead(() => ({
  title: pageTitle.value,
}));
const user = useUserStore();
user.value.adminTitle = "收货地址";
import { useAll } from "~/api/pubAll/service";
const { getUserAddress } = useAll();
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<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: Ref<msgListQuery> = ref(defaultQuery());
async function getUserAddressFun() {
  if (isFetching.value) return;
  isFetching.value = true;
  const res: any = await getUserAddress();
  if (res.httpCode == 200) {
    pageData.value = res.data;
    loading.value = false;
    isEnd.value = true;
  }
}
function linkTo(item: any) {
  if (item) {
    navigateTo({
      path: "/home/user/address/edit",
      query: {
        item: JSON.stringify(item),
      },
    });
  } else {
    navigateTo({
      path: "/home/user/address/edit"
    });
  }
}
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;
    getUserAddressFun();
  }
});
onUnmounted(() => {});
// 滚动事件处理函数
const isBottom = ref(false); // 用于标记是否到底部

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) {
      getUserAddressFun();
    }
  },
  { immediate: true }
);
</script>
<template>
  <div class="admin-boxs-bg">
    <div class="admin-wrap address-wrap">
      <div class="address-con" v-loading="loading">
        <ul class="item-ul">
          <li class="item-li" v-for="(item,idx) in pageData" :key="idx" @click="linkTo(item)">
            <h3>收货人：{{item.consignee}}<span>{{item.mobile}}</span><i v-if="item.isDefault==1">默认</i></h3>
            <div class="address">{{item.province}}{{item.city}}{{item.district}}{{item.address}}</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>
      </div>
    </div>
    <div class="buy-btn-wrap">
      <el-button class="btn_main" @click="linkTo('')">
        新增地址
      </el-button>
    </div>
  </div>
</template>
<style scoped lang="scss">
.address-wrap {
  background: #f7f7f7;
  padding-bottom: 70px;
  :deep(.el-loading-mask) {
    top: 150px;
  }
  .address-con {
    padding: 12px 12px 1px;
  }
}
.buy-btn-wrap {
  display: flex;
  position: fixed;
  bottom: 54px;
  background: #fff;
  width: 100%;
  height: 60px;
  align-items: center;
  justify-content: space-between;
  left: 0;
  padding: 0 30px;
  z-index: 1;
  box-shadow: 0 0 18px rgba(0, 0, 0, 0.1);
  .btn_main {
    border-radius: 25px !important;
    flex: 1;
    height: 42px !important;
    &.is-disabled {
      background: #e9e9e9;
      color: #fff;
    }
  }
}
.item-li {
  background: #fff url(@/assets/img/icon/edit.svg) no-repeat right 12px center;
  background-size: 20px auto;
  padding: 6px 12px 10px 12px;
  border-radius: 4px;
  position: relative;
  & + .item-li {
    margin-top: 12px;
  }
  .address {
    font-size: 12px;
    color: #666;
    padding-right: 25px;
  }
  h3 {
    font-size: 14px;
    position: relative;
    line-height: 26px;
    font-weight: bold;
    margin-bottom: 3px;
    color: #333;
    display: flex;
    align-items: center;
    span {
      font-weight: 400;
      color: #666;
      display: flex;
      align-items: center;
      margin-left: 10px;
    }
    i {
      border: 1px solid $mainColor;
      font-size: 10px;
      font-style: normal;
      height: 16px;
      display: flex;
      align-items: center;
      color: $mainColor;
      padding: 0 3px;
      margin-left: 15px;
    }
  }
}
</style> 
