<route lang="json5" type="page">
{
  layout: 'default',
  style: {
    navigationBarTitleText: '订单详情',
  },
}
</route>

<template>
  <view class="">
    <wd-cell-group>
      <wd-cell title="订单号" :value="orderDetail.ordersnumber" />
      <wd-cell title="产品名称" :value="orderDetail.ordersproductname" />
      <wd-cell title="产品SN" :value="orderDetail.ordersproductsn" />
      <wd-cell title="订单总金额" :value="`¥${orderDetail.orderstotalprice}`" />
      <wd-cell title="优惠金额" :value="`¥${orderDetail.ordersyhprice}`" />
      <wd-cell title="实付金额" :value="`¥${orderDetail.ordersdiscountprice}`" />
      <wd-cell title="订单状态" :value="getOrderStatus(orderDetail.ordersorderstatus)" />
      <wd-cell title="下单时间" :value="orderDetail.ordersorderdate" />
      <wd-cell title="发票图片">
        <view class="flex flex-wrap">
          <view class="flex-1" v-for="item in invoiceImg" :key="item.id">
            <wd-img :width="100" :height="100" :src="item.url" :enable-preview="true" />
          </view>
        </view>
      </wd-cell>
      <wd-cell title="证件信息图片">
        <view class="flex flex-wrap">
          <view class="flex-1" v-for="item in identityImg" :key="item.id">
            <wd-img :width="100" :height="100" :src="item.url" :enable-preview="true" />
          </view>
        </view>
      </wd-cell>
    </wd-cell-group>

    <!-- 回到订单列表 -->
    <view class="sticky-box-content w-full box-border px-2 pb-2 mt-4">
      <wd-button type="primary" block @click="handleBack">回到订单列表</wd-button>
    </view>
  </view>
</template>

<script lang="ts" setup>
import { useOrderStore } from '@/store/order'
import { useFileStore } from '@/store/file'
import type { OrderType } from '@/types/order'

const orderStore = useOrderStore()
const fileStore = useFileStore()
const currentOrdersId = ref('')
const orderDetail = ref<OrderType>({} as OrderType)

// 获取订单详情
const getOrderDetail = async () => {
  const res = await orderStore.getOrderDetail(currentOrdersId.value)
  orderDetail.value = res
}

// 获取订单状态
const getOrderStatus = (status: number) => {
  const statusMap = {
    0: '待审核',
    1: '已支付',
    2: '已完成',
  }
  return statusMap[status] || '未知状态'
}

// 回到订单列表
function handleBack() {
  uni.navigateTo({
    url: '/pages/order/index',
  })
}

const invoiceImg = ref([]) // 发票图片
const identityImg = ref([]) // 证件信息图片
// 获取发票图片附件
const getInvoiceImg = async () => {
  const res = await fileStore.getAttachmentList({
    attlsh: currentOrdersId.value,
    attmodel: 'orders_invoice',
  })
  invoiceImg.value = res
}

// 获取证件信息图片附件
const getIdentityImg = async () => {
  const res = await fileStore.getAttachmentList({
    attlsh: currentOrdersId.value,
    attmodel: 'orders_identity',
  })
  identityImg.value = res
}

onLoad((query) => {
  currentOrdersId.value = query.ordersid
  getOrderDetail()
  getInvoiceImg()
  getIdentityImg()
})
</script>

<style lang="scss" scoped>
//
</style>
