/* eslint-disable @typescript-eslint/no-explicit-any */ import React, { useState, useRef } from "react"; import { useQuery } from "@tanstack/react-query"; import { ImagePath } from "@/Constant"; import html2canvas from "html2canvas"; import jsPDF from "jspdf"; import { useSearchParams } from "next/navigation"; import { getSingleSettledView, uploadManifest, manifestColumnMapping } from "../../../../Api/ShipmentApi"; // Assume these components exist or adjust import paths accordingly import Header from "./ViewContainers/Header"; import TableManifest from "./ViewContainers/TableManifest"; import Footer from "./ViewContainers/Footer"; import Modal from "../../../Modal/Modal"; import ManifestColumnsForm from "./ManifestColumnsForm"; import { ShipmentStatus } from "@/Constant/enums"; const ShipmentPreview = ({ editShipment, setShipmentDetails }: any) => { const fileInputRef = useRef(null); const [isUploading, setIsUploading] = useState(false); const [manifestResponse, setManifestResponse] = useState(null); const [uploadError, setUploadError] = useState(null); const [isMappingModalOpen, setIsMappingModalOpen] = useState(false); const [mappingSelections, setMappingSelections] = useState>({}); // eslint-disable-next-line @typescript-eslint/no-unused-vars const [mappingFilter, setMappingFilter] = useState(""); const [isSavingMapping, setIsSavingMapping] = useState(false); const [PriceList] = useState({ customer_details: { companyName: "Acme Corp", address: "123 Main St, Springfield", phone: "1234567890", email: "info@acmecorp.com", }, settlementId: "SETTLE123456", shipment_summary: { totalAmount: 5000, currency: "USD", }, shipments: [ { shipmentId: "SHIP123", containerNumber: "CONT001", origin: "New York", destination: "Dubai", items: [ { name: "Electronics", quantity: 10, weight: 200, price: 1500, }, ], }, ], }); const searchParams = useSearchParams(); const id = searchParams.get("id"); const componentRef = useRef(null); const handleUploadManifest = (e: React.MouseEvent) => { e.preventDefault(); // Prevent form submission e.stopPropagation(); // Stop event from bubbling up to modal setUploadError(null); if (fileInputRef.current) { fileInputRef.current.value = ""; fileInputRef.current.click(); } }; const handleManifestFileChange = async (e: React.ChangeEvent) => { try { const file = e.target.files?.[0]; if (!file) return; const shipmentId = editShipment?.id ?? (id ? Number(id) : undefined); if (!shipmentId) { setUploadError("Shipment ID is required"); return; } setIsUploading(true); const result = await uploadManifest({ manifest_file: file, shipment_id: shipmentId, metadata: { uploaded_at: new Date().toISOString(), file_name: file.name, file_size: file.size, }, }); if (result.success) { setManifestResponse(result.data); const initialSelections: Record = {}; (result as any)?.data?.originalColumns?.forEach((col: any) => { initialSelections[col.variable] = ""; }); setMappingSelections(initialSelections); setIsMappingModalOpen(true); } else { setUploadError(result.error || "Failed to upload manifest"); console.error("Manifest upload failed:", result.error, result.data); } } catch (error: any) { setUploadError(error?.message || "An unexpected error occurred during upload"); } finally { setIsUploading(false); } }; const handlePrints = () => { const content = document.getElementById("print-area"); const printWindow: any = window.open("", "_blank", "width=700,height=700"); printWindow.document.write(` Print `); printWindow.document.close(); printWindow.onload = () => { const images = printWindow.document.images; let loadedCount = 0; const checkAllImagesLoaded = () => { loadedCount++; if (loadedCount === images.length) { printWindow.focus(); printWindow.print(); printWindow.close(); } }; if (images.length === 0) { printWindow.focus(); printWindow.print(); printWindow.close(); } else { for (const img of images) { if (img.complete) { checkAllImagesLoaded(); } else { img.onload = checkAllImagesLoaded; img.onerror = checkAllImagesLoaded; } } } }; }; const { isLoading: isPriceListLoading } = useQuery({ queryKey: ["getSingleSettledView", id], queryFn: () => (id ? getSingleSettledView(id) : Promise.resolve(null)), enabled: !!id, }); const downloadPDF = () => { const input: any = document.getElementById("pdf-content"); html2canvas(input).then((canvas) => { const imgData = canvas.toDataURL("image/png"); const pdf = new jsPDF(); const imgWidth = 200; const imgHeight = (canvas.height * imgWidth) / canvas.width; const margin = 10; pdf.addImage( imgData, "PNG", margin, margin, imgWidth - 2 * margin, imgHeight - 2 * margin ); const fileName = `Settlement_${PriceList?.customer_details?.companyName || "" }_${PriceList?.settlementId || ""}.pdf`; pdf.save(fileName); }); }; return ( <> {editShipment?.status?.id === ShipmentStatus.Created ? (
{/* */}
) : ''} {isMappingModalOpen && ( { setIsMappingModalOpen(false) }} header={false} title="" // fullscreen={"sm"} centered={true} classname="custom-center-modal" position={"top"} > )}
{isPriceListLoading && (
{isPriceListLoading ? "Loading..." : ``}
)}
{false && (
)} ); }; export default ShipmentPreview;