/* eslint-disable @next/next/no-img-element */ /* eslint-disable jsx-a11y/role-supports-aria-props */ /* eslint-disable @typescript-eslint/no-explicit-any */ import Link from "next/link"; import React from "react"; import { useRouter, useSearchParams } from "next/navigation"; import { amountFormatWithCurrency, cryptoHandler, formatDate, getFormattedAddress, getOrderStatusInfo, } from "../../../lib/Helper"; import { getBulkOrderDetails } from "../../../Api/OrderApi"; import HourGlassLoader from "../../Loader/Loader"; interface OrderPackage { id: string; qrCode: string; generatedOrderPackageId: string; weight: number; packageDescription: string; customerInputPackageValue: string; status: { id: number; name: string; }; } interface OrderData { id: string; generatedOrderId: string; destinationCustomerName: string; destinationMobileNumber: string; destinationAlternateNumber: string; destination_block_details?: { name: string }; destination_road_details?: { name: string }; destination_building_details?: { name: string }; destinationFlatOrOfficeNumber: string; orderType: { name: string }; status: { id: number }; barCode: string; calculatedTotalShippingCharge: number; order_package_list: OrderPackage[]; totalWeight: number; pickupDate: string; deliveryInstructions: string; codAmount: number; } const OrderViewIndex = () => { // const router = useRouter(); const searchParams = useSearchParams(); const router = useRouter() const [orderData, setOrderData] = React.useState({}); const [isLoading, setIsLoading] = React.useState(true); const [error, setError] = React.useState(null); const [packageShow, setPackageShow] = React.useState(true); // const [isDownloading, setIsDownloading] = React.useState(false); const togglePackageShow = () => setPackageShow(!packageShow); React.useEffect(() => { const fetchOrderDetails = async () => { try { const encryptedId = searchParams?.get("id"); if (!encryptedId) { setError("No order ID found in URL"); setIsLoading(false); return; } const decryptedId = cryptoHandler(encryptedId, "decrypt"); if (!decryptedId) { setError("Invalid order ID"); setIsLoading(false); return; } const orderDetails = await getBulkOrderDetails(decryptedId); if (orderDetails && typeof orderDetails === "object") { setOrderData(orderDetails as unknown as OrderData); } else { setError("Invalid order data received"); } } catch (error) { console.error("Error fetching order details:", error); setError("Failed to load order details. Please try again."); } finally { setIsLoading(false); } }; fetchOrderDetails(); }, [searchParams]); const storedUser = localStorage.getItem("ALL_DATA"); const allData = storedUser ? JSON.parse(storedUser)?.data : null; // const handleDeletePackage = async (packageId: string) => { // if (!packageId) { // toast.error("Invalid package ID"); // return; // } // Swal.fire({ // title: "Are you sure?", // text: "Do you really want to delete this package?", // icon: "warning", // showCancelButton: true, // confirmButtonText: "Yes, delete it!", // cancelButtonText: "Cancel", // reverseButtons: true, // customClass: { // confirmButton: "delybell-primary px-4", // cancelButton: "delybell-dark", // }, // }).then(async (response) => { // if (response?.isConfirmed) { // try { // await deletePackage(packageId); // toast.success("Package deleted successfully"); // if (orderData?.id) { // const updatedOrder = await getOrderHistroy(orderData.id); // if (updatedOrder && typeof updatedOrder === "object") { // setOrderData(updatedOrder as unknown as OrderData); // } // } // } catch (error: any) { // console.error("Error deleting package:", error); // toast.error( // error?.response?.data?.message || // "Failed to delete package. Please try again." // ); // } // } // }); // }; // const handleDownloadPdf = async () => { // try { // if (!orderData?.invoice_details?.id) { // toast.error("Order ID is not available."); // return; // } // setIsDownloading(true); // const response = await downloadInvoice(orderData?.invoice_details?.id); // const url = window.URL.createObjectURL(new Blob([response])); // const link = document.createElement("a"); // link.href = url; // link.setAttribute( // "download", // `invoice-${orderData.generatedOrderId}.pdf` // ); // document.body.appendChild(link); // link.click(); // link.parentNode?.removeChild(link); // toast.success("Invoice downloaded successfully"); // } catch (error: any) { // console.error("Error downloading invoice:", error); // toast.error( // error?.response?.data?.message || // "Failed to download invoice. Please try again." // ); // } finally { // setIsDownloading(false); // } // }; if (isLoading) { return ; } if (error) { return (
{error}
Back to Orders
); } if (!orderData) { return (
No order data found
Back to Orders
); } const handleEdit = (orderDetail: any) => { console.log("handleEdit called with order:", orderDetail); const url = `/create-order?mode=edit_order&orderId=${ orderDetail?.id }&customerName=${encodeURIComponent( orderDetail?.destinationCustomerName || "" )}`; console.log("Navigating to:", url); window.location.href = url; }; // const handleEditPackages = (packageId: any, orderDetail: any) => { // console.log("handleEdit called with order:", orderDetail); // const url = `/create-order?mode=edit_order&orderId=${ // orderDetail?.id // }&customerName=${encodeURIComponent( // orderDetail?.destinationCustomerName || "" // )}`; // console.log("Navigating to:", url); // window.location.href = url; // }; return ( <>
{orderData?.generatedOrderId}