// material-ui
import * as React from 'react';
import {
    Button, useMediaQuery,
} from '@mui/material';
import {
    GridActionsCellItem,
} from "@mui/x-data-grid";
import * as Icon from 'utils/IconUtils';
import { useNavigate } from "react-router-dom";
import * as DateUtils from "utils/DateUtils";
import * as ProofStatus from "utils/statusUtils/ProofStatus";
import {FiDataGrid} from "components/FiDataGrid";
import * as FormatUtils from "utils/FormatUtils"
import * as HttpUtils from "utils/HttpUtils"
import * as UrlUtils from "utils/ApiPathConst"
import {useTheme} from "@emotion/react";
// ==============================|| EVENT TABLE ||============================== //

export default function ProofTab({rows}) {

    const navigate = useNavigate()
    const theme = useTheme();
    const isMdOrLg = useMediaQuery(theme.breakpoints.up('md'));

    const handleEditClick = (params) => () => {
        navigate('/proof/reply/' + params.row.id);
    };

    const onDownloadClick = (params) => () => {
        HttpUtils.fileDownload({
            url: UrlUtils.GEN_GAZETTE_PROOF+"/"+params.row.id,
        });
    };

    const columns = [
        {
           
            field: 'refNo',
            headerName: 'Proof No.',
            width: isMdOrLg ? 'auto' : 200,
            flex: isMdOrLg ? 1 : undefined,
            cellClassName: 'actions',
            renderCell: (params) => {
                return <Button onClick={handleEditClick(params)}><u>{params.row.refNo}</u></Button>;
            },
        },
        {
            field: 'status',
            headerName: 'Status',
            width: isMdOrLg ? 'auto' : 160,
            flex: isMdOrLg ? 1 : undefined,
            renderCell: (params) => {
                return ProofStatus.getStatus_Eng(params);
            },
        },
        {
            field: 'created',
            headerName: 'Proof Date',
            width: isMdOrLg ? 'auto' : 160,
            flex: isMdOrLg ? 1 : undefined,

            valueGetter: (params) => {
                return DateUtils.datetimeStr(params?.value);
            }
        },
        {
            field: 'replyDate',
            headerName: 'Confirmed/Return Date',
            width: isMdOrLg ? 'auto' : 160,
            flex: isMdOrLg ? 1 : undefined,
            valueGetter: (params) => {
                return params?.value?DateUtils.datetimeStr(params?.value):"";
            }
        },
        {
            field: 'fee',
            headerName: 'Fee',
            width: isMdOrLg ? 'auto' : 160,
            flex: isMdOrLg ? 1 : undefined,

            valueGetter: (params) => {
                return (params?.value)?"$ "+FormatUtils.currencyFormat(params?.value):"";
            }
        },
        {
            type: 'actions',
            headerName: 'Proof Slip',
            width: 100,
            cellClassName: 'actions',
            getActions: (params) => {
                if(params.row.action == null) return[];
                if(params.row.action != true) return[];
                return [
                    <GridActionsCellItem
                        key="OutSave"
                        icon={<Icon.Download />}
                        label="Download"
                        className="textPrimary"
                        onClick={onDownloadClick(params)}
                        color="primary"
                    />]
            },
        },
    ];

    return (
        <div style={{height:'20%', width: '98%'}}>
            <FiDataGrid
                rows={rows}
                columns={columns}
                customPageSize={5}
            />
        </div>
    );
}
