import {Button, CircularProgress} from "@mui/material";
import {useTranslations} from "next-intl";
import {useState} from "react";
import useRequest from "@/lib/app/hooks/useRequest";
import DescriptionIcon from '@mui/icons-material/Description';

const PrintHistory = ({attachment}) => {
    const t = useTranslations();
    const [loading, setLoading] = useState(false)
    const requestServer = useRequest({auth: true, pending: false, success: {notification: {show: false}}})

    const clickHandler = (attachment) => {
        const anchor = document.createElement('a');
        anchor.href = attachment;
        anchor.setAttribute('download', '');
        document.body.appendChild(anchor);
        anchor.click();
        document.body.removeChild(anchor);
    }


    return (<Button
        color="primary"
        variant="contained"
        size="small"
        disabled={loading}
        sx={{textTransform: "unset", alignSelf: "center", mx: .25}}
        startIcon={loading ? <CircularProgress size={18} color="inherit"/> : <DescriptionIcon/>}
        onClick={() => clickHandler(attachment.file)}
    >
        {attachment.title}
    </Button>)
}

export default PrintHistory