import { useCallback, useEffect, useState } from 'react' import Taro, { useRouter } from '@tarojs/taro' import { View, Text, Image, ScrollView } from '@tarojs/components' import { Empty, Popup } from '@antmjs/vantui' import { getBillDetailApi } from '@/services/bill' import { getOrderDetailApi } from '@/services/order' import { ORDER_STATUS_TEXT } from '@/types/order' import {formatRetailPrice, formatSpec, resolveFileUrl} from '@/utils/format' import type { OrderDetail } from '@/types/order' import type { BillDetail } from '@/services/bill' import './index.less' /** * 账单详情 * 账单信息 + 金额构成(商品/配送费/筐托盘附加)+ 合并商品明细 + 关联订单(可下钻订单明细) */ export default function BillDetailPage() { const router = useRouter() const id = Number(router.params.id ?? 0) const [detail, setDetail] = useState(null) const [loading, setLoading] = useState(false) /** 关联订单明细弹层 */ const [showOrder, setShowOrder] = useState(false) const [orderDetail, setOrderDetail] = useState(null) useEffect(() => { if (!id) return setLoading(true) getBillDetailApi(id) .then(res => setDetail(res.data)) .catch(() => {}) .finally(() => setLoading(false)) }, [id]) /** 下钻关联订单明细 */ const handleOrderTap = useCallback(async (orderId: number) => { try { const res = await getOrderDetailApi(orderId) setOrderDetail(res.data) setShowOrder(true) } catch { // 错误已由 request 层 toast } }, []) /** 去付款 → 发起付款页(预选本账单) */ const goPay = () => { Taro.navigateTo({ url: `/pages/payment/index` }) } if (loading && !detail) { return } if (!detail) { return } const { bill, items, orders } = detail /** 筐/托盘明细:正压负回,数量取绝对值(单价为出账时快照) */ const boxTotalPrice = (Number(bill.box_price) * bill.box_num).toFixed(2) const trayTotalPrice = (Number(bill.tray_price) * bill.tray_num).toFixed(2) return ( {/* ===== 账单信息 ===== */} {bill.bill_no} {bill.pay_state_name} {bill.pay_state === 1 && ( 已提交付款凭证,待后台审核;审核拒绝后将回到待支付 )} 账单日期 {bill.bill_date} {bill.purchase && ( 关联采购单 {bill.purchase.purchase_no}({bill.purchase.purchase_date}) )} 应结算日期 {bill.settlement_date} {bill.pay_state === 2 && bill.paid_at && ( 付款时间 {bill.paid_at} )} {bill.pay_remark && ( 付款备注 {bill.pay_remark} )} {bill.remark && ( 账单备注 {bill.remark} )} {/* ===== 金额构成 ===== */} 金额明细 商品金额 ¥{bill.product_amount} 配送费 ¥{bill.delivery_fee} 周转筐({bill.box_price} × {bill.box_num}) {Number(boxTotalPrice) < 0 ? `- ¥${boxTotalPrice}` : `¥${boxTotalPrice}`} 周转托盘({bill.tray_price} × {bill.tray_num}) {Number(trayTotalPrice) < 0 ? `- ¥${trayTotalPrice}` : `¥${trayTotalPrice}`} 售后 {Number(bill.after_sale) < 0 ? `- ¥${bill.after_sale}` : `¥${bill.after_sale}`} 账单总额 ¥{bill.total_amount} {/* ===== 商品明细(跨订单按商品合并) ===== */} 商品明细({items?.length ?? 0}) {(items ?? []).map(item => ( {!!item.image && ( )} {item.product_name} {formatSpec(item.product_spec, item.unit)} 单价:{formatRetailPrice(item.price, item.spec)} {item.price_unit} ¥{item.price} × {item.quantity} ¥{item.amount} ))} {(items ?? []).length === 0 && } {/* ===== 关联订单 ===== */} 关联订单({orders?.length ?? 0}) {(orders ?? []).map(order => ( handleOrderTap(order.id)}> {order.order_no} {order.order_date} {ORDER_STATUS_TEXT[order.status] || ''} ¥{order.total_amount} ))} {(orders ?? []).length === 0 && } {/* ===== 订单明细弹层 ===== */} setShowOrder(false)} > {orderDetail && ( {orderDetail.order_no} {orderDetail.order_date} {ORDER_STATUS_TEXT[orderDetail.status] || ''} {(orderDetail.items ?? []).map(item => ( {item.product_name} {formatSpec(item.product_spec, item.unit)}{' '} 单价:{formatRetailPrice(item.price, item.product_spec)} {item.price_unit} ¥{item.price} × {item.quantity} ))} 合计 ¥{orderDetail.total_amount} )} {/* ===== 去付款操作栏(可付款账单) ===== */} {bill.can_pay && ( 账单总额 ¥{bill.total_amount} 去付款 )} ) }