import { Trash } from 'iconsax-react'
import { FC, useState } from 'react'
import { useTranslation } from 'react-i18next'
import { useDeleteCategory } from '../hooks/useTicketData'
import ModalConfrim from '../../../components/ModalConfrim'
import { toast } from 'react-toastify'
import { ErrorType } from '../../../helpers/types'

interface Props {
    id: string;
}

const DeleteCategory: FC<Props> = ({ id }) => {
    const { t } = useTranslation('global')
    const [isOpen, setIsOpen] = useState<boolean>(false)
    const deleteCategory = useDeleteCategory()

    const handleDelete = () => {
        deleteCategory.mutate(id, {
            onSuccess: () => {
                setIsOpen(false)
                toast.success(t('success'))
            },
            onError: (error: ErrorType) => {
                toast.error(error.response?.data?.error.message[0])
            }
        })
    }

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

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

export default DeleteCategory 