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 PrintFormB = () => {
    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 _data = response.data.data
            setPrintPage(_data.length)
            setPrintTitle(t('PassengerBoss.print_form_b'))
            sessionStorage.setItem('form-b-print', JSON.stringify(_data))
            router.push('/dashboard/passenger-boss/prints/form-b')
        }).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_b")}
        </Button>
    )
}

export default PrintFormB