import { useState, useEffect, useRef } from 'react'; import { InputField } from '../../../../../Components/Forms/InputField.jsx'; import { Form } from 'antd'; import Buttons from '../../../../../Components/Forms/Buttons.jsx'; import { ArrowRightOutlined } from '@ant-design/icons'; const WeightCalculatePrice = ({ itemData, CartOrderDetails, onSubmit }) => { const [enteredPrice, setEnteredPrice] = useState(''); const [calculatedQty, setCalculatedQty] = useState(0); const priceInputRef = useRef(null); const [form] = Form.useForm(); const basePrice = parseFloat(itemData?.OrderRate || itemData?.SellingPrice || 0); useEffect(() => { setTimeout(() => { const inputElement = priceInputRef.current?.querySelector('input'); if (inputElement) { inputElement.focus(); } }, 100); }, []); // Calculate quantity based on entered price const handlePriceChange = (e) => { const value = e.target.value; setEnteredPrice(value); if (value && basePrice > 0) { const price = parseFloat(value); const qty = price / basePrice; setCalculatedQty(qty); } else { setCalculatedQty(0); } }; const handleSubmit = (e) => { if (e && e.preventDefault) { e.preventDefault(); } if (enteredPrice && calculatedQty > 0) { console.log('Submitting weight calculation:', { price: parseFloat(enteredPrice), quantity: calculatedQty, }); // Pass the calculated values back to parent component if (onSubmit) { onSubmit({ price: parseFloat(enteredPrice), quantity: calculatedQty, }); } } }; return (
e.preventDefault()} >

Product: {itemData?.ProdName}

Variant: {itemData?.ProdVariantName}

Base Price: ₹{basePrice.toFixed(2)} per {itemData?.UomName || 'unit'}

{ const cleanedValue = e.target.value.replace(/[^0-9.]/g, ''); const parts = cleanedValue.split('.'); e.target.value = parts.length > 2 ? `${parts[0]}.${parts.slice(1).join('')}` : cleanedValue; }} />
{calculatedQty > 0 ? calculatedQty.toFixed(3) : '0.000'} {itemData?.UomName || 'units'}
{calculatedQty > 0 && (
₹{parseFloat(enteredPrice || 0).toFixed(2)}
)}
} />
); }; export default WeightCalculatePrice;