<template>
  <view class="c-card">
    <page-view
      @loadList="
        (v) => {
          getList(v);
        }
      "
      :noMoreData="pageList.noMoreData"
      :list="pageList.list"
      :height="0"
      :isLoading="pageList.isLoading"
    >
      <view
        v-for="(item, index) in pageList.list"
        :key="index"
        @click="getDetail(item)"
        class="box"
      >
        <view class="bill">
          <view
            >{{ state.type === "1" ? "收货" : "出货" }}单号：{{
              state.type === "1" ? item.receiptNumber : item.orderNumber
            }}</view
          >
          <view
            >{{ state.type === "1" ? "应付" : "应收" }}金额：<text class="num"
              >¥{{
                state.type === "1" ? formatMoney(item.balanceTotalPrice)  : formatMoney(item.totalPrice)
              }}</text
            ></view
          >
        </view>
        <view class="time"> {{ item.createTime }} </view>
      </view>
    </page-view>
  </view>
</template>
<script setup lang="ts">
import PageView from "@/components/PageView/index.vue";
import { ReceiveApi, ShipmentApi } from "@/services";
import { formatMoney } from "@/utils";
import { ScaleStatus } from "@/utils/enum";
import { onLoad } from "@dcloudio/uni-app";
const pageList: PageResult<Order> = reactive({
  isLoading: false,
  noMoreData: false,
  total: 0,
  list: [],
  pageNum: 1,
  pageSize: 10,
});
const state = reactive({
  id: "",
  type: "",
  name: "",
});
const getDetail = (item: Order | Shipment) => {
  uni.navigateTo({
    url: `/pagesStatistics/${
      state.type === "1" ? "supplier" : "customer"
    }/detail?name=${state.name}&obj=${JSON.stringify(item)}`, // 要跳转到的页面路径
  });
};
const getList = (v?: boolean) => {
  if (v) {
    if (Math.ceil(pageList.total / pageList.pageSize) > pageList.pageNum) {
      pageList.pageNum++;
    } else {
      pageList.noMoreData = true;
      return;
    }
  }
  pageList.isLoading = true;
  let params: any = {
    pageSize: pageList.pageSize,
    pageNumber: pageList.pageNum,
    scaleStatus: ScaleStatus.ToBePay,
  };
  if (state.id) {
    params.userId = state.id;
  }

  if (state.type === "1") {
    ReceiveApi.getOrderPage(params).then((res) => {
      if (res.code === 200) {
        pageList.isLoading = false;
        (pageList as any).list = (res.data.list as any).map((item: any) => {
          return { ...item, isChecked: false };
        });
        pageList.total = (res.data as any).total;
        
      }
    });
  } else if (state.type === "2") {
    ShipmentApi.getOrderPage(params).then((res) => {
      if (res.code === 200) {
        pageList.isLoading = false;
        (pageList as any).list = (res.data.list as any).map((item: any) => {
          return { ...item, isChecked: false };
        });
        pageList.total = (res.data as any).total;
        
      }
    });
  }
};

onLoad((option: any) => {
  const id = option.id;
  state.type = option.type;
  state.id = id;
  state.name = option.name;
  getList();
  // 设置页面标题
  uni.setNavigationBarTitle({
    title: state.name,
  });
});
</script>
<style lang="scss" scoped>
.c-card {
  margin: 30rpx 25rpx;
  background: #ffffff;
  box-shadow: 0rpx 0rpx 10rpx 0rpx rgba(5, 68, 37, 0.12);
  border-radius: 13rpx;
  padding: 20rpx;
  font-family: Source Han Sans CN;
  font-weight: 400;
  font-size: 26rpx;
  color: #000000;
  .box {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    padding: 15rpx 0rpx;
    .num {
      color: rgba(236, 15, 62, 1);
    }
    .bill {
      view {
        line-height: 60rpx;
      }
    }
    .time {
      font-family: Source Han Sans CN;
      font-weight: 400;
      font-size: 22rpx;
      color: #999999;
    }
  }
  .box + .box {
    border-top: 1px solid rgba(233, 233, 233, 0.76);
  }
}
</style>
