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

export default function SearchPublicNoticeTable({ searchCriteria, applyGridOnReady, applySearch}) {

    const navigate = useNavigate()

    const [_searchCriteria, set_searchCriteria] = React.useState(searchCriteria);
    React.useEffect(() => {
        set_searchCriteria(searchCriteria);
    }, [searchCriteria]);

    const columns = [
        {
            field: 'announceDate',
            headerName: 'Announcement Date',
            width: 250,
            cellClassName: 'announceDate',
            renderCell: (params) => {
                return clickableLink('/setting/announcement/details/' + params.id, DateUtils.datetimeStr(params?.value));
            },
        },
        {
            id: 'subjec',
            field: 'subjec',
            headerName: 'Subject',
            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
                columns={columns}
                customPageSize={10}
                getRowHeight={() => 'auto'}
                onRowDoubleClick={handleRowDoubleClick}
                applyGridOnReady={applyGridOnReady}
                applySearch = {applySearch}
                // doLoad={{
                //     url: GET_ANNOUNCE_LIST,
                //     params: _searchCriteria,
                // }}
                doLoad={React.useMemo(() => ({
                    url: GET_ANNOUNCE_LIST,
                    params: _searchCriteria,
                }), [_searchCriteria])}
            />
        </div>
    );
}
