<template>
  <view class="body" v-if="list">
    <shadow-card :list="list">
      <view class="flex-column flex-center pb-48">
        <view class="fw-500 color-000">
          <text class="fs-40 mr-8">¥</text>
          <text class="fs-60 lh-60">{{ price }}</text>
        </view>
        <view class="fs-26 mt-8" style="color: rgba(0, 0, 0, 0.4)"
          >实付金额</view
        >
      </view>
    </shadow-card>
  </view>
</template>

<script setup lang="ts">
import { fetchOrder } from "../../api/user";
import { onLoad } from "@dcloudio/uni-app";
import { ref } from "vue";

const list = ref<any[]>();
const price = ref();
onLoad((options: any) => {
  fetchOrder(options.id)
    .then((res) => {
      const totalMoney = (res.totalMoney / 100).toFixed(2);
      const discountAmount = (res.discountAmount / 100).toFixed(2);
      const discountAmountText = res.rightsDesc;
      const startChargeSeq = res.startChargeSeq;
      const reg = new RegExp("\B(?=(\d{3})+(?!\d))", "g");
      const start = new Date(res.startTime.replace(/-/g, "/"));
      const end = res.endTime
        ? new Date(res.endTime.replace(/-/g, "/"))
        : new Date();
      let diff = parseInt(`${(end.getTime() - start.getTime()) / 1000}`);
      if (diff <= 0) {
        diff = 60;
      }
      const min = parseInt(`${diff / 60}`);
      const time =
        min >= 60
          ? `${parseInt(`${min / 60}`)}小时${parseInt(
              `${min - parseInt(`${min / 60}`) * 60}`
            )}分钟`
          : `${parseInt(`${diff / 60}`)}分钟`;
      const _list = [
        {
          label: "累计充电量",
          value: `${res.totalPower}度`,
        },
        {
          label: "订单金额",
          value: `${totalMoney.replace(reg, ",")}元`,
        },
        {
          label: "开始时间",
          value: res.startTime,
          color: "",
        },
        {
          label: "结束时间",
          value: res.chargeStatus === 2 ? "充电中" : res.endTime,
        },
        {
          label: "充电用时",
          value: time,
        },  {
          label: "订单号",
          value: startChargeSeq,
          copy:true
        },
      ];
      if (Number(discountAmount) > 0) {
        _list.splice(2, 0, {
          label: "优惠金额",
          value: `${
            Number(discountAmount) <= 0 ? "" : "-"
          }${discountAmount}元${discountAmountText}`,
          color: "#F43636",
        });
      }
      list.value = _list;
      price.value = (res.payAmount / 100).toFixed(2);
    })
    .catch(() => {
      uni.showModal({
        content: "出现错误，请退出重进",
        success() {
          uni.navigateBack();
        },
      });
    });
});
</script>

<style lang="scss">
.body {
  padding: 30rpx 60rpx;
}
</style>
