import React, { useState, useEffect, useRef } from "react";
import { Link, useParams } from "react-router-dom";
import Select from "react-select";
import { all_routes } from "../../Router/all_routes";
import { DatePicker } from "antd";
import Addunits from "../../core/modals/inventory/addunits";
import AddCategory from "../../core/modals/inventory/addcategory";
import AddBrand from "../../core/modals/addbrand";
import {
ArrowLeft,
Calendar,
ChevronDown,
ChevronUp,
Info,
LifeBuoy,
List,
PlusCircle,
Trash2,
X,
} from "feather-icons-react/build/IconComponents";
import { useDispatch, useSelector } from "react-redux";
import { setToogleHeader } from "../../core/redux/action";
import { fetchProduct } from "../../core/redux/actions/productActions";
import { OverlayTrigger, Tooltip } from "react-bootstrap";
import ImageWithBasePath from "../../core/img/imagewithbasebath";
const EditProduct = () => {
const route = all_routes;
const dispatch = useDispatch();
const { id } = useParams(); // Get product ID from URL
const data = useSelector((state) => state.toggle_header);
// Get product data from Redux store
const { currentProduct, productLoading, productError } = useSelector((state) => state.products || {});
// Track if we've already fetched this product
const fetchedProductId = useRef(null);
// Form state for editing
const [formData, setFormData] = useState({
name: '',
slug: '',
sku: '',
description: '',
price: '',
category: null, // Change to null for Select component
brand: null, // Change to null for Select component
quantity: '',
unit: null, // Change to null for Select component
status: '',
images: []
});
// Load product data if ID is provided and product not already loaded
useEffect(() => {
if (id && fetchedProductId.current !== id) {
console.log('Fetching product details for ID:', id);
fetchedProductId.current = id;
dispatch(fetchProduct(id));
}
}, [id, dispatch]);
// Helper function to find option by value or label
const findSelectOption = (options, value) => {
if (!value) return null;
return options.find(option =>
option.value === value ||
option.label === value ||
option.value.toLowerCase() === value.toLowerCase() ||
option.label.toLowerCase() === value.toLowerCase()
) || null;
};
// Update form data when currentProduct changes
useEffect(() => {
if (currentProduct) {
console.log('Product data loaded:', currentProduct);
// Find matching options for select fields
const categoryOption = findSelectOption(category, currentProduct.category || currentProduct.categoryName);
const brandOption = findSelectOption(brand, currentProduct.brand || currentProduct.brandName);
const unitOption = findSelectOption(unit, currentProduct.unit);
setFormData({
name: currentProduct.name || currentProduct.productName || '',
slug: currentProduct.slug || '',
sku: currentProduct.sku || currentProduct.code || '',
description: currentProduct.description || '',
price: currentProduct.price || currentProduct.salePrice || '',
category: categoryOption,
brand: brandOption,
quantity: currentProduct.quantity || currentProduct.stock || '',
unit: unitOption,
status: currentProduct.status || 'active',
images: currentProduct.images || []
});
console.log('Form data updated:', {
category: categoryOption,
brand: brandOption,
unit: unitOption
});
}
}, [currentProduct]);
const [selectedDate, setSelectedDate] = useState(new Date());
const handleDateChange = (date) => {
setSelectedDate(date);
};
const [selectedDate1, setSelectedDate1] = useState(new Date());
const handleDateChange1 = (date) => {
setSelectedDate1(date);
};
const renderCollapseTooltip = (props) => (
Loading product details...