import {useTranslations} from "next-intl";
import {useRouter} from "next/router";
import {useState} from "react";
import useRequest from "@/lib/app/hooks/useRequest";
import usePrint from "@/lib/app/hooks/usePrint";
import {GET_PASSENGER_BOSS} from "@/core/data/apiRoutes";
import {Button, CircularProgress} from "@mui/material";
import PrintIcon from "@mui/icons-material/Print";

const PrintFormA = () => {
    const t = useTranslations();
    const router = useRouter()
    const [loading, setLoading] = useState(false)
    const requestServer = useRequest({auth: true, pending: false, success: {notification: {show: false}}})
    const {setPrintPage, setPrintTitle} = usePrint()
    const clickHandler = () => {
        setLoading(true)
        const params = new URLSearchParams();
        params.set("start", '0');
        params.set("filters", '[]');
        params.set("sorting", '[]');
        requestServer(`${GET_PASSENGER_BOSS}?${params}`, 'get').then((response) => {
            const _chunkSize = 10
            const _data = response.data.data
            const chunk = Array.from({length: Math.ceil(_data.length / _chunkSize)}, (v, i) =>
                _data.slice(i * _chunkSize, i * _chunkSize + _chunkSize)
            );
            setPrintPage(chunk.length)
            setPrintTitle(t('PassengerBoss.print_form_a'))
            sessionStorage.setItem('form-a-print', JSON.stringify(chunk))
            router.push('/dashboard/passenger-boss/prints/form-a')
        }).catch(() => {
            setLoading(false)
        })
    }
    return (
        <Button
            color="primary"
            variant="contained"
            size="small"
            disabled={loading}
            sx={{textTransform: "unset", alignSelf: "center"}}
            startIcon={loading ? <CircularProgress size={18} color="inherit"/> : <PrintIcon/>}
            onClick={clickHandler}
        >
            {t("PassengerBoss.print_form_a")}
        </Button>
    )
}

export default PrintFormA