import { FC, useState } from 'react'
import Button from '../../../components/Button'
import { useTranslation } from 'react-i18next'
import { useRejectTransfer } from '../hooks/usePaymentData'
import ModalConfrim from '../../../components/ModalConfrim'
import { toast } from 'react-toastify'
import { ErrorType } from '../../../helpers/types'

type Props = {
    id: string
}

const Reject: FC<Props> = (props: Props) => {

    const { t } = useTranslation('global')
    const [openConfrimModal, setOpenConfrimModal] = useState<boolean>(false)
    const rejectTransfer = useRejectTransfer()

    const handleRejectTransfer = (text?: string) => {
        if (text && text.length > 0) {
            rejectTransfer.mutate({ id: props.id, comment: text }, {
                onSuccess: () => {
                    toast.success(t('success'))
                    setOpenConfrimModal(false)
                    window.location.reload()
                },
                onError(error: ErrorType) {
                    toast.error(error?.response?.data?.error?.message[0])
                },
            })
        } else {
            toast.error(t('payment.reject_comment_error'))
        }
    }

    return (
        <div>
            <Button
                label={t('payment.reject')}
                className='px-4 w-fit bg-red-50 text-red-500 font-semibold'
                onClick={() => setOpenConfrimModal(true)}
            />

            <ModalConfrim
                isOpen={openConfrimModal}
                close={() => setOpenConfrimModal(false)}
                onConfrim={handleRejectTransfer}
                isLoading={rejectTransfer.isPending}
                label={t('payment.accept_confrim')}
                isHasDescription
            />
        </div>
    )
}

export default Reject