import { useState, useContext } from 'react'
import PropTypes from 'prop-types'
import { ApiServerContext } from '../../context/ApiServerContext'
import { message } from 'antd'
import MessageDialogView from '../../common/MessageDialogView.jsx'

const AcknowledgeInvoice = ({ onOk, objectData }) => {
  const [acknowledgeLoading, setAcknowledgeLoading] = useState(false)
  const { sendObjectFunction } = useContext(ApiServerContext)

  const handleAcknowledge = async () => {
    setAcknowledgeLoading(true)
    try {
      const result = await sendObjectFunction(
        objectData._id,
        'Invoice',
        'acknowledge'
      )
      if (result) {
        message.success('Invoice acknowledged successfully')
        onOk(result)
      }
    } catch (error) {
      console.error('Error acknowledging invoice:', error)
    } finally {
      setAcknowledgeLoading(false)
    }
  }

  return (
    <MessageDialogView
      title={'Are you sure you want to acknowledge this invoice?'}
      description={`Acknowledging invoice ${objectData?.name || objectData?._reference || objectData?._id} will update its status to acknowledged.`}
      onOk={handleAcknowledge}
      okText='Acknowledge'
      okLoading={acknowledgeLoading}
    />
  )
}

AcknowledgeInvoice.propTypes = {
  onOk: PropTypes.func.isRequired,
  objectData: PropTypes.object
}

export default AcknowledgeInvoice

