import DeleteIcon from '@mui/icons-material/Delete'; import FilterListIcon from '@mui/icons-material/FilterList'; import Box from '@mui/material/Box'; import Checkbox from '@mui/material/Checkbox'; import FormControlLabel from '@mui/material/FormControlLabel'; import IconButton from '@mui/material/IconButton'; import Paper from '@mui/material/Paper'; import { alpha } from '@mui/material/styles'; import Switch from '@mui/material/Switch'; import Table from '@mui/material/Table'; import TableBody from '@mui/material/TableBody'; import TableCell from '@mui/material/TableCell'; import TableContainer from '@mui/material/TableContainer'; import TableHead from '@mui/material/TableHead'; import TablePagination from '@mui/material/TablePagination'; import TableRow from '@mui/material/TableRow'; import TableSortLabel from '@mui/material/TableSortLabel'; import Toolbar from '@mui/material/Toolbar'; import Tooltip from '@mui/material/Tooltip'; import Typography from '@mui/material/Typography'; import { visuallyHidden } from '@mui/utils'; import React, { useState } from 'react'; import { ClientTableView, StyledStatus } from './ClientsTableView'; interface Data { clientCode: number, name: string, unity: string, status: string, } function createData( clientCode: number, name: string, unity: string, status: string, ): Data { return { clientCode, name, unity, status, }; } const rows = [ createData(9500130, 'Copel', 'clique para ver unidades', 'ativo'), createData(9500131, 'Copel', 'clique para ver unidades', 'ativo'), createData(9500132, 'Copel', 'clique para ver unidades', 'ativo'), createData(9500689, 'Copel', 'clique para ver unidades', 'pendente'), createData(9500690, 'Copel', 'clique para ver unidades', 'inativo'), createData(9500691, 'Copel', 'clique para ver unidades', 'inativo'), ]; function descendingComparator(a: T, b: T, orderBy: keyof T) { if (b[orderBy] < a[orderBy]) { return -1; } if (b[orderBy] > a[orderBy]) { return 1; } return 0; } type Order = 'asc' | 'desc'; function getComparator( order: Order, orderBy: any, ): ( a: { [key in Key]: number | string }, b: { [key in Key]: number | string }, ) => number { return order === 'desc' ? (a, b) => descendingComparator(a, b, orderBy) : (a, b) => -descendingComparator(a, b, orderBy); } function stableSort(array: readonly T[], comparator: (a: T, b: T) => number) { const stabilizedThis = array.map((el, index) => [el, index] as [T, number]); stabilizedThis.sort((a, b) => { const order = comparator(a[0], b[0]); if (order !== 0) { return order; } return a[1] - b[1]; }); return stabilizedThis.map((el) => el[0]); } interface HeadCell { disablePadding: boolean; id: keyof Data | string; label: string; numeric: boolean; } const headCells: readonly HeadCell[] = [ { id: 'clientCode', numeric: false, disablePadding: true, label: 'código do cliente', }, { id: 'name', numeric: true, disablePadding: false, label: 'name', }, { id: 'unity', numeric: true, disablePadding: false, label: 'unity', }, { id: 'status', numeric: true, disablePadding: false, label: 'status', }, ]; interface EnhancedTableProps { numSelected: number; onRequestSort: (event: React.MouseEvent, property: keyof Data) => void; onSelectAllClick: (event: React.ChangeEvent) => void; order: Order; orderBy: string; rowCount: number; } function EnhancedTableHead(props: EnhancedTableProps) { const { onSelectAllClick, order, orderBy, numSelected, rowCount, onRequestSort } = props; const createSortHandler = (property: any) => (event: React.MouseEvent) => { onRequestSort(event, property); }; return ( 0 && numSelected < rowCount} checked={rowCount > 0 && numSelected === rowCount} onChange={onSelectAllClick} inputProps={{ 'aria-label': 'select all desserts', }} /> {headCells.map((headCell) => ( {headCell.label} {orderBy === headCell.id ? ( {order === 'desc' ? 'sorted descending' : 'sorted ascending'} ) : null} ))} ); } export default function ClientTable() { const [order, setOrder] = useState('asc'); const [orderBy, setOrderBy] = useState('status'); const [selected, setSelected] = useState([]); const [page, setPage] = useState(0); const [dense, setDense] = useState(false); const [rowsPerPage, setRowsPerPage] = useState(5); const handleRequestSort = ( event: React.MouseEvent, property: keyof Data, ) => { const isAsc = orderBy === property && order === 'asc'; setOrder(isAsc ? 'desc' : 'asc'); setOrderBy(property); }; const handleSelectAllClick = (event: React.ChangeEvent) => { if (event.target.checked) { const newSelecteds = rows.map((n) => n.name); setSelected(newSelecteds); return; } setSelected([]); }; const handleClick = (event: React.MouseEvent, code: string) => { const selectedIndex = selected.indexOf(code); let newSelected: readonly string[] = []; if (selectedIndex === -1) { newSelected = newSelected.concat(selected, code); } else if (selectedIndex === 0) { newSelected = newSelected.concat(selected.slice(1)); } else if (selectedIndex === selected.length - 1) { newSelected = newSelected.concat(selected.slice(0, -1)); } else if (selectedIndex > 0) { newSelected = newSelected.concat( selected.slice(0, selectedIndex), selected.slice(selectedIndex + 1), ); } setSelected(newSelected); }; const handleChangePage = (event: unknown, newPage: number) => { setPage(newPage); }; const handleChangeRowsPerPage = (event: React.ChangeEvent) => { setRowsPerPage(parseInt(event.target.value, 10)); setPage(0); }; const isSelected = (code: any) => selected.indexOf(code.toString()) !== -1; // Avoid a layout jump when reaching the last page with empty rows. const emptyRows = page > 0 ? Math.max(0, (1 + page) * rowsPerPage - rows.length) : 0; return ( {stableSort(rows, getComparator(order, orderBy)) .slice(page * rowsPerPage, page * rowsPerPage + rowsPerPage) .map((row, index) => { const isItemSelected = isSelected(row.clientCode); const labelId = `enhanced-table-checkbox-${index}`; return ( handleClick(event, row.clientCode.toString())} role="checkbox" aria-checked={isItemSelected} tabIndex={-1} key={row.clientCode} selected={isItemSelected} > Unidade - {row.clientCode} {row.name} {row.unity} {row.status} ); })} {emptyRows > 0 && ( )}
); }