import { useState, type ReactNode } from "react";
import { useNavigate, useParams } from "react-router";
import {
Alert,
Box,
Button,
Chip,
CircularProgress,
Dialog,
DialogActions,
DialogContent,
DialogTitle,
Paper,
Stack,
Table,
TableBody,
TableCell,
TableHead,
TableRow,
TextField,
Typography,
} from "@mui/material";
import { useApproveUplift, useDenyUplift } from "@/domain/uplifts/use-cases/use-uplift-actions";
import { useVendorPoDetail } from "@/domain/vendor-pos/use-cases/use-vendor-po-detail";
import type { UpliftRequest } from "@/domain/uplifts/types/uplift";
import { formatDateTime, toDateInputValue } from "@/lib/time-utils";
function formatMoney(value: number | null | undefined): string {
if (value == null) return "—";
return `$${Number(value).toFixed(2)}`;
}
function InfoRow({ label, children }: { label: string; children: ReactNode }) {
return (
{label}
{children == null || children === "" ? "—" : children}
);
}
export default function VendorPoViewPage() {
const { id } = useParams();
const navigate = useNavigate();
const { data: dispatch, isLoading, error, refetch } = useVendorPoDetail(id);
const approveUplift = useApproveUplift(id);
const denyUplift = useDenyUplift(id);
const [upliftAction, setUpliftAction] = useState<{
type: "approve" | "deny";
uplift: UpliftRequest;
} | null>(null);
const [note, setNote] = useState("");
const handleUpliftConfirm = () => {
if (!upliftAction) return;
if (upliftAction.type === "approve") {
approveUplift.mutate(
{ id: upliftAction.uplift.id, note: note || undefined },
{
onSuccess: () => {
setUpliftAction(null);
setNote("");
void refetch();
},
},
);
} else {
if (!note.trim()) return;
denyUplift.mutate(
{ id: upliftAction.uplift.id, note: note.trim() },
{
onSuccess: () => {
setUpliftAction(null);
setNote("");
void refetch();
},
},
);
}
};
if (isLoading) {
return (
);
}
if (error || !dispatch) {
return (
{error instanceof Error ? error.message : "Vendor PO not found"}
);
}
const vendor = dispatch.vendor;
return (
{dispatch.poNumber || "(no PO #)"}
Dispatch {dispatch.dispatchNumber || "—"}
{vendor?.companyName && (
· {vendor.companyName}
)}
{dispatch.nteAmount != null && (
· NTE {formatMoney(dispatch.nteAmount)}
)}
Vendor
{vendor ? (
{vendor.companyName}
{vendor.contactName}
{vendor.email}
{vendor.phone}
{[vendor.address, vendor.city, vendor.state, vendor.zip].filter(Boolean).join(", ")}
{vendor.tradeSpecialties}
) : (
No vendor attached.
)}
PO Details
{dispatch.poNumber}
{dispatch.dispatchNumber}
{dispatch.status}
{formatMoney(dispatch.nteAmount)}
{formatDateTime(dispatch.dispatchedAt)}
{formatDateTime(dispatch.acknowledgedAt)}
{toDateInputValue(dispatch.scheduledDate) || "—"}
{toDateInputValue(dispatch.completedDate) || "—"}
{formatDateTime(dispatch.verifiedAt)}
{dispatch.emailSent ? "Yes" : "No"}
{dispatch.description && (
Description
{dispatch.description}
)}
{dispatch.primaryLocation && (
Site
{dispatch.primaryLocation.name}
{[
dispatch.primaryLocation.address,
dispatch.primaryLocation.city,
dispatch.primaryLocation.state,
dispatch.primaryLocation.zipCode,
]
.filter(Boolean)
.join(", ")}
)}
Linked Work Orders ({dispatch.workOrders.length})
{dispatch.workOrders.length === 0 ? (
No work orders linked.
) : (
WO #
Title
Trade
Status
Location
{dispatch.workOrders.map((wo) => (
{wo.title}
{wo.trade}
{wo.status}
{wo.locationName}
))}
)}
NTE Uplift History ({dispatch.upliftRequests.length})
{dispatch.upliftRequests.length === 0 ? (
No uplift requests on this PO.
) : (
{dispatch.upliftRequests.map((u) => (
{formatMoney(u.currentNTE)} → {formatMoney(u.requestedNTE)}
(Δ {formatMoney(u.delta)}, Tier {u.requiredTier})
{u.vendorReason && (
Reason: {u.vendorReason}
)}
{u.status === "Pending" && (
)}
))}
)}
);
}