"use client"; import React, { useContext, useState } from "react"; import { Plus, Minus, FileText } from "lucide-react"; // Added FileText icon import ProductContext from "@/app/contexts/productContext"; import { backendUrl } from "@/utils/axios"; import { useCurrency } from "@/app/contexts/currencyContext"; import Link from "next/link"; import Image from "next/image"; function ProductDetails({ productId }) { const [quantity, setQuantity] = useState(1); const [selectedSize, setSelectedSize] = useState(null); const [selectedDesign, setSelectedDesign] = useState(null); const [termsAccepted, setTermsAccepted] = useState(false); const [showCertificate, setShowCertificate] = useState(false); const { products, cartFn } = useContext(ProductContext); const { formatPrice, isLoading } = useCurrency(); const product = products?.find((pr) => pr.id == productId); React.useEffect(() => { if (product && product.variants?.length > 0) { setSelectedSize(product.variants[0]?.size?.size_name || ""); setSelectedDesign(product.design_associations?.[0]?.design?.id || ""); } }, [product]); if (!product) return null; const hasVariants = product.variants?.length > 0; const hasDesigns = product.design_associations?.length > 0; const hasCertificate = product.certificate && product.certificate !== "NA"; const selectedVariant = hasVariants ? product.variants.find((v) => v.size.size_name === selectedSize) : null; const currentPrice = selectedVariant?.price || product.price || 0; const designPrice = hasDesigns ? product.design_associations.find((d) => d.design.id === selectedDesign) ?.design.design_price || 0 : 0; const totalPrice = currentPrice + designPrice; const handleViewCertificate = () => { setShowCertificate(true); // If it's a PDF, open in a new tab if (product.certificate.toLowerCase().endsWith('.pdf')) { window.open(`${backendUrl}${product.certificate}`, '_blank'); setShowCertificate(false); } }; return (

{product.product_name}

{hasVariants ? (
{isLoading ? ( Loading... ) : ( {formatPrice(totalPrice)} )}
) : (
Price on request
)}
{hasVariants && (

Select Size

{product.variants.map((variant) => ( ))}
)} {hasDesigns && (

Select Your Design

{product.design_associations.map((design) => ( ))}
)}
{product?.product_category?.category_name && (

Category: {product.product_category.category_name}

)} {product?.product_subcategory?.subcategory_name && (

Subcategory: {product.product_subcategory.subcategory_name}

)}
{hasCertificate && (
)} {hasVariants && ( <>
{quantity}
Stock:{" "} {selectedVariant?.stock > 0 ? "available" : "Not Available"}
)}
setTermsAccepted(e.target.checked)} /> I agree with the terms of services and return And Cancellation policy
{hasVariants && selectedVariant?.stock > 0 ? ( ) : ( )}

Product Description

Beej Mantra

{showCertificate && !product.certificate.toLowerCase().endsWith('.pdf') && (

Product Certificate

Product Certificate
)}
); } export default ProductDetails;