import React, { useState } from 'react'; import { CreditCard as Edit, Trash2, Minus, Plus, ShoppingCart } from 'lucide-react'; import { Product, Language } from '../types'; import { ProductForm } from './ProductForm'; interface ExcelTableProps { products: Product[]; language: Language; onProductUpdate: (product: Product) => void; onProductDelete: (productId: string) => void; onProductAdd: (product: Product) => void; onQuantityChange: (productId: string, change: number) => void; } export const ExcelTable: React.FC = ({ products, language, onProductUpdate, onProductDelete, onProductAdd, onQuantityChange }) => { const [editingProduct, setEditingProduct] = useState(); const [showForm, setShowForm] = useState(false); const [sellQuantities, setSellQuantities] = useState<{ [key: string]: number }>({}); const isArabic = language.code === 'ar'; const labels = { ar: { addProduct: 'إضافة منتج جديد', productName: 'اسم المنتج', quantity: 'الكمية', price: 'السعر', category: 'الفئة', actions: 'الإجراءات', edit: 'تعديل', delete: 'حذف', sell: 'بيع', sellLabel: 'كمية البيع', noProducts: 'لا توجد منتجات', total: 'المجموع', items: 'عنصر', value: 'القيمة الإجمالية' }, en: { addProduct: 'Add New Product', productName: 'Product Name', quantity: 'Quantity', price: 'Price', category: 'Category', actions: 'Actions', edit: 'Edit', delete: 'Delete', sell: 'Sell', sellLabel: 'Sell Qty', noProducts: 'No products found', total: 'Total', items: 'items', value: 'Total Value' } }; const t = labels[language.code]; const totalItems = products.reduce((sum, product) => sum + product.quantity, 0); const totalValue = products.reduce((sum, product) => sum + (product.quantity * product.price), 0); const handleSellProduct = (productId: string) => { const sellQty = sellQuantities[productId] || 1; const product = products.find(p => p.id === productId); if (product && sellQty <= (product.currentStock || product.quantity)) { onQuantityChange(productId, -sellQty); // Update sold quantity const updatedProduct = { ...product, totalSold: (product.totalSold || 0) + sellQty, currentStock: (product.currentStock || product.quantity) - sellQty }; onProductUpdate(updatedProduct); setSellQuantities({ ...sellQuantities, [productId]: 1 }); } }; const formatPrice = (price: number) => { return new Intl.NumberFormat(isArabic ? 'ar-SA' : 'en-US', { style: 'currency', currency: 'SAR' }).format(price); }; return (
{/* Stats Cards */}

{t.total} {t.items}

{totalItems.toLocaleString()}

{t.value}

{formatPrice(totalValue)}

💰

Products

{products.length}

📦
{/* Add Product Button */}

{isArabic ? 'إدارة المخزون' : 'Inventory Management'}

{/* Table */}
{products.length === 0 ? (

{t.noProducts}

{isArabic ? 'قم بإضافة منتج جديد أو تحميل ملف Excel' : 'Add a new product or upload an Excel file'}

) : (
{products.map((product) => ( ))}
{t.supplier} {t.productName} {t.received} {t.sold} {t.remaining} {t.price} {t.sell} {t.actions}
{isArabic ? product.supplierAr : product.supplier}
{product.importDate}
{isArabic ? product.nameAr : product.nameEn}
{product.secoCode && `Code: ${product.secoCode}`} {product.categoryAr && (
{isArabic ? product.categoryAr : product.categoryEn}
)}
{product.totalReceived || 0} {product.totalSold || 0} 10 ? 'bg-green-100 text-green-800' : (product.currentStock || product.quantity) > 0 ? 'bg-yellow-100 text-yellow-800' : 'bg-red-100 text-red-800' }`}> {product.currentStock || product.quantity} {formatPrice(product.price)}
setSellQuantities({ ...sellQuantities, [product.id]: Math.min(Number(e.target.value), product.currentStock || product.quantity) })} className="w-16 px-2 py-1 text-sm border border-gray-300 rounded focus:ring-2 focus:ring-blue-500 focus:border-transparent" />
)}
{/* Product Form Modal */} { setShowForm(false); setEditingProduct(undefined); }} onSave={(product) => { if (editingProduct) { onProductUpdate(product); } else { onProductAdd(product); } setEditingProduct(undefined); }} language={language} />
); };