// material-ui
import * as React from 'react';
import {
    Button,
    Box
} from '@mui/material';
import * as DateUtils from "utils/DateUtils";
import { useNavigate } from "react-router-dom";
import { FiDataGrid } from "components/FiDataGrid";
// ==============================|| EVENT TABLE ||============================== //

export default function SearchPublicNoticeTable({ recordList }) {
    const [rows, setRows] = React.useState(recordList);
    const navigate = useNavigate()

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

    const handleEditClick = (params) => () => {
        navigate('/setting/announcement/details/' + params.id);
    };


    const columns = [
        {
            field: 'announceDate',
            headerName: 'Announce Date',
            width: 250,
            cellClassName: 'announceDate',
            renderCell: (params) => {
                return <Button onClick={handleEditClick(params)}><u>{DateUtils.datetimeStr(params?.value)}</u></Button>;
            },
        },
        {
            id: 'subjec',
            field: 'subjec',
            headerName: 'Subjec',
            minWidth: 400,
            renderCell: (params) => {
                return <>
                    <Box sx={{ textOverflow: 'ellipsis', overflow: 'hidden', 'white-space': 'nowrap', }}>
                        <Box component="div" sx={{ textOverflow: 'ellipsis', overflow: 'hidden', 'white-space': 'nowrap', }}>{params.row.subjectEng}</Box>
                        <Box component="div" sx={{ textOverflow: 'ellipsis', overflow: 'hidden', 'white-space': 'nowrap', }}>{params.row.subjectCht}</Box>
                        <Box component="div" sx={{ textOverflow: 'ellipsis', overflow: 'hidden', 'white-space': 'nowrap', }}>{params.row.subjectChs}</Box>
                    </Box>
                </>
            },
        },
        {
            id: 'content',
            field: 'content',
            headerName: 'Content',
            flex:1,
            minWidth: 400,
            renderCell: (params) => {
                return <>
                    <Box sx={{ textOverflow: 'ellipsis', overflow: 'hidden', 'white-space': 'nowrap', }}>
                        <Box sx={{ textOverflow: 'ellipsis', overflow: 'hidden', 'white-space': 'nowrap', }}>{params.row.contentEng}</Box>
                        <Box sx={{ textOverflow: 'ellipsis', overflow: 'hidden', 'white-space': 'nowrap', }}>{params.row.contentCht}</Box>
                        <Box sx={{ textOverflow: 'ellipsis', overflow: 'hidden', 'white-space': 'nowrap', }}>{params.row.contentChs}</Box>
                    </Box >
                </>
            }
        },
    ];

    function handleRowDoubleClick(params) {
        navigate('/setting/announcement/details/' + params.id);
    }

    return (
        <div style={{ width: '100%' }}>
            <FiDataGrid
                rows={rows}
                columns={columns}
                customPageSize={10}
                getRowHeight={() => 'auto'}
                onRowDoubleClick={handleRowDoubleClick}
            />
        </div>
    );
}
