// material-ui
import * as React from 'react';
import {
    Button, useMediaQuery
} 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";
import {
    isORGLoggedIn,
  } from "utils/Utils";
import {useTheme} from "@emotion/react";
import {useIntl} from "react-intl";
// ==============================|| EVENT TABLE ||============================== //

export default function SearchPublicNoticeTable({ recordList }) {
    const [rows, setRows] = React.useState(recordList);
    const navigate = useNavigate()
    const theme = useTheme();
    const isMdOrLg = useMediaQuery(theme.breakpoints.up('md'));
    const intl = useIntl();
    const { locale } = intl;

    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) => () => {
        navigate('/proof/reply/' + params.row.id);
    };

    /*eslint no-irregular-whitespace: ["error", { "skipComments": true }]*/
    /*const getGroupTitle = (title) => {
        switch (title) {
            case 'Private Bill':
                return "私人帳單"; 
            case 'Companies Ordinance':
                return "公司條例"; 
            case 'High Court':
                return "高等法院"; 
            case 'Notices':
                return "通知"; 
            case 'Miscellaneous (Companies)':
                return "其他"; 
            default:
                return title;
        }
    }*/
    
    const columns = [
        {
            field: 'actions',
            headerName: intl.formatMessage({id: 'proofId'}),
            width: isMdOrLg ? 'auto' : 250,
            flex: isMdOrLg ? 1.5 : undefined,
            cellClassName: 'actions',
            renderCell: (params) => {
                return <Button onClick={handleEditClick(params)}><u>{params.row.refNo}</u></Button>;
            },
        },
        {
            id: 'appId',
            field: 'appId',
            headerName: isORGLoggedIn()?intl.formatMessage({id: 'gazetteCount3'}):intl.formatMessage({id: 'gazetteCount2'}),
            width: isMdOrLg ? 'auto' : 160,
            flex: isMdOrLg ? 1 : undefined,
            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 }}>{appNo}<br />{isssue}<br />{params.row.appRemarks}</div>
                return <div style={{ margin: 4 }}>{isssue}<br />{isORGLoggedIn()?<>{params.row.appCareOf}<br /></>:null}{params.row.appRemarks}</div>
            },
        },
        {
            id: 'created',
            field: 'created',
            headerName: intl.formatMessage({id: 'proofDate'}),
            width: isMdOrLg ? 'auto' : 250,
            flex: isMdOrLg ? 1.5 : undefined,
            valueGetter: (params) => {
                return DateUtils.datetimeStr(params?.value);
            }
        },
        {
            id: 'proofPaymentDeadline',
            field: 'proofPaymentDeadline',
            headerName: intl.formatMessage({id: 'replyBefore'}),
            width: isMdOrLg ? 'auto' : 250,
            flex: isMdOrLg ? 1.5 : undefined,
            valueGetter: (params) => {
                let proofPaymentDeadline = DateUtils.convertToDate(params?.value);
                proofPaymentDeadline = proofPaymentDeadline.setHours(14, 0, 0, 0);
                return locale === 'en' ? DateUtils.datetimeStr(proofPaymentDeadline) : DateUtils.datetimeStr_Cht(proofPaymentDeadline);
            }
        },
        {
            id: 'replyDate',
            field: 'replyDate',
            headerName: intl.formatMessage({id: 'replyDate'}),
            width: isMdOrLg ? 'auto' : 250,
            flex: isMdOrLg ? 1.5 : undefined,
            valueGetter: (params) => {
                return params?.value ? DateUtils.datetimeStr(params?.value) : "";
            }
        },
        {
            id: 'actions',
            headerName: intl.formatMessage({id: 'status'}),
            width: isMdOrLg ? 'auto' : 160,
            flex: isMdOrLg ? 1 : undefined,
            renderCell: (params) => {
                return locale === 'en' ? ProofStatus.getStatus_Eng(params) : ProofStatus.getStatus_Cht(params);
            },
        },
        {
            id: 'fee',
            field: 'fee',
            headerName: intl.formatMessage({id: 'fee'}),
            width: isMdOrLg ? 'auto' : 250,
            flex: isMdOrLg ? 1.5 : undefined,
            valueGetter: (params) => {
                return (params?.value) ? "$ " + FormatUtils.currencyFormat(params?.value) : "";
            }
        },
    ];

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

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

            <FiDataGrid
                sx={_sx}
                rows={rows}
                columns={columns}
                customPageSize={10}
                getRowHeight={() => "auto"}
                onRowDoubleClick={handleRowDoubleClick}
            />
        </div>
    );
}