// material-ui
import * as React from 'react';
import {
    Button
} from '@mui/material';
import * as DateUtils from "utils/DateUtils";
import * as PublicNoteStatusUtils from "utils/statusUtils/PublicNoteStatusUtils"
import { FiDataGrid } from "components/FiDataGrid";
// ==============================|| EVENT TABLE ||============================== //

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

    const _sx = {
        padding: "4 2 4 2",
        boxShadow: 1,
        border: 1,
        borderColor: '#DDD',
        '& .MuiDataGrid-cell': {
            borderTop: 1,
            borderBottom: 1,
            borderColor: "#EEE"
        },
        '& .MuiDataGrid-footerContainer': {
            border: 1,
            borderColor: "#EEE"
        }
    }

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

    const handleEditClick = (params) => () => {
        window.open('/application/' + params.row.id);
    };

    const columns = [
        {
            id: 'appNo',
            field: 'appNo',
            headerName: 'App No.',
            flex: 1,
            renderCell: (params) => {
                return <Button onClick={handleEditClick(params)}><u>{params.row.appNo}</u></Button>;
            },
        },
        {
            field: 'status',
            headerName: 'Status',
            flex: 1,
            renderCell: (params) => {
                return PublicNoteStatusUtils.getStatusByTextEng(params.row.status, params.row.creditor);
            }
        },
        {
            field: 'date',
            headerName: 'Submit Date',
            flex: 1,
            renderCell: (params) => {
                return DateUtils.datetimeStr(params.row.created);
            }
        },
        {
            id: 'contactPerson',
            field: 'contactPerson',
            headerName: 'Contact Person',
            flex: 1,
            renderCell: (params) => {
                let company = params.row.enCompanyName != null?params.row.enCompanyName: params.row.chCompanyName;
                company = company != null ? company : "";

                /*
                let phone = JSON.parse(params.row.contactTelNo);
                let faxNo = JSON.parse(params.row.contactFaxNo);

                let contact = "";
                if (phone) {
                    contact = "Phone No.: " + phone?.countryCode + " " + phone?.phoneNumber
                }

                if (faxNo && faxNo?.faxNumber) {
                    if (contact != "")
                        contact = contact + ", "
                    contact = contact + "Fax No.:" + faxNo?.countryCode + " " + faxNo?.faxNumber
                }
                */

                return (<>
                    {params?.value}<br />{company}
                </>);
            }
        },
        {
            id: 'groupNo',
            field: 'groupNo',
            headerName: 'Gazette Group',
            flex: 1,
            valueGetter: (params) => {
                return (params?.value) ? (params?.value) : "";
            }
        },
    ];

    return (
        <div style={{ width: '100%' }}>

            <FiDataGrid
                sx={_sx}
                rowHeight={80}
                rows={rows}
                columns={columns}
                customPageSize={5}
                onRowDoubleClick={handleEditClick}
            />
        </div>
    );
}
