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

  const handlePost = async () => {
    setPostLoading(true)
    try {
      const result = await sendObjectFunction(objectData._id, 'Payment', 'post')
      if (result) {
        message.success('Payment posted successfully')
        onOk(result)
      }
    } catch (error) {
      console.error('Error posting payment:', error)
    } finally {
      setPostLoading(false)
    }
  }

  return (
    <MessageDialogView
      title={'Are you sure you want to post this payment?'}
      description={`Posting payment ${objectData?.name || objectData?._reference || objectData?._id} will set it to posted status.`}
      onOk={handlePost}
      okText='Post'
      okLoading={postLoading}
    />
  )
}

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

export default PostPayment

