// const app = getApp()
import uni from '@/utils/uniHooks';
import { invoicesOrders } from '@/api/parking';

export default {
  data() {
    return {
      picUrl: this.$picUrl,
      order: {},
      parFee: '',
      id: '',
      orderList: [],
      optionsQuery: null,
      useParkingOrderCache: false,
    };
  },
  created() {
    const options = this.$route.query;
    this.optionsQuery = options;
    this.id = options.id;
    const useParkingOrderCache = options?.useParkingOrderCache || 'false';
    // 如果是通过"发票申请"页面进入的，可以适用本地缓存直接获取数据
    if (useParkingOrderCache == 'true') {
      this.useParkingOrderCache = true;
      const tempParkingOrder = this.optionsQuery
      this.parFee = Number(tempParkingOrder.money).toFixed(2);
      console.log('tempParkingOrder.invoiceDetailList:::', tempParkingOrder.invoiceDetailList)
      const orderList = JSON.parse(tempParkingOrder.invoiceDetailList);
      console.log(231,options)
      console.log(231, orderList)
      // orderList.forEach((item) => {
      //   item.invoiceCategory = 0;
      // });
      this.orderList = orderList;
    } else {
      this.useParkingOrderCache = false;
      this.getInvoicedDetail(this.id);
    }
  },
  mounted() {
    setTimeout(() => {
      uni.setNavigationBarTitle({
        title: '支付订单详情',
      });
    }, 300);
    setTimeout(() => {
      window?.toWXSendMsg({
        type: 'uni_func',
        funcName: 'setNavigationBarColor',
        options: {
          frontColor: '#000000',
          backgroundColor: '#FBFCFF',
        },
      });
    }, 500)
  },
  filters: {
    parkingTime(val) {
      const days = parseInt(val / 60 / 24);
      const hours = parseInt((val / 60) % 24);
      const minutes = parseInt(val % 60);
      if (days > 0) {
        return `${days}天 ${hours}小时 ${minutes}分钟`;
      }
      if (hours > 0) {
        return `${hours}小时 ${minutes}分钟`;
      }
      return `${minutes}分钟`;
    },
  },
  methods: {
    gotoDetail(item) {
      console.log(this.orderList);
      this.$router.push({
        path: 'parkingFeeDetailSuccess?orderNo=' + item.id,
      });
      // this.$router.push({ path: '/pages/parkingFee/parkingFeeDetailSuccess?orderNo=' + item.orderNo });
    },
    // 根据发票id获取支付记录详情信息
    getInvoicedDetail: async function(id) {
      const self = this;
      try {
        const res = await invoicesOrders(id)
        this.parFee = res.invoiceTotalAmount
        this.orderList = res.invoicedOrders
        console.log(res, '订单支付详情');
        // if (res.data.code === 0) {
        //   self.order = res.data.data.list[0];
        //   self.parFee = (res.data.data.invoiceAmount / 100).toFixed(2);
        //   self.orderList = res.data.data.invoicedOrders;
        // } else {
        //   uni.showToast({
        //     title: res.data.msg,
        //     duration: 2000,
        //     icon: 'none',
        //   });
        // }
      } catch {
        uni.showToast({
          title: '服务器开小差了呢，请您稍后再试',
          icon: 'none',
        });
      }
    },
    getOrderItemPrice(item) {
      if (this.useParkingOrderCache) {
        const payFee = item.totalPaidAmount;
        return payFee;
      } else {
        const payFee = item.totalPaidAmount;
        return payFee;
      }
    },
  },
};
