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

const PrintReport = () => {
    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_MACHINARY_OFFICE}?${params}`, 'get').then((response) => {
            const _data = response.data.data
            setPrintPage(_data.length)
            setPrintTitle(t('MachinaryOffice.print_report'))
            sessionStorage.setItem('report-print', JSON.stringify(_data))
            router.push('/dashboard/machinery-expert/prints/report')
        }).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("MachinaryOffice.print_report")}
        </Button>
    )
}

export default PrintReport