<template>
  <page-view
    @loadList="
      (v) => {
        getList(v);
      }
    "
    :noMoreData="pageList.noMoreData"
    :list="pageList.list"
    :height="0"
    :isLoading="pageList.isLoading"
  >
    <view class="msg-box" v-if="pageList.list.length > 0">
      <view class="u-page">
        <u-list :height="'auto'">
          <u-list-item v-for="(item, index) in pageList.list" :key="index">
            <!-- #ifdef MP-WEIXIN -->
            <u-cell @click="handleItem(item)">
              <view slot="title" class="u-slot-title">
                <view>
                  <view class="u-cell-text title"
                    >{{ item.title }}
                    <text class="title-badge" v-if="item.status === 0"> </text>
                  </view>
                  <view class="u-cell-text desc">{{ item.content }}</view>
                </view>
              </view>
              <template #value>
                <text class="u-slot-value time">{{ item.createTime }}</text>
              </template>
            </u-cell>
            <!-- #endif -->
            <!-- #ifndef MP-WEIXIN -->
            <view class="u-slot-title-content" @click="handleItem(item)">
              <view>
                <view class="u-cell-text title"
                  >{{ item.title }}
                  <text class="title-badge" v-if="item.status === 0"> </text>
                </view>
                <view class="u-cell-text desc">{{ item.content }}</view>
                <view
                  ><text class="u-slot-value time">{{
                    item.createTime
                  }}</text></view
                >
              </view>
            </view>
            <!-- #endif -->
          </u-list-item>
        </u-list>
      </view>
    </view>
  </page-view>
</template>
<script setup lang="ts">
import PageView from "@/components/PageView/index.vue";
import { MessageApi, ReceiveApi, ShipmentApi } from "@/services";
import { MsgType, ScaleStatus } from "@/utils/enum";
const pageList: PageResult<any> = reactive({
  isLoading: false,
  noMoreData: false,
  total: 0,
  list: [],
  pageNum: 1,
  pageSize: 10,
});
const resetPageList = () => {
  pageList.noMoreData = false;
  pageList.total = 0;
  pageList.list = [];
  pageList.pageNum = 1;
  pageList.pageSize = 10;
};

const getList = (v?: boolean) => {
  if (v) {
    if (Math.ceil(pageList.total / pageList.pageSize) > pageList.pageNum) {
      pageList.pageNum++;
    } else {
      pageList.noMoreData = true;
      return;
    }
  }
  let params: any = {
    pageSize: pageList.pageSize,
    pageNum: pageList.pageNum,
  };

  pageList.isLoading = true;
  MessageApi.getUserNoticeInfoVoPage({
    ...params,
  }).then((res: any) => {
    if (res.code === 200) {
      pageList.isLoading = false;
      (pageList as any).list = (pageList as any).list = pageList.list.concat(
        res.data.list
      );
      pageList.total = (res.data as any).total;
    }
  });
};

const handleItem = (item: any) => {
  if (item.status === 0) {
    MessageApi.readNotice({ ids: [item.id] }).then((res) => {
      if (res.code === 200) {
        resetPageList();
        getList();
      }
    });
  }
  if (
    MsgType.ToBePriced === item.msgType ||
    MsgType.ToBeReview === item.msgType
  ) {
    ReceiveApi.getOrderPage({
      pageSize: 10,
      pageNumber: 1,
      receiptNumber: item.business,
    }).then((res) => {
      if (res.code === 200) {
        if (res.data.list.length === 0) {
          uni.showToast({
            icon: "none",
            title: "该订单已处理",
          });
          return;
        } else {
          if (
            res.data.list[0].scaleStatus === ScaleStatus.ToBePriced &&
            MsgType.ToBePriced === item.msgType
          ) {
            uni.navigateTo({
              url: "/pagesReceive/pricing?scaleStatus=0", // 要跳转到的页面路径
            });
          } else if (
            res.data.list[0].scaleStatus === ScaleStatus.ToBeReview &&
            MsgType.ToBeReview === item.msgType
          ) {
            uni.navigateTo({
              url: "/pagesReceive/payReview?scaleStatus=2", // 要跳转到的页面路径
            });
          } else {
            uni.showToast({
              icon: "none",
              title: "该订单已处理",
            });
            return;
          }
        }
      }
    });
  } else if (
    MsgType.ToBeShipment === item.msgType ||
    MsgType.ToBeShipmentReview === item.msgType
  ) {
    ShipmentApi.getOrderPage({
      pageSize: 10,
      pageNumber: 1,
      orderNumber: item.business,
    }).then((res) => {
      if (res.code === 200) {
        if (res.data.list.length === 0) {
          uni.showToast({
            title: "该订单已处理",
          });
          return;
        } else {
          if (
            res.data.list[0].scaleStatus === ScaleStatus.ToBeShipment &&
            MsgType.ToBeShipment === item.msgType
          ) {
            uni.navigateTo({
              url: "/pagesShipment/shipmenting?scaleStatus=0", // 要跳转到的页面路径
            });
          } else if (
            res.data.list[0].scaleStatus === ScaleStatus.ToBeShipmentReview &&
            MsgType.ToBeShipmentReview === item.msgType
          ) {
            uni.navigateTo({
              url: "/pagesShipment/shipmentSettlement?scaleStatus=2", // 要跳转到的页面路径
            });
          } else {
            uni.showToast({
              title: "该订单已处理",
            });
            return;
          }
        }
      }
    });
  }
};
onMounted(() => {
  getList();
});
</script>
<style lang="scss">
.msg-box {
  // background: #f8f8f8;
  width: 100%;
  .u-slot-title-content {
    padding: 13px 15px;
    font-size: 15px;
    color: #303133;
    border-bottom: 1px solid rgba(0, 0, 0, 0.15);
  }

  .title {
    font-size: 28rpx;
    font-family: Source Han Sans CN;
    font-weight: 400;
    color: #000000;
    position: relative;
    width: fit-content;
    .title-badge {
      position: absolute;
      right: -6px;
      top: 0px;
      width: 17rpx;
      height: 17rpx;
      background: #ec0f3e;
      border-radius: 50%;
      display: inline-block;
    }
  }
  .desc {
    font-size: 24rpx;
    font-family: Source Han Sans CN;
    font-weight: 400;
    color: #999999;
    margin-top: 12.18rpx;
  }
  .time {
    font-size: 24rpx;
    font-family: Source Han Sans CN;
    font-weight: 400;
    color: #999999;
  }
}
</style>
