// material-ui
import {
    FormControl,
    Grid,
    Typography,
    FormLabel,
    TextField,
    Stack
} from '@mui/material';

import { useFormik } from 'formik';
import * as React from "react";
import * as DateUtils from "utils/DateUtils"
import * as FormatUtils from "utils/FormatUtils"
import { useParams } from "react-router-dom";
import Loadable from 'components/Loadable';
const MainCard = Loadable(React.lazy(() => import('components/MainCard')));
import * as StatusUtils from "utils/statusUtils/PublicNoteStatusUtils";
import FileList from "components/FileList"
import { FormattedMessage, useIntl } from "react-intl";
import {
    isORGLoggedIn,
} from "utils/Utils";
// ==============================|| DASHBOARD - DEFAULT ||============================== //
const ApplicationDetailCard = ({ formData, }) => {

    const params = useParams();
    const intl = useIntl();
    const { locale } = intl;

    const [data, setData] = React.useState({});
    //const [proofId, setProofId] = React.useState();

    React.useEffect(() => {
        if (formData) {
            setData(formData);
            //setProofId(formData.id);
        }
    }, [formData]);

    const formik = useFormik({
        enableReinitialize: true,
        initialValues: data,
    });

    const DisplayField = ({ name, width }) => {
        return <TextField
            fullWidth
            disabled
            size="small"
            onChange={formik.handleChange}
            id={name}
            name={name}
            value={formik.values[name]}
            variant="outlined"
            sx={
                {
                    "& .MuiInputBase-input.Mui-disabled": {
                        WebkitTextFillColor: "#000000",
                        background: "#f8f8f8",
                    },
                    width: width ? width : '100%'
                }
            }
        />;
    }

    return (
        <MainCard elevation={0}
            border={false}
            content={false}
        >
            <Typography variant="h4" component="h2" sx={{ textAlign: "left", mb: 2, borderBottom: "1px solid black" }}>
                <FormattedMessage id="publicNoticePaymentProofInfo" />
            </Typography>
            <form>
                <Grid container direction="column">
                    <Grid item xs={12} md={12} lg={12}>
                        <Grid container direction="row" justifyContent="space-between"
                            alignItems="center">
                            <Grid item xs={12} md={6} lg={6} sx={{ mb: 1 }}>
                                <Grid container alignItems={"center"}>
                                    <Grid item xs={12} md={3} lg={3}
                                        sx={{ display: 'flex', alignItems: 'center' }}>
                                        <FormLabel><Typography variant="pnspsFormParagraph">
                                            <FormattedMessage id="applicationId" />:
                                        </Typography></FormLabel>
                                    </Grid>

                                    <Grid item xs={12} md={9} lg={9}>
                                        <DisplayField name="appNo" />
                                    </Grid>
                                </Grid>
                            </Grid>
                            <Grid item xs={12} md={5} lg={5} sx={{ mb: 1 }}>
                                <Grid container alignItems={"left"}>
                                    <Grid item xs={12} md={3} lg={3}
                                        sx={{ display: 'flex', alignItems: 'center' }}>
                                        <FormLabel><Typography variant="pnspsFormParagraph">
                                            <FormattedMessage id="applyStatus" />:
                                        </Typography></FormLabel>
                                    </Grid>

                                    <Grid item xs={12} md={9} lg={9} sx={{ display: 'flex', alignItems: 'center' }}>
                                        <FormControl variant="outlined">
                                            {StatusUtils.getStatusByTextIntl(data.appStatus, false, intl)}
                                        </FormControl>
                                    </Grid>
                                </Grid>
                            </Grid>
                        </Grid>
                        <Grid container direction="row" justifyContent="space-between"
                            alignItems="center">
                            <Grid item xs={12} md={6} lg={6} sx={{ mb: 1 }}>
                                <Grid container alignItems={"center"}>
                                    <Grid item xs={12} md={3} lg={3}
                                        sx={{ display: 'flex', alignItems: 'center' }}>
                                        <FormLabel><Typography variant="pnspsFormParagraph">
                                            <FormattedMessage id="applyPerson" />:
                                        </Typography></FormLabel>
                                    </Grid>

                                    <Grid item xs={12} md={9} lg={9}>
                                        <FormControl variant="outlined" fullWidth disabled >
                                            {data.orgId === null ?
                                                <DisplayField name="contactPerson" />
                                                :
                                                <DisplayField name="applicant" />
                                            }
                                        </FormControl>
                                    </Grid>
                                </Grid>
                            </Grid>
                            <Grid item xs={12} md={5} lg={5} sx={{ mb: 1, ml: { xs: 0, sm: 0, md: 1, lg: 1 } }}>
                                <Grid container alignItems={"center"}>
                                    <Grid item xs={12} md={3} lg={3}
                                        sx={{ display: 'flex', alignItems: 'center' }}>
                                        <FormLabel><Typography variant="pnspsFormParagraph">
                                            <FormattedMessage id="gazetteCount" />:
                                        </Typography></FormLabel>
                                    </Grid>

                                    <Grid item xs={12} md={9} lg={9}>
                                        <DisplayField name="issueNoStr" />
                                    </Grid>
                                </Grid>
                            </Grid>
                        </Grid>
                        <Grid container direction="row" justifyContent="space-between"
                            alignItems="center">
                            <Grid item xs={12} md={6} lg={6} sx={{ mb: 1 }}>
                                <Grid container alignItems={"center"}>
                                    <Grid item xs={12} md={3} lg={3}
                                        sx={{ display: 'flex', alignItems: 'center' }}>
                                        <FormLabel><Typography variant="pnspsFormParagraph">
                                            <FormattedMessage id="contactPerson" />:
                                        </Typography></FormLabel>
                                    </Grid>

                                    <Grid item xs={12} md={9} lg={9}>
                                        <DisplayField name="contactPerson" />

                                    </Grid>
                                </Grid>
                            </Grid>
                            <Grid item xs={12} md={5} lg={5} sx={{ mb: 1, ml: { xs: 0, sm: 0, md: 1, lg: 1 } }}>
                                <Grid container alignItems={"center"}>
                                    <Grid item xs={12} md={3} lg={3}
                                        sx={{ display: 'flex', alignItems: 'center' }}>
                                        <FormLabel><Typography variant="pnspsFormParagraph">
                                            <FormattedMessage id="applicationPublishDate" />:
                                        </Typography></FormLabel>
                                    </Grid>

                                    <Grid item xs={12} md={9} lg={9}>
                                        <DisplayField name="issueDateStr" />
                                    </Grid>
                                </Grid>
                            </Grid>
                        </Grid>
                        {isORGLoggedIn() ?
                            <>
                                <Grid container direction="row" justifyContent="space-between"
                                    alignItems="center">
                                    <Grid item xs={12} md={6} lg={6} sx={{ mb: 1, }}>
                                        <Grid container alignItems="left">
                                            <Grid item xs={12} md={3} lg={3}
                                                sx={{ display: 'flex', alignItems: 'center' }}>
                                                <FormLabel><Typography variant="pnspsFormParagraph">
                                                    <FormattedMessage id="careOf" />:
                                                </Typography></FormLabel>
                                            </Grid>

                                            <Grid item xs={12} md={9} lg={9}>
                                                <Stack direction="row">
                                                    <DisplayField name="careOf" />
                                                </Stack>
                                            </Grid>
                                        </Grid>
                                    </Grid>
                                </Grid>
                            </>
                            :
                            null

                        }
                        <Grid container direction="row" justifyContent="space-between"
                            alignItems="center">
                            <Grid item xs={12} md={6} lg={6} sx={{ mb: 1, }}>
                                <Grid container alignItems="left">
                                    <Grid item xs={12} md={3} lg={3}
                                        sx={{ display: 'flex', alignItems: 'center' }}>
                                        <FormLabel><Typography variant="pnspsFormParagraph">
                                            <FormattedMessage id="myRemarks" />:
                                        </Typography></FormLabel>
                                    </Grid>

                                    <Grid item xs={12} md={9} lg={9}>
                                        <Stack direction="row">
                                            <DisplayField name="appRemarks" />
                                        </Stack>
                                    </Grid>
                                </Grid>
                            </Grid>
                        </Grid>

                        <Grid container
                            direction="row"
                            justifyContent="space-between"
                            alignItems="center"
                            width="auto"
                        >

                            <Grid item xs={12} sm={12} md={6} lg={6} sx={{ mb: 1 }}>
                                <Grid container alignItems={"center"}>
                                    <Grid item xs={12} sm={12} md={12} lg={12} sx={{ alignItems: 'center', wordBreak: 'break-word' }}>
                                        <Typography>
                                            <Typography variant="pnspsFormParagraph">
                                                <FormattedMessage id="pleaseCheckReminder" />：
                                            </Typography>
                                        </Typography>
                                    </Grid>
                                    <Grid item xs={12} sm={12} md={12} lg={12} sx={{ width: '95%', maxWidth: { xs: '70vw', sm: '72vw', md: '75vw', lg: '80vw' } }}>
                                        <FileList
                                            lang="ch"
                                            refId={params.id}
                                            refType={"proof"}
                                            dateHideable={true}
                                            disablePagination
                                            disableSelectionOnClick
                                            disableColumnMenu
                                            disableColumnSelector
                                            hideFooter
                                        />
                                    </Grid>
                                </Grid>
                            </Grid>

                            <Grid item xs={12} md={5} lg={5} sx={{ mb: 1, }}>
                                <Grid container alignItems={"center"}>
                                    {data.creditor == true ?
                                        <>
                                            <Grid item xs={12} md={12} lg={12}
                                                sx={{ display: 'flex', alignItems: 'center' }}>
                                                <Typography variant="h5" component="span" display="inline">
                                                    <FormattedMessage id="commentDeadline" />：
                                                </Typography>
                                            </Grid>
                                            <Grid item xs={12} md={12} lg={12} sx={{ mb: 4, display: 'flex', alignItems: 'center' }}>
                                                <Typography variant="h5" component="span">&emsp;
                                                    {locale === 'en' ?
                                                        `${intl.formatMessage({ id: 'before' })} ${DateUtils.dateFormat(data?.reviseDeadline, intl.formatMessage({ id: "paymentMethodDatetimeStrFormat" }))?.replace("am", "a.m.")?.replace("pm", "p.m.")} `
                                                        :
                                                        `${DateUtils.dateFormat(data?.reviseDeadline, intl.formatMessage({ id: "paymentMethodDatetimeStrFormat" }))?.replace("am", "上午")?.replace("pm", "下午")}${intl.formatMessage({ id: 'before' })}`
                                                    }
                                                </Typography>
                                            </Grid>

                                            <Grid item xs={12} md={12} lg={12}
                                                sx={{ display: 'flex', alignItems: 'center' }}>
                                                <Typography variant="h5" component="span" display="inline">
                                                    <FormattedMessage id="paymentDeadline" />：
                                                </Typography>
                                            </Grid>
                                            <Grid item xs={12} md={12} lg={12} sx={{ mb: 4, display: 'flex', alignItems: 'center' }}>
                                                <Typography variant="h5" component="span">&emsp;
                                                    {locale === 'en' ?
                                                        `${intl.formatMessage({ id: 'before' })}  ${DateUtils.dateFormat(data?.expiryDate, intl.formatMessage({ id: "paymentMethodDatetimeStrFormat" }))?.replace("am", "a.m.")?.replace("pm", "p.m.")} `
                                                        :
                                                        `${DateUtils.dateFormat(data?.expiryDate, intl.formatMessage({ id: "paymentMethodDatetimeStrFormat" }))?.replace("am", "上午")?.replace("pm", "下午")}${intl.formatMessage({ id: 'before' })}`
                                                    }
                                                </Typography>
                                            </Grid>
                                        </>
                                    :
                                        <></>
                                    }
                                    <Grid item xs={12} sm={3} md={3} lg={3}
                                        sx={{ mb: 1, display: 'flex', alignItems: 'center' }}>
                                        <Typography>
                                            <Typography variant="pnspsFormParagraph">
                                                <FormattedMessage id="payFeeFor" />:
                                            </Typography>
                                        </Typography>
                                    </Grid>
                                    <Grid item xs={12} sm={9} md={9} lg={9} sx={{ mb: 1, display: 'flex', alignItems: 'center' }}>
                                        <Typography variant="h4" component="span" style={{ color: "#0049B8", fontWeight: "bold", }}>{FormatUtils.currencyFormat(data.fee)}</Typography>
                                    </Grid>
                                    <Grid item xs={12} md={9} lg={9} sx={{ mb: 4, display: 'flex', alignItems: 'center' }}>
                                        {
                                            formik.values.groupType === "Private Bill"
                                                ?
                                                <Typography>
                                                    <Typography variant="pnspsFormParagraph">( {data.noOfPages} {intl.formatMessage({ id: data.noOfPages === 1 ? 'page' : 'pages' })} x $6,552 )</Typography>
                                                </Typography>
                                                :
                                                <Typography>
                                                    <Typography variant="pnspsFormParagraph">( {data.length} cm x {data.colCount === 2 ?
                                                        "$364 " + intl.formatMessage({ id: 'doubleCol' }) :
                                                        "$182 " + intl.formatMessage({ id: 'singleCol' })} )
                                                    </Typography>
                                                </Typography>
                                        }
                                    </Grid>
                                </Grid>
                            </Grid>
                        </Grid>
                    </Grid>
                </Grid>
            </form>
        </MainCard>
    );
};

export default ApplicationDetailCard;
