import {
    AlertDialog,
    AlertDialogCancel,
    AlertDialogContent,
    AlertDialogDescription,
    AlertDialogFooter,
    AlertDialogHeader,
    AlertDialogTitle,
    AlertDialogTrigger,
} from '@/components/ui/alert-dialog';
import { Withdraw } from '@/types';
import { router } from '@inertiajs/react';
import { CheckCircle, CircleAlertIcon, XCircle } from 'lucide-react';
import { useState } from 'react';
import { Button } from '../ui/button';

export default function WithdrawAction({ withdraw }: { withdraw: Withdraw }) {
    
    const [openApproveAlert, setOpenApproveAlert] = useState(false);
    const [openRejectAlert, setOpenRejectAlert] = useState(false);
    const [loading, setLoading] = useState(false);

   
    const handleApproveWithdraw = () => {
        router.patch(
            route('withdraw.approve', withdraw.id),
            {},
            {
                onStart: () => setLoading(true),
                onSuccess: () => {
                    setOpenApproveAlert(false);
                },
                onFinish: () => setLoading(false),
            },
        );
    };
    return (
        <div className="flex items-center gap-2">
            {withdraw.status === 'pending' && (
                <>
                    <AlertDialog open={openApproveAlert} onOpenChange={setOpenApproveAlert}>
                        <AlertDialogTrigger asChild>
                            <Button variant="outline" size="sm" className="border-green-200 bg-transparent text-xs text-green-600 hover:bg-green-50">
                                <CheckCircle className="size-3" />
                                Approve
                            </Button>
                        </AlertDialogTrigger>
                        <AlertDialogContent>
                            <div className="flex flex-col gap-2 max-sm:items-center sm:flex-row sm:gap-4">
                                <div className="flex size-9 shrink-0 items-center justify-center rounded-full border" aria-hidden="true">
                                    <CircleAlertIcon className="opacity-80" size={16} />
                                </div>
                                <AlertDialogHeader>
                                    <AlertDialogTitle>Are you sure?</AlertDialogTitle>
                                    <AlertDialogDescription>
                                        Are you sure you want to approve this withdrawal? After approval this action can't be reversed.
                                    </AlertDialogDescription>
                                </AlertDialogHeader>
                            </div>
                            <AlertDialogFooter>
                                <AlertDialogCancel disabled={loading}>Cancel</AlertDialogCancel>
                                <Button disabled={loading} onClick={handleApproveWithdraw}>
                                    Confirm
                                </Button>
                            </AlertDialogFooter>
                        </AlertDialogContent>
                    </AlertDialog>

                    <Button disabled={loading} variant="outline" size="sm" className="border-red-200 bg-transparent text-red-600 hover:bg-red-50">
                        <XCircle className="mr-2 h-4 w-4" />
                        Reject
                    </Button>
                </>
            )}
        </div>
    );
}
