import React, { useState, useRef } from 'react'; import { Upload, Download, FileSpreadsheet, RefreshCw, Package } from 'lucide-react'; import { Product, Language, languages } from './types'; import { readExcelFile, downloadExcelFile, createSampleData } from './utils/excelUtils'; import { ExcelTable } from './components/ExcelTable'; import { LanguageToggle } from './components/LanguageToggle'; import { ProductCatalog } from './components/ProductCatalog'; import { Navigation } from './components/Navigation'; function App() { const [products, setProducts] = useState([]); const [language, setLanguage] = useState(languages[0]); const [currentPage, setCurrentPage] = useState<'inventory' | 'catalog'>('inventory'); const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(''); const fileInputRef = useRef(null); const isArabic = language.code === 'ar'; const labels = { ar: { title: 'إدارة المخزون', subtitle: 'نظام إدارة المخزون باستخدام Excel', uploadFile: 'تحميل ملف Excel', downloadFile: 'تنزيل Excel محدث', loadSample: 'تحميل بيانات تجريبية', uploadHint: 'اسحب ملف Excel هنا أو انقر للتحديد', supportedFormats: 'يدعم ملفات .xlsx', loading: 'جارٍ المعالجة...', error: 'خطأ', success: 'تم بنجاح' }, en: { title: 'Inventory Manager', subtitle: 'Excel-based Inventory Management System', uploadFile: 'Upload Excel File', downloadFile: 'Download Updated Excel', loadSample: 'Load Sample Data', uploadHint: 'Drag Excel file here or click to select', supportedFormats: 'Supports .xlsx files', loading: 'Processing...', error: 'Error', success: 'Success' } }; const t = labels[language.code]; const handleFileUpload = async (file: File) => { if (!file.name.endsWith('.xlsx')) { setError(isArabic ? 'يرجى اختيار ملف Excel صالح (.xlsx)' : 'Please select a valid Excel file (.xlsx)'); return; } setIsLoading(true); setError(''); try { const parsedProducts = await readExcelFile(file); setProducts(parsedProducts); } catch (err) { setError(isArabic ? 'فشل في قراءة الملف' : 'Failed to read file'); console.error('File reading error:', err); } finally { setIsLoading(false); } }; const handleDrop = (e: React.DragEvent) => { e.preventDefault(); const files = Array.from(e.dataTransfer.files); if (files.length > 0) { handleFileUpload(files[0]); } }; const handleProductUpdate = (updatedProduct: Product) => { setProducts(products.map(p => p.id === updatedProduct.id ? updatedProduct : p)); }; const handleProductDelete = (productId: string) => { setProducts(products.filter(p => p.id !== productId)); }; const handleProductAdd = (newProduct: Product) => { setProducts([...products, newProduct]); }; const handleQuantityChange = (productId: string, change: number) => { setProducts(products.map(product => product.id === productId ? { ...product, quantity: Math.max(0, product.quantity + change) } : product )); }; const handleDownloadExcel = () => { if (products.length === 0) { setError(isArabic ? 'لا توجد بيانات للتنزيل' : 'No data to download'); return; } downloadExcelFile(products, 'inventory'); }; const loadSampleData = () => { setProducts(createSampleData()); }; return (
{/* Header */}
{currentPage === 'inventory' ? ( ) : ( )}

{currentPage === 'inventory' ? t.title : (isArabic ? 'كتالوج المنتجات' : 'Product Catalog')}

{currentPage === 'inventory' ? t.subtitle : (isArabic ? 'تصفح مجموعتنا من المعدات الكهربائية' : 'Browse our electrical equipment collection')}

{/* Navigation */} {currentPage === 'catalog' ? ( ) : (
{/* File Upload Area */}
{/* Upload Section */}
e.preventDefault()} className="md:col-span-2 border-2 border-dashed border-gray-300 rounded-xl p-8 text-center hover:border-blue-400 transition-colors cursor-pointer bg-white" onClick={() => fileInputRef.current?.click()} >

{t.uploadHint}

{t.supportedFormats}

{/* Action Buttons */}
{ const file = e.target.files?.[0]; if (file) handleFileUpload(file); }} className="hidden" />
{/* Loading State */} {isLoading && (
{t.loading}
)} {/* Error State */} {error && (
!
{error}
)} {/* Products Table */}
)}
); } export default App;