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 DeclinePayment = ({ onOk, objectData }) => {
  const [declineLoading, setDeclineLoading] = useState(false)
  const { sendObjectFunction } = useContext(ApiServerContext)

  const handleDecline = async () => {
    setDeclineLoading(true)
    try {
      const result = await sendObjectFunction(
        objectData._id,
        'Payment',
        'decline'
      )
      if (result) {
        message.success('Payment declined successfully')
        onOk(result)
      }
    } catch (error) {
      console.error('Error declining payment:', error)
    } finally {
      setDeclineLoading(false)
    }
  }

  return (
    <MessageDialogView
      title={'Are you sure you want to decline this payment?'}
      description={`Declining payment ${objectData?.name || objectData?._reference || objectData?._id} will update its status to declined.`}
      onOk={handleDecline}
      okText='Decline'
      okLoading={declineLoading}
    />
  )
}

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

export default DeclinePayment
