// material-ui
import * as React from 'react';
import { useEffect } from "react";
import {
    Box,
    Stack,
    Table,
    TableBody,
    TableCell,
    TableHead,
    TableRow,
    Typography
} from '@mui/material';
import { FormattedMessage, useIntl } from "react-intl";
import FileDeleteButton from 'components/FileDeleteButton';
// ==============================|| EVENT TABLE ||============================== //

export default function UploadFileTable({ recordList, setUpdateRows, }) {

    const [rows, setRows] = React.useState(recordList);

    const intl = useIntl();

    useEffect(() => {
        setRows(recordList);
    }, [recordList]);

    const handleCancelClick = (id) => {
        setRows((prevRows) => {
            const newRows = prevRows.filter((row) => row.id !== id);
            setUpdateRows(newRows);
            return newRows;
        });
    };

    const formatFileSize = (size) => `${Math.ceil(size / 1024)} KB`;

    if (rows.length === 0) {
        return (
            <Box
                sx={{
                    height: '200px',
                    width: '100%',
                    border: 1,
                    borderColor: 'divider',
                    display: 'flex',
                    alignItems: 'center',
                    justifyContent: 'center',
                }}
            >
                <Typography component="span" variant="h6">
                    <FormattedMessage id="noFile"/>
                </Typography>
            </Box>
        );
    }

    return (
        <Box sx={{ height: '200px', width: '100%', border: 1, borderColor: 'divider', overflow: 'auto' }}>
            <Table size="small" aria-label={intl.formatMessage({ id: 'fileName' })}>
                <TableHead>
                    <TableRow>
                        <TableCell padding="checkbox" sx={{ width: 48 }} />
                        <TableCell>{intl.formatMessage({ id: 'fileName' })}</TableCell>
                        <TableCell align="right">{intl.formatMessage({ id: 'fileSize' })}</TableCell>
                    </TableRow>
                </TableHead>
                <TableBody>
                    {rows.map((row) => (
                        <TableRow key={row.id}>
                            <TableCell padding="checkbox">
                                <FileDeleteButton onDelete={() => handleCancelClick(row.id)} />
                            </TableCell>
                            <TableCell>
                                <Stack direction="row" alignItems="center">
                                    <Typography variant="body2">{row.name}</Typography>
                                </Stack>
                            </TableCell>
                            <TableCell align="right">
                                <Typography variant="body2">{formatFileSize(row.size)}</Typography>
                            </TableCell>
                        </TableRow>
                    ))}
                </TableBody>
            </Table>
        </Box>
    );
}
