'use client'; import { useState } from 'react'; import type { CoilUsageRow } from '@/types/coil-activity'; import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from '@/components/ui/card'; import { Input } from '@/components/ui/input'; import { Button } from '@/components/ui/button'; import { Table, TableBody, TableCell, TableRow, } from '@/components/ui/table'; import { Download, Search } from 'lucide-react'; import { SortableTableHead, useSortableTable, } from '@/components/ui/sortable-table-head'; import { formatDate } from '@/lib/utils'; type Props = { data: CoilUsageRow[]; }; export function UsageTable({ data }: Props) { const [searchTerm, setSearchTerm] = useState(''); const filteredData = data.filter((row) => { const s = searchTerm.toLowerCase(); return ( (row.vorteq_part_num || '').toLowerCase().includes(s) || (row.customer_part_num || '').toLowerCase().includes(s) || (row.part_desc || '').toLowerCase().includes(s) || (row.lot_num || '').toLowerCase().includes(s) || (row.mfg_lot || '').toLowerCase().includes(s) || (row.plant_name || '').toLowerCase().includes(s) || (row.job_num || '').toLowerCase().includes(s) || (row.customer_po || '').toLowerCase().includes(s) ); }); const { sortKey, sortDirection, handleSort, sortedData } = useSortableTable(filteredData); const handleExportCSV = () => { const headers = [ 'Date Used', 'Vorteq Part#', 'Cust Part#', 'Part Description', 'Lot#', 'Mfg Lot#', 'Weight', 'Plant Name', 'Job#', 'Cust PO#', 'Qty LB', ]; const rows = sortedData.map((row) => [ row.date_used ? new Date(row.date_used).toLocaleDateString() : '', row.vorteq_part_num || '', row.customer_part_num || '', row.part_desc || '', row.lot_num || '', row.mfg_lot || '', row.weight ?? '', row.plant_name || '', row.job_num || '', row.customer_po || '', row.on_hand_qty ?? '', ]); const csvContent = [headers, ...rows] .map((row) => row.map((cell) => `"${cell}"`).join(',')) .join('\n'); const blob = new Blob([csvContent], { type: 'text/csv' }); const url = window.URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; a.download = `usage-data-${new Date().toISOString().split('T')[0]}.csv`; a.click(); window.URL.revokeObjectURL(url); }; return ( Usage Data Showing {sortedData.length} results
setSearchTerm(e.target.value)} className="pl-8" />
Date Used Vorteq Part# Cust Part# Part Description Lot# Mfg Lot# Weight Plant Name Job# Cust PO# Qty LB {sortedData.length === 0 ? ( No usage data found ) : ( sortedData.map((row, i) => ( {row.date_used ? formatDate(new Date(row.date_used)) : '-'} {row.vorteq_part_num || '-'} {row.customer_part_num || '-'} {row.part_desc || '-'} {row.lot_num || '-'} {row.mfg_lot || '-'} {row.weight != null ? row.weight.toLocaleString() : '-'} {row.plant_name || '-'} {row.job_num || '-'} {row.customer_po || '-'} {row.on_hand_qty != null ? row.on_hand_qty.toLocaleString() : '-'} )) )}
Showing {sortedData.length} of {data.length} records
); }