import { Trash } from 'iconsax-react'
import { FC, useState } from 'react'
import { useDeleteCompany } from '../hooks/useCompanyData'
import ModalConfrim from '../../../components/ModalConfrim'
import { ErrorType } from '../../../helpers/types'
import { toast } from 'react-toastify'

type Props = {
    id: string,
    refetch: () => void,
}

const DeleteCompany: FC<Props> = ({ id, refetch }) => {

    const [isOpen, setIsOpen] = useState<boolean>(false)
    const deleteCompany = useDeleteCompany()

    const handleConfrim = () => {
        deleteCompany.mutate(id, {
            onSuccess: () => {
                refetch()
            },
            onError: (error: ErrorType) => {
                toast.error(error.response?.data?.error.message[0])
            }
        })
    }

    return (
        <div>
            <Trash onClick={() => setIsOpen(true)} size={20} color='#888' />

            <ModalConfrim
                close={() => setIsOpen(false)}
                isOpen={isOpen}
                onConfrim={handleConfrim}
                isLoading={deleteCompany.isPending}
            />
        </div>
    )
}

export default DeleteCompany