<script setup lang="ts">
import { ref } from 'vue';
import dayjs from 'dayjs';
import { fetchGetNomalOrderInfo } from '@/service/api/delivery/normal-orde';
import { useAppStore } from '@/store/modules/app';
import { copyTextToClipboard } from '@/utils/zt';
import { useModal } from '@/components/zt/Modal/hooks/useModal';
import { dvyStatus, orderColumns, orderStatusEnum } from '../split-order';
const [registerModal, { openModal, setModalLoading }] = useModal({
  title: '订单详情',
  isShowHeaderText: false,
  showFooter: false,
  width: 1200,
  height: 800
});
const orderInfo = ref<Api.delivery.deliveryOrder>();
const appStore = useAppStore();
const TimeDown = ref<number>(0);
const emit = defineEmits<{
  (e: 'finish'): void;
}>();
async function open(orderNumber: string) {
  openModal();
  setModalLoading(true);
  const { data, error } = await fetchGetNomalOrderInfo(orderNumber);
  if (!error) {
    orderInfo.value = data;
    if (orderInfo.value.hbLogisticStatus == orderStatusEnum.WAIT_PAY) {
      const createTime = dayjs(orderInfo.value.createTime);
      const currentTime = dayjs();
      const elapsed = currentTime.diff(createTime);
      const fifteenMinutesInMillis = 15 * 60 * 1000;
      TimeDown.value = fifteenMinutesInMillis - elapsed;
    }
  }
  setModalLoading(false);
}
defineExpose({ open });
function handleFinish() {
  open(String(orderInfo.value?.orderNumber));
  emit('finish');
}

function handleCopy(value: string | number | undefined) {
  copyTextToClipboard(String(value));
  // copyTextToClipboard(String(orderInfo.value?.orderNumber));
}
</script>

<template>
  <div>
    <BasicModal @register="registerModal" @after-leave="orderInfo = undefined">
      <div v-if="orderInfo">
        <NFlex justify="space-between" align="center">
          <NFlex>
            <NTag v-if="orderInfo.parentOrderNumber">
              <div class="flex items-center">
                父订单编号: {{ orderInfo?.parentOrderNumber }}
                <div @click="handleCopy(orderInfo?.parentOrderNumber)">
                  <SvgIcon icon="bxs:copy" class="mx-3 cursor-pointer text-[#f97316]"></SvgIcon>
                </div>
              </div>
            </NTag>
            <NTag>
              <div class="flex items-center">
                {{ orderInfo.orderLevel === 0 ? '订单编号' : '子订单编号' }}: {{ orderInfo?.orderNumber }}
                <div @click="handleCopy(orderInfo?.orderNumber)">
                  <SvgIcon icon="bxs:copy" class="mx-3 cursor-pointer text-[#f97316]"></SvgIcon>
                </div>
              </div>
            </NTag>
            <NTag>下单时间: {{ orderInfo?.createTime }}</NTag>
          </NFlex>
          <NFlex vertical>
            <template v-if="orderInfo.hbOrderStatus == orderStatusEnum.WAIT_PAY">
              <div class="text-16px font-semibold">商品已拍下,等待买家付款</div>
              <div class="text-gray">
                如买家未在
                <NTag :type="TimeDown > 300094 ? 'success' : 'error'">
                  <NCountdown :duration="TimeDown" @finish="handleFinish" />
                </NTag>
                内付款，订单将 自动关闭。
              </div>
            </template>
            <template v-if="orderInfo.hbOrderStatus == orderStatusEnum.ORDER_ARRIVE">
              <div class="text-16px font-semibold">等待买家收货</div>
              <div>商家已发货，等待确认收货</div>
            </template>
            <template v-if="orderInfo.hbOrderStatus == orderStatusEnum.ORDER_CANCEL">
              <div class="text-16px font-semibold">已取消</div>
              <div>取消原因：{{ orderInfo.cancelReason || '未知取消原因' }}</div>
            </template>
            <template v-if="orderInfo.hbOrderStatus == orderStatusEnum.ORDER_COMPLETE">
              <div class="text-16px font-semibold">交易成功</div>
              <div>买家已收货</div>
            </template>
          </NFlex>
        </NFlex>
        <NDivider />
        <template v-if="orderInfo.hbOrderStatus != orderStatusEnum.ORDER_CANCEL">
          <div class="p3">
            <NSteps :current="3" :vertical="appStore.isMobile">
              <NStep title="用户下单" :description="orderInfo.createTime" />
              <NStep title="买家已付款" :description="orderInfo.payTime" />
              <NStep
                :title="orderInfo.autoSplit == 1 ? '已拆单（自动）' : `已拆单（手动-${orderInfo.splitUserName}）`"
                :description="orderInfo.dvyTime"
              />
            </NSteps>
          </div>
          <NDivider />
        </template>
        <NDescriptions bordered :column="appStore.isMobile ? 1 : 4">
          <NDescriptionsItem label="收货人信息">
            <div>收货人姓名：{{ orderInfo?.userAddrOrder?.receiver || '---' }}</div>
            <div>收货人手机号：{{ orderInfo?.userAddrOrder?.mobile || '---' }}</div>
            <div>收货地址：{{ orderInfo?.userAddrOrder?.address || '---' }}</div>
          </NDescriptionsItem>
          <NDescriptionsItem label="配送信息">
            <div>配送方式: {{ dvyStatus[orderInfo.dvyType as keyof typeof dvyStatus] || '暂无' }}</div>
          </NDescriptionsItem>
          <NDescriptionsItem label="父订单付款信息">
            <div>实付金额：{{ orderInfo.actualTotal }}元</div>
            <div>
              付款方式：{{
                orderInfo.hbOrderStatus == orderStatusEnum.WAIT_PAY ||
                orderInfo.hbOrderStatus == orderStatusEnum.ORDER_CANCEL
                  ? '暂无'
                  : '微信'
              }}
            </div>
            <div>付款时间：{{ orderInfo.payTime || '暂无' }}</div>
          </NDescriptionsItem>
          <NDescriptionsItem label="买家信息">
            <div>买家昵称：{{ orderInfo?.nickName || '---' }}</div>
            <div>买家电话：{{ orderInfo?.userMobile || '---' }}</div>
            <div>买家留言：{{ orderInfo?.remarks || '暂无' }}</div>
          </NDescriptionsItem>
        </NDescriptions>
        <NDivider />
        <NCard title="子订单信息" :bordered="false">
          <NDataTable :columns="orderColumns" :data="orderInfo.orderItems" :bordered="false" />
        </NCard>
        <NCard title="子费用信息" :bordered="false">
          <NTable :single-line="false">
            <NThead>
              <NTr>
                <NTh>费用类型</NTh>
                <NTh>金额/元</NTh>
              </NTr>
            </NThead>
            <NTbody>
              <NTr>
                <NTd>商品总额</NTd>
                <NTd>{{ orderInfo.total }}</NTd>
              </NTr>
              <NTr>
                <NTd>配送费（快递）</NTd>
                <NTd>{{ orderInfo.freightAmount }}</NTd>
              </NTr>
              <NTr>
                <NTd>积分</NTd>
                <NTd>-{{ (Number(orderInfo.offsetPoints) / 100).toFixed(2) || 0 }}</NTd>
              </NTr>
              <NTr>
                <NTd v-if="orderInfo.hbOrderStatus == orderStatusEnum.WAIT_PAY">需付款</NTd>
                <NTd
                  v-if="
                    orderInfo.hbOrderStatus != orderStatusEnum.WAIT_PAY &&
                    orderInfo.hbOrderStatus != orderStatusEnum.ORDER_CANCEL
                  "
                >
                  实际付款
                </NTd>
                <NTd v-if="orderInfo.hbOrderStatus == orderStatusEnum.ORDER_CANCEL">应付款</NTd>
                <NTd>{{ orderInfo.actualTotal }}</NTd>
              </NTr>
            </NTbody>
          </NTable>
        </NCard>
      </div>
    </BasicModal>
  </div>
</template>

<style scoped></style>
