// material-ui
import * as React from 'react';
import {
    Button
} from '@mui/material';
import * as DateUtils from "utils/DateUtils";
import * as FormatUtils from "utils/FormatUtils";
import * as StatusUtils from "utils/statusUtils/PublicNoteStatusUtils";
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('/application/' + params.id);
    };


    const columns = [
        {
            field: 'actions',
            headerName: 'App No.',
            width: 150,
            cellClassName: 'actions',
            renderCell: (params) => {
                return <Button onClick={handleEditClick(params)}><u>{params.row.appNo}</u></Button>;
            },
        },
        {
            id: 'status',
            field: 'status',
            headerName: 'Status',
            width: 300,
            renderCell: (params) => {
                return [StatusUtils.getStatusEng(params)]
            },
        },
        {
            id: 'created',
            field: 'created',
            headerName: 'Submit Date',
            flex: 1,
            valueGetter: (params) => {
                return DateUtils.datetimeStr(params?.value);
            }
        },
        {
            id: 'contactPerson',
            field: 'contactPerson',
            headerName: 'Contact Person',
            flex: 2,
            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) : "";
            }
        },
        {
            id: 'issueId',
            field: 'issueId',
            headerName: 'Issue No',
            flex: 1,
            valueGetter: (params) => {
                return params.row.issueYear
                    + " Vol. " + FormatUtils.zeroPad(params.row.issueVolume, 3)
                    + ", No. " + FormatUtils.zeroPad(params.row.issueNo, 2)
                    + ", " + DateUtils.dateFormat(params.row.issueDate, "D MMM YYYY (ddd)");
            }
        },
    ];

    function handleRowDoubleClick(params) {
        // handleEditClick(params)
        navigate('/application/' + params.id);
    }

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