// 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 ProofStatus from "utils/statusUtils/ProofStatus";
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('/proof/reply/' + params.row.id);
    };

    const columns = [
        {
            field: 'actions',
            headerName: 'Proof No.',
            width: 150,
            cellClassName: 'actions',
            renderCell: (params) => {
                return <Button onClick={handleEditClick(params)}><u>{params.row.refNo}</u></Button>;
            },
        },
        {
            id: 'appId',
            field: 'appId',
            headerName: 'Application No./ Gazette Code/ Gazette Issue',
            flex: 1.5,
            renderCell: (params) => {
                let appNo = params.row.appNo;
                let code = params.row.groupNo;
                let isssue = 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)");

                return <div style={{ margin: 4 }}>App No: {appNo}<br />Gazette Code: {code}<br />Issue: {isssue}</div>
            },
        },
        {
            id: 'actions',
            headerName: 'Status',
            flex: 1,
            renderCell: (params) => {
                return ProofStatus.getStatus_Eng(params);
            },
        },
        {
            id: 'created',
            field: 'created',
            headerName: 'Proof Date',
            flex: 1,
            valueGetter: (params) => {
                return DateUtils.datetimeStr(params?.value);
            }
        },
        {
            id: 'replyDate',
            field: 'replyDate',
            headerName: 'Confirmed/Return Date',
            flex: 1,
            valueGetter: (params) => {
                return params?.value ? DateUtils.datetimeStr(params?.value) : "";
            }
        },
        {
            id: 'contactPerson',
            field: 'contactPerson',
            headerName: 'Client',
            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: 'groupTitle',
            field: 'groupTitle',
            headerName: 'Gazette Group',
            flex: 1,
            valueGetter: (params) => {
                return (params?.value) ? (params?.value) : "";
            }
        },
        {
            id: 'fee',
            field: 'fee',
            headerName: 'Fee',
            flex: 1,
            valueGetter: (params) => {
                return (params?.value) ? "$ " + FormatUtils.currencyFormat(params?.value) : "";
            }
        },
    ];

    function handleRowDoubleClick(params) {
        navigate('/proof/reply/' + params.row.id);
    }

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