import hocAuth from "@/hocs/auth"; import { CommonComponent } from "@/types/typings"; import Taro, { useDidShow } from "@tarojs/taro"; import { business } from "@/services"; import { useEffect, useState } from "react"; import { View } from "@tarojs/components"; import dayjs from "dayjs"; import purchaseOrder from "@/constant/purchaseOrder"; import { Button, Dialog, SafeArea, Toast, Input, } from "@nutui/nutui-react-taro"; import { formatCurrency } from "@/utils/format"; import { BasicInfoSection, CompanyInfoSection, CostSummarySection, DealerInfoSection, EmptyBoxInfoSection, LaborInfoSection, MarketPriceSection, PackageInfoSection, PackagingCostSection, ProfitCalcSection, PurchaseCostInfoSection, PurchaseOrderRejectApprove, PurchaseOrderRejectFinal, PurchaseOrderFinalApprove, SupplierInfoSection, RebateCalcSection, } from "@/components"; import { getMelonCost1, getTotalGrossWeight, getTotalNetWeight, } from "@/utils/calcutePurchaseOrder"; const sectionList = { // 销售方信息卡片 supplierInfo: CompanyInfoSection, // 下游经销商信息 dealerInfo: DealerInfoSection, // 基础信息 basicInfo: BasicInfoSection, // 瓜农信息 farmerInfo: SupplierInfoSection, // 采购成本 purchaseCostInfo: PurchaseCostInfoSection, // 包装纸箱费 packageInfo: PackageInfoSection, // 空箱费用 emptyBoxInfo: EmptyBoxInfoSection, // 用工信息 laborInfo: LaborInfoSection, // 包装费 packagingCost: PackagingCostSection, // 成本合计 costSummary: CostSummarySection, // 市场报价 marketPrice: MarketPriceSection, // 返点计算 rebateCalc: RebateCalcSection, // 利润计算 profitCalc: ProfitCalcSection, }; const sectionConfig = { supplierInfo: { title: "销售方信息", containerClass: "border-l-8 border-l-blue-500", contentClass: "p-4 bg-white rounded-b-lg shadow-sm overflow-x-auto", }, dealerInfo: { title: "下游经销商信息", containerClass: "border-l-8 border-l-green-500", contentClass: "p-4 bg-white rounded-b-lg shadow-sm overflow-x-auto", }, basicInfo: { title: "基础信息", containerClass: "border-l-8 border-l-yellow-500", contentClass: "p-4 bg-white rounded-b-lg shadow-sm overflow-x-auto", }, farmerInfo: { title: "瓜农信息", containerClass: "border-l-8 border-l-purple-500", contentClass: "p-4 bg-white rounded-b-lg shadow-sm overflow-x-auto", }, purchaseCostInfo: { title: "采购成本", containerClass: "border-l-8 border-l-red-500", contentClass: "p-4 bg-white rounded-b-lg shadow-sm overflow-x-auto", }, packageInfo: { title: "包装纸箱费", containerClass: "border-l-8 border-l-indigo-500", contentClass: "p-4 bg-white rounded-b-lg shadow-sm overflow-x-auto", }, emptyBoxInfo: { title: "空箱费用", containerClass: "border-l-8 border-l-pink-500", contentClass: "p-4 bg-white rounded-b-lg shadow-sm overflow-x-auto", }, laborInfo: { title: "用工信息", containerClass: "border-l-8 border-l-teal-500", contentClass: "p-4 bg-white rounded-b-lg shadow-sm overflow-x-auto", }, packagingCost: { title: "包装费", containerClass: "border-l-8 border-l-orange-500", contentClass: "p-4 bg-white rounded-b-lg shadow-sm overflow-x-auto", }, costSummary: { title: "成本合计", containerClass: "border-l-8 border-l-cyan-500", contentClass: "p-4 bg-white rounded-b-lg shadow-sm overflow-x-auto", }, marketPrice: { title: "市场报价", containerClass: "border-l-8 border-l-lime-500", contentClass: "p-4 bg-white rounded-b-lg shadow-sm overflow-x-auto", }, rebateCalc: { title: "返点计算", containerClass: "border-l-8 border-l-amber-500", contentClass: "p-4 bg-white rounded-b-lg shadow-sm overflow-x-auto", }, profitCalc: { title: "利润计算", containerClass: "border-l-8 border-l-emerald-500", contentClass: "p-4 bg-white rounded-b-lg shadow-sm overflow-x-auto", }, }; export default hocAuth(function Page(props: CommonComponent) { const { router, isInitialized, setIsInitialized, role } = props; const orderId = router.params .orderId as BusinessAPI.PurchaseOrderVO["orderId"]; const [purchaseOrderVO, setPurchaseOrderVO] = useState(); const [originPrincipal, setOriginPrincipal] = useState(""); console.log("purchaseOrderVO-----", purchaseOrderVO); const [collapsedSections, setCollapsedSections] = useState< Record >({ supplierInfo: false, dealerInfo: false, basicInfo: false, farmerInfo: false, purchaseCostInfo: false, packageInfo: false, emptyBoxInfo: false, laborInfo: false, packagingCost: false, costSummary: false, marketPrice: false, rebateCalc: false, profitCalc: false, }); // 暂存和提交审核的Dialog状态 const [saveDialogVisible, setSaveDialogVisible] = useState(false); const [submitDialogVisible, setSubmitDialogVisible] = useState(false); const toggleSection = (section: string) => { setCollapsedSections((prev) => ({ ...prev, [section]: !prev[section], })); }; // 暂存操作 const handleSave = () => { setSaveDialogVisible(true); }; // 确认暂存 const confirmSave = async () => { // 关闭对话框 setSaveDialogVisible(false); // 这里应该调用暂存API console.log("暂存数据:", purchaseOrderVO); const { data } = await business.purchaseOrder.approvePurchaseOrder({ ...purchaseOrderVO!, draft: true, }); if (data.success) { Toast.show("toast", { icon: "success", title: "提示", content: "暂存成功", }); // 返回采购单页面 Taro.switchTab({ url: "/pages/main/purchase/index" }); } }; // 提交老板审核操作 const handleSubmit = () => { setSubmitDialogVisible(true); }; // 确认提交老板审核 const confirmSubmit = async () => { // 关闭对话框 setSubmitDialogVisible(false); // 表单校验 const errorMsg = validateForm(); if (errorMsg) { Toast.show("toast", { icon: "fail", title: "校验失败", content: errorMsg, }); return; } // 这里应该调用提交审核API console.log("提交老板审核:", purchaseOrderVO); const { data } = await business.purchaseOrder.approvePurchaseOrder({ ...purchaseOrderVO!, draft: false, }); if (data.success) { Toast.show("toast", { icon: "success", title: "提示", content: "暂存成功", }); // 返回采购单页面 Taro.switchTab({ url: "/pages/main/purchase/index" }); } }; // 表单校验 const validateForm = () => { // 校验销售方 if (!purchaseOrderVO?.orderCompany?.companyId) { return "请选择销售方"; } // 校验经销商和仓库 if ( !purchaseOrderVO?.orderDealer?.dealerId || !purchaseOrderVO?.orderDealer?.warehouseId ) { return "请选择经销商和仓库"; } // 校验本车次号 if (!purchaseOrderVO?.orderVehicle?.vehicleNo) { return "请输入本车次号"; } // 校验运费类型 if (!purchaseOrderVO?.orderVehicle?.priceType) { return "请选择运费类型"; } // 校验市场报价的报价方式 if (!purchaseOrderVO?.pricingMethod) { return "请选择市场报价的报价方式"; } // 校验市场报价的销售单价 purchaseOrderVO.orderSupplierList.forEach( (supplier: BusinessAPI.OrderSupplier) => { if (!supplier.salePrice || supplier.salePrice <= 0) { return "请填写市场报价的销售单价"; } }, ); return null; }; const init = async (orderId: BusinessAPI.PurchaseOrderVO["orderId"]) => { const { data } = await business.purchaseOrder.showPurchaseOrder({ purchaseOrderShowQry: { orderId, }, }); if (data.success) { setPurchaseOrderVO(data.data); } }; useEffect(() => { if (orderId && !isInitialized) { init(orderId).then(() => { setIsInitialized(true); }); } }, []); useDidShow(() => { if (orderId && isInitialized) { init(orderId).then(); } }); if (!purchaseOrderVO) { return; } return ( {/* 顶部导航 */} {/* 展示产地负责人*/} 产地负责人 setOriginPrincipal(value)} onBlur={() => { // 更新采购订单中的产地负责人 setPurchaseOrderVO((prev) => ({ ...prev!, originPrincipal: originPrincipal, })); }} className="w-full bg-transparent" /> {`第${purchaseOrderVO?.orderVehicle?.vehicleNo || "-"}车 - ${purchaseOrderVO?.orderVehicle.origin} 至 ${purchaseOrderVO?.orderVehicle.destination}`} {purchaseOrderVO?.orderDealer && ( <> 经销商名称: {purchaseOrderVO?.orderDealer?.shortName} 经销商公司: {purchaseOrderVO?.orderDealer?.companyName} )} {/* 状态栏 */} { purchaseOrder.stateList.find( (item) => item.value === purchaseOrderVO?.state, )?.title } 创建时间:{" "} {dayjs(purchaseOrderVO?.createdAt).format("YYYY-MM-DD HH:mm")} 车牌号: {purchaseOrderVO?.orderVehicle.plate} {/* 关键指标 */} {purchaseOrderVO?.orderSupplierList.length} 关联瓜农 {formatCurrency(getTotalGrossWeight(purchaseOrderVO))} 斤 总毛重 {formatCurrency(getTotalNetWeight(purchaseOrderVO))} 斤 总净重 {formatCurrency(getMelonCost1(purchaseOrderVO))} 西瓜采购总金额 {/* 循环渲染各部分内容 */} {Object.keys(sectionList).map((sectionKey) => { const SectionComponent = sectionList[sectionKey]; const config = sectionConfig[sectionKey]; const isCollapsed = collapsedSections[sectionKey]; return ( toggleSection(sectionKey)} > {config.title} {isCollapsed ? "▲" : "▼"} {!isCollapsed && ( )} ); })} {/* 按钮操作 */} {role === "reviewer" && purchaseOrderVO.state === "WAITING_AUDIT" && ( <> { // 返回首页 Taro.switchTab({ url: "/pages/main/purchase/index" }); }} /> )} {role === "boss" && purchaseOrderVO.state === "WAITING_BOSS_APPROVE" && ( <> { // 返回首页 Taro.switchTab({ url: "/pages/main/purchase/index" }); }} /> { // 返回首页 Taro.switchTab({ url: "/pages/main/purchase/index" }); }} /> )} {/* 暂存确认对话框 */} setSaveDialogVisible(false)} onConfirm={confirmSave} /> {/* 提交审核确认对话框 */} setSubmitDialogVisible(false)} onConfirm={confirmSubmit} /> ); });