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

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

  return (
    <MessageDialogView
      title={'Are you sure you want to post this product stock?'}
      description={`Posting product stock ${objectData?.name || objectData?._reference || objectData?._id} will finalize it and make it read-only.`}
      onOk={handlePost}
      okText='Post'
      okLoading={postLoading}
    />
  )
}

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

export default PostProductStock
