import {useTranslations} from "next-intl";
import {useEffect, useState} from "react";
import {Stack, Typography} from "@mui/material";
import CenterLayout from "@/layouts/CenterLayout";
import Content from "@/components/dashboard/passenger-boss/Prints/form-b/Content";

const FormBComponent = () => {
    const t = useTranslations()
    const [data, setData] = useState(null)
    const [loading, setLoading] = useState(true)
    const [isNotData, setIsNotData] = useState(true)

    useEffect(() => {
        if (sessionStorage.getItem('form-b-print') === null) {
            setLoading(false)
            setIsNotData(true)
            sessionStorage.setItem('form-b-print', 'seen')
        } else if (sessionStorage.getItem('form-b-print') === 'seen') {
            setLoading(false)
            setData(null)
            setIsNotData(true)
        } else if (data) {
            setLoading(false)
            sessionStorage.setItem('form-b-print', 'seen')
            setIsNotData(false)
        } else {
            setLoading(true)
            setData(JSON.parse(sessionStorage.getItem('form-b-print')))
            setIsNotData(true)
        }
    }, [data]);

    return (
        <Stack sx={{bgcolor: '#efefef', width: isNotData ? '100%' : 'auto', height: isNotData ? '100%' : 'auto'}}>
            {loading ? (
                <CenterLayout>
                    <Stack>
                        <Typography variant={'h5'}>
                            {t('print_loading')}
                        </Typography>
                    </Stack>
                </CenterLayout>
            ) : !data ? (
                <CenterLayout>
                    <Typography variant={'h5'}>
                        {t('data_not_found')}
                    </Typography>
                </CenterLayout>
            ) : !data.length ? (
                <CenterLayout>
                    <Typography variant={'h5'}>
                        {t('data_not_found')}
                    </Typography>
                </CenterLayout>
            ) : data.map(item => <Content key={item.id} data={item}/>)}
        </Stack>
    )
}

export default FormBComponent