import Box from '@mui/material/Box' import Checkbox from '@mui/material/Checkbox' import Paper from '@mui/material/Paper' 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 { visuallyHidden } from '@mui/utils' import { forwardRef, useDeferredValue, useEffect, useState } from 'react' import Image from 'next/image' import MuiAlert, { AlertProps } from '@mui/material/Alert' import Snackbar from '@mui/material/Snackbar' import Modal from '@mui/material/Modal' import { FormControl, InputLabel, MenuItem, Select, TextField, Typography } from '@mui/material' import FormData from 'form-data' import { InputUploadView } from '../inputUploadImg/inputUploadView' import { api } from '../../services/api' import FaqButton1 from '../buttons/faqButton/FaqButton1' import FaqButton2 from '../buttons/faqButton/FaqButton2' import { StyledStatus, TableView } from './TableView' import ReactLoading from 'react-loading' import { sanitizeStringSearch } from '../../utils/stringHelper' import { stableSort } from '../../utils/stableSort' const style = { position: 'absolute' as const, top: '50%', left: '50%', transform: 'translate(-50%, -50%)', width: 900, height: 500, bgcolor: 'background.paper', border: '2px solid #000', boxShadow: 24, p: 4, overflowY: 'scroll' } const Alert = forwardRef(function Alert( props, ref ) { return }) interface Data { clientCode: number name: string unity: string status: string } 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) } 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: false, disablePadding: false, label: 'name' }, { id: 'unity', numeric: false, disablePadding: false, label: 'unity' }, { id: 'status', numeric: false, 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 } interface ClientsTableInterface { clients: any onChange: any } function sortedClients(client, search: string) { search = sanitizeStringSearch(search) return client .map(client => ({ ...client, name: sanitizeStringSearch(client.name), client_id: sanitizeStringSearch(String(client.client_id)) })) .filter((client) => client.name.includes(search) || client.client_id.includes(search)) } 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({ clients, onChange }: ClientsTableInterface) { const [order, setOrder] = useState('asc') const [orderBy, setOrderBy] = useState('asc') const [selected, setSelected] = useState([]) const [page, setPage] = useState(0) const [dense, setDense] = useState(false) const [rowsPerPage, setRowsPerPage] = useState(5) const [openSnackError, setOpenSnackError] = useState(false) const [open, setOpen] = useState(false) const [openModalInativar, setOpenModalInativar] = useState(false) const [clientEdit, setClientEdit] = useState() const [logo, setLogo] = useState(false) const [imageURLS, setImageURLs] = useState([]) const [images, setImages] = useState([] as any) const [nivelAcess, setnivelAcess] = useState(2) const [openEditUserModal, setOpenEditUserModal] = useState(false) const [openSnackSuccess, setOpenSnackSuccess] = useState(false) const [loading, setLoading] = useState(false) const [selectedClient, setSelectedClient] = useState(2) const [search, setSearch] = useState('') const [units, setUnits] = useState([]) // Avoid a layout jump when reaching the last page with empty rows. const emptyRows = page > 0 ? Math.max(0, (1 + page) * rowsPerPage - clients.length) : 0 const formData = new FormData() const listClients = useDeferredValue( sortedClients(stableSort(clients, getComparator(order, orderBy)), search) .slice(page * rowsPerPage, page * rowsPerPage + rowsPerPage) ) // const handleOpen = () => setOpen(true) const handleClose = () => setOpen(false) function getClientUnits(client_id: number) { api .post('/units', { filters: [ { type: '=', field: 'dados_cadastrais.cod_smart_cliente', value: client_id } ], fields: ['unidade'], distinct: true }) .then((res) => setUnits(res.data.data)) .catch(() => setOpenSnackError(true)) return units } const handleCloseSnack = ( event?: React.SyntheticEvent | Event, reason?: string ) => { if (reason === 'clickaway') { return } setOpenSnackError(false) } 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 = clients.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 function onImageChange(e: any) { setImages([...e.target.files]) setLogo(e.target.files[0]) } async function handleUpdateClient(props, id) { logo && formData.append('file', logo) let new_profile_picture try { setLoading(true) if (logo) { const { data } = await api.post('/sendFile', formData) new_profile_picture = data.url } await api.put(`/user/${id}`, { ...props, profile_picture: new_profile_picture }) setOpenSnackSuccess(true) setOpenModalInativar(false) // window.location.reload() } catch (err) { setOpenSnackError(true) } finally { setLoading(false) } } useEffect(() => { onChange(selected) }, [selected]) useEffect(() => { if (images.length < 1) return const newImageUrls: any = [] images.forEach((image: any) => newImageUrls.push(URL.createObjectURL(image)) ) setImageURLs(newImageUrls) }, [images]) return ( Não foi possivel encontrar unidades do client! setSearch(e.target.value)} placeholder="Pesquisar por nome:" /> {listClients.map((row, index) => { const isItemSelected = isSelected(row.id) const labelId = `enhanced-table-checkbox-${index}` return ( handleClick(event, row.id.toString())} role="checkbox" aria-checked={isItemSelected} tabIndex={-1} key={row.id} selected={isItemSelected} > Client - {row.client_id} { setOpenEditUserModal(true) setSelectedClient(row) setClientEdit({ email: row.email, name: row.name, client_id: row.client_id, profile_picture: row.profile_picture }) }} > {row.name} { setOpen(true) getClientUnits(row.client_id) setSelectedClient(row) }} > clique aqui para ver as unidades {' '} {row.deleted_at ? 'inativo' : 'ativo'} ) })} {emptyRows > 0 && ( )}
setOpenEditUserModal(false)} aria-labelledby="modal-modal-title" aria-describedby="modal-modal-description" >

Editar Cliente - {selectedClient.name}

Adicionar Cliente Smart Energia
{ setClientEdit({ ...clientEdit, name: value.target.value }) }} value={clientEdit?.name} variant="outlined" /> { setClientEdit({ ...clientEdit, email: value.target.value.toLowerCase() }) }} variant="outlined" /> { setClientEdit({ ...clientEdit, password: value.target.value }) }} variant="outlined" /> { setClientEdit({ ...clientEdit, password_confirmation: value.target.value }) }} variant="outlined" /> { setClientEdit({ ...clientEdit, client_id: value.target.value }) }} variant="outlined" />
{imageURLS.map((imageSrc, index) => { return ( ) })}
Nivel de acesso
{!loading && ( setOpenEditUserModal(false)} /> )} {!loading ? ( handleUpdateClient(clientEdit, selectedClient.id)} /> ) : ( )}
{units.map((units, index) => { return ( <>
  • {units.unidade}

  • ) })}
    ) }