import { useState, useMemo } from 'react'; import { DataTable } from '~/components/ui/DataTable'; import { Button } from '~/components/ui/Button'; import { Text } from '~/components/ui/Text'; import { processTableData, type TableState } from '~/lib/table-utils'; import { useSettings } from '~/contexts/SettingsContext'; import type { Customer } from '~/types/database'; interface EnhancedCustomerTableProps { customers: Customer[]; loading?: boolean; onEdit?: (customer: Customer) => void; onDelete?: (customer: Customer) => void; onView?: (customer: Customer) => void; } export function EnhancedCustomerTable({ customers, loading = false, onEdit, onDelete, onView, }: EnhancedCustomerTableProps) { const { formatDate } = useSettings(); const [tableState, setTableState] = useState({ search: '', filters: {}, sort: { key: 'name', direction: 'asc' }, pagination: { page: 1, pageSize: 10 }, }); // Define searchable fields const searchableFields = ['name', 'phone', 'email', 'address'] as const; // Process table data const processedData = useMemo(() => { return processTableData(customers, tableState, searchableFields); }, [customers, tableState]); // Handle sorting const handleSort = (key: string, direction: 'asc' | 'desc') => { setTableState(prev => ({ ...prev, sort: { key, direction }, })); }; // Handle page change const handlePageChange = (page: number) => { setTableState(prev => ({ ...prev, pagination: { ...prev.pagination, page }, })); }; // Define table columns const columns = [ { key: 'name' as keyof Customer, header: 'اسم العميل', sortable: true, filterable: true, render: (customer: Customer) => (
{customer.name}
), }, { key: 'phone' as keyof Customer, header: 'رقم الهاتف', sortable: true, filterable: true, render: (customer: Customer) => ( {customer.phone || '-'} ), }, { key: 'email' as keyof Customer, header: 'البريد الإلكتروني', sortable: true, filterable: true, render: (customer: Customer) => ( {customer.email || '-'} ), }, { key: 'address' as keyof Customer, header: 'العنوان', filterable: true, render: (customer: Customer) => ( {customer.address || '-'} ), }, { key: 'createdDate' as keyof Customer, header: 'تاريخ الإنشاء', sortable: true, render: (customer: Customer) => ( {formatDate(customer.createdDate)} ), }, ]; return (
{/* Table Header */}
قائمة العملاء إدارة بيانات العملاء
المجموع: {processedData.originalCount} {processedData.filteredCount !== processedData.originalCount && ( (مفلتر: {processedData.filteredCount}) )}
{/* Enhanced Data Table */} (
{onView && ( )} {onEdit && ( )} {onDelete && ( )}
), }} />
); }