// material-ui
import * as React from 'react';
import {FiDataGrid} from "components/FiDataGrid";
import {
    Button,
    // Typography, 
    useMediaQuery
} from '@mui/material';
import * as DateUtils from "utils/DateUtils"
import {useNavigate} from "react-router-dom";
import {
    isORGLoggedIn,
  } from "utils/Utils";
import {useTheme} from "@emotion/react";
import {getStatusIntl} from "utils/statusUtils/PublicNoteStatusUtils";
import {FormattedMessage, useIntl} from "react-intl";
// ==============================|| EVENT TABLE ||============================== //

export default function BaseGrid({rows}) {
    const navigate = useNavigate()
    const theme = useTheme();
    const isMdOrLg = useMediaQuery(theme.breakpoints.up('md'));
    const intl = useIntl();

    const handleDetailClick = (params) => () => {
        navigate('/publicNotice/'+ params.id);
    };

    // const remarkHeadername = rows.orgId===null?"我的備註":"Care Of / 我的備註"

    const columns = [
        {
            id: 'appNo',
            field: 'appNo',
            headerName: intl.formatMessage({id: 'applicationId'}),
            width: isMdOrLg ? 'auto' : 160,
            flex: isMdOrLg ? 1 : undefined,
        },
        {
            id: 'created',
            field: 'created',
            headerName: intl.formatMessage({id: 'submitDate'}),
            width: isMdOrLg ? 'auto' : 300,
            flex: isMdOrLg ? 1 : undefined,
            valueGetter:(params)=>{
                return DateUtils.datetimeStr(params?.value);
            }
        },
        // {
        //     id: 'contactPerson',
        //     field: 'contactPerson',
        //     headerName: '聯絡人',
        //     flex: 2,
        //     renderCell: (params) => {
        //         let phone = JSON.parse(params.row.contactTelNo);
        //         let faxNo = JSON.parse(params.row.contactFaxNo);

        //         let contact = "";
        //         if (phone) {
        //             contact = "電話: " + phone?.countryCode + " " + phone?.phoneNumber
        //         }

        //         if (faxNo && faxNo?.faxNumber) {
        //             if (contact != "")
        //                 contact = contact + ", "
        //             contact = contact + "傳真:" + faxNo?.countryCode + " " + faxNo?.faxNumber
        //         }

        //         return (<>
        //             {params?.value}<br />
        //             {contact}
        //         </>);
        //     }
        // },
        {
            id: 'remarks',
            field: 'remarks',
            headerName:  isORGLoggedIn()? intl.formatMessage({id: 'gazetteCount2_1'}) : intl.formatMessage({id: 'myRemarks'}),
            width: isMdOrLg ? 'auto' : 400,
            flex: isMdOrLg ? 3 : undefined,
            renderCell: (params) => (
                isORGLoggedIn()? 
                <div>
                    Care Of: {params.row.careOf} <br/>
                    <FormattedMessage variant="pnspsNormalText" id="myRemarks"/>: {params.row.remarks}
    
                </div>:
                <div>
                    {params.row.remarks}
                    {/* <Typography variant="pnspsNormalText">{params.row.remarks}</Typography> */}
                </div>
              )
        },
        {
            id: 'status',
            field: 'status',
            headerName: intl.formatMessage({id: 'status'}),
            width: isMdOrLg ? 'auto' : 160,
            flex: isMdOrLg ? 1 : undefined,
            renderCell: (params) => {
                return [getStatusIntl(params,intl)]
            },
        },
        {
            field: 'actions',
            headerName: '',
            width: 160,
            cellClassName: 'actions',
            renderCell: (params) => {
                return <Button aria-label={intl.formatMessage({id: 'viewDetail'})} onClick={handleDetailClick(params)}>
                    <FormattedMessage id="viewDetail"/>
                </Button>;
            },
        }
    ];
    
    function handleRowDoubleClick(params) {
        navigate('/publicNotice/'+ params.id);
    }

    return (
        <div style={{minHeight: 400, height:"100%", width: '100%', padding: 4}}>
            <FiDataGrid
                rows={rows}
                columns={columns}
                customPageSize={10}
                onRowDoubleClick={handleRowDoubleClick}
                getRowHeight={() => 'auto'}
            />
        </div>
    );
}
