import { Progress, Button } from "antd"
import { download, notify } from "../../components/factory"

export const getReportFile = async (fileInfo) => {
  try {
    await download(`/api/well/${fileInfo.idWell}/files/${fileInfo.id}`)
  } catch (error) {
    notify(`Не удалось скачать отчет ${fileInfo.name} по скважине (${fileInfo.idWell})`, 'error')
    console.log(error)
  }
}

export const ReportCreationNotify = ({progressData}) => {
  progressData = progressData ?? {progress: 0.0, operation: 'Создание отчета'}

  let downloadButton = null
  if (progressData.file)
    downloadButton = <Button      
      type="link"
      onClick={_ => {getReportFile(progressData.file)}}
      download={progressData.file.name}>
        {progressData.file.name}
      </Button>
  
  const progressText = `${progressData.operation} стр ${progressData.currentPage} из ${progressData.totalPages}`
  return (
  <>
      <Progress percent={ progressData.progress } />
      <br/>
      <span>{progressText}</span>
      {downloadButton}
  </>
  )
}