import React, { useState } from 'react'; import { useTheme } from '@mui/material/styles'; import { useMediaQuery, Box, Typography, Paper, Table, TableBody, TableCell, TableHead, TableRow, Chip, Pagination, Button, Avatar, TableContainer, TextField, IconButton, } from '@mui/material'; import AssignmentIcon from '@mui/icons-material/Assignment'; import DriveFileRenameOutlineSharpIcon from '@mui/icons-material/DriveFileRenameOutlineSharp'; import ArrowDropDownIcon from '@mui/icons-material/ArrowDropDown'; import CloseIcon from '@mui/icons-material/Close'; import { green } from '@mui/material/colors'; import jsPDF from 'jspdf'; import autoTable from 'jspdf-autotable'; import * as XLSX from 'xlsx'; import { saveAs } from 'file-saver'; import TuneIcon from '@mui/icons-material/Tune'; const TableView = ({ data = [], onAddNewProduct }) => { const theme = useTheme(); const isMobile = useMediaQuery(theme.breakpoints.down('sm')); const itemsPerPage = 5; const [currentPage, setCurrentPage] = useState(1); const [showSearch, setShowSearch] = useState(false); const [searchTerm, setSearchTerm] = useState(''); // فلترة البيانات بناءً على نص البحث (غير حساس لحالة الأحرف) const filteredData = data.filter((item) => item.product.toLowerCase().includes(searchTerm.toLowerCase()) ); const pageCount = Math.ceil(filteredData.length / itemsPerPage); const paginatedData = filteredData.slice( (currentPage - 1) * itemsPerPage, currentPage * itemsPerPage ); const handleSearchChange = (e) => { setSearchTerm(e.target.value); setCurrentPage(1); // العودة للصفحة الأولى عند تغيير البحث }; const handleExportPDF = () => { const doc = new jsPDF(); doc.setFontSize(14); doc.text('Top Selling Products', 14, 20); const tableColumn = ['No.', 'Product', 'Sales', 'Amount', 'Price', 'Expiration', 'Status']; const tableRows = data.map((row, i) => ([ i + 1, row.product, row.sales, `$${row.amount}`, `$${row.price}`, row.expiration, row.status ])); autoTable(doc, { startY: 30, head: [tableColumn], body: tableRows, styles: { fontSize: 10 }, headStyles: { fillColor: [240, 240, 240] } }); doc.save('top_selling_products.pdf'); }; const handleExportSpreadsheet = () => { const wsData = [ ['No.', 'Product', 'Sales', 'Amount', 'Price', 'Expiration', 'Status'], ...data.map((row, i) => [ i + 1, row.product, row.sales, `$${row.amount}`, `$${row.price}`, row.expiration, row.status ]) ]; const worksheet = XLSX.utils.aoa_to_sheet(wsData); const workbook = XLSX.utils.book_new(); XLSX.utils.book_append_sheet(workbook, worksheet, "Top Selling"); const wbout = XLSX.write(workbook, { bookType: 'xlsx', type: 'array' }); const blob = new Blob([wbout], { type: 'application/octet-stream' }); saveAs(blob, 'top_selling_products.xlsx'); }; return ( {/* Header */} Table View {/* زر إضافة منتج جديد */} {/* زر تصدير جدول (Spreadsheet) */} {/* زر PDF */} {/* زر الفلاتر */} {/* جدول البيانات */} No. setShowSearch(prev => !prev)} > Product Name {showSearch && ( setSearchTerm('')} aria-label="Clear search" > ), }} /> )} {!isMobile && Barcode} Branch {!isMobile && Expiration} {!isMobile && Stock Quantity } Consumption Status Action {paginatedData.length > 0 ? ( paginatedData.map((row, i) => ( {(currentPage - 1) * itemsPerPage + i + 1} {row.product} {!isMobile && ( {row.sales} )} ${row.amount.toLocaleString()} {!isMobile && ( ${row.price} )} {!isMobile && ( {row.expiration} )} )) ) : ( No products found. )}
{/* Pagination Footer */} Showing{' '} {(currentPage - 1) * itemsPerPage + 1} to{' '} {Math.min(currentPage * itemsPerPage, filteredData.length)} of{' '} {filteredData.length} setCurrentPage(value)} size={isMobile ? 'small' : 'medium'} sx={{ '& .MuiPaginationItem-root': { fontSize: { xs: '12px', sm: '14px' }, minWidth: { xs: 24, sm: 32 }, height: { xs: 24, sm: 32 }, backgroundColor: '#FFECE0', color: theme.palette.primary.main, borderRadius: '8px', '&.Mui-selected': { backgroundColor: theme.palette.primary.main, color: '#fff', }, '&:hover': { backgroundColor: '#FFD6B5', }, }, }} />
); }; export default TableView;