import {
  Dialog,
  DialogContent,
  DialogFooter,
  DialogHeader,
  DialogTitle,
  DialogDescription
} from '@/components/ui/dialog';
import { Button } from '@/components/ui/button';
import { Alert, useDataGrid } from '@/components';
import { useCallback, useState } from 'react';
import { apiConfig } from '@/config/api.config';
import { toast } from 'sonner';
import { useCallApi } from '@/hooks';
import { useManageTransferFeeContext } from '../hooks/useManageTransferFeeContext';

const API_URL = apiConfig.service_transaction;

const DeleteFeeDialog = () => {
  const { showDeleteFeeDialog, handleDeleteFeeDialog, selectedTransferFee } =
    useManageTransferFeeContext();
  const { reload } = useDataGrid();
  const { DeleteData } = useCallApi();
  const [alert, setAlert] = useState({
    show: false,
    message: ''
  });

  const doDeleteTransferFee = useCallback(async () => {
    const response = await DeleteData(
      `${API_URL}/transactionfees/delete/${selectedTransferFee}/false`,
      {
        id: selectedTransferFee
      }
    );

    if (response?.status) {
      setAlert({ show: false, message: '' });
      handleDeleteFeeDialog(false, null);
      // toast.success('Success Delete Transaction Fee');
      reload();
    } else {
      // toast.error('Failed Delete Transaction Fee');
      setAlert({ show: true, message: response?.message });
    }
  }, [selectedTransferFee]);

  return (
    <Dialog open={showDeleteFeeDialog} onOpenChange={(open) => handleDeleteFeeDialog(open, null)}>
      <DialogContent className="container-fixed max-w-md flex flex-col p-5 overflow-hidden [&>button]:hidden">
        <DialogHeader className="p-0 border-0 block">
          <DialogTitle className="text-lg">Delete Transfer Type</DialogTitle>
          <DialogDescription className="text-sm">
            Are you sure you want to delete this data?
          </DialogDescription>
          <Alert variant="warning">
            <h3 className="text-lg">Are you sure?</h3>
            <span className="text-sm">You will delete this data!</span>
          </Alert>
          {alert.show && (
            <Alert variant="danger">
              <h3>{alert.message}</h3>
            </Alert>
          )}
        </DialogHeader>
        <DialogFooter className="flex justify-end items-center gap-4 mt-3">
          <Button variant={'outline'} onClick={() => handleDeleteFeeDialog(false, null)}>
            Cancel
          </Button>
          <Button variant={'destructive'} onClick={() => doDeleteTransferFee()}>
            Delete
          </Button>
        </DialogFooter>
      </DialogContent>
    </Dialog>
  );
};

export default DeleteFeeDialog;
