import { Form, Head, Link } from '@inertiajs/react';
import { ArrowLeft, Printer } from 'lucide-react';
import WithdrawalController from '@/actions/App/Http/Controllers/WithdrawalController';
import MemberPage from '@/components/member/member-page';
import WithdrawalDetails from '@/components/member/withdrawal-details';
import type { WithdrawalItem } from '@/components/member/withdrawal-status';
import { Button } from '@/components/ui/button';
import { index } from '@/routes/withdrawals';

export default function WithdrawalShow({
    withdrawal,
    canCancel,
}: {
    withdrawal: WithdrawalItem & {
        memberName: string;
        memberId: string;
        reviewedBy: string | null;
        paidBy: string | null;
    };
    canCancel: boolean;
}) {
    return (
        <>
            <Head title={`Withdrawal ${withdrawal.reference}`} />

            <MemberPage
                title={
                    withdrawal.status === 'paid'
                        ? 'Withdrawal receipt'
                        : 'Withdrawal'
                }
                description={`${withdrawal.memberName} · ${withdrawal.memberId}`}
                actions={
                    <div className="print-hidden flex gap-2">
                        <Link
                            href={index()}
                            className="inline-flex items-center gap-1.5 rounded-full border border-surface-200 bg-white px-3.5 py-1.5 text-sm font-semibold text-ink-700 hover:border-brand/40"
                        >
                            <ArrowLeft className="size-4" />
                            All withdrawals
                        </Link>
                        {withdrawal.status === 'paid' && (
                            <button
                                type="button"
                                onClick={() => window.print()}
                                className="inline-flex items-center gap-1.5 rounded-full bg-brand px-3.5 py-1.5 text-sm font-semibold text-white hover:bg-brand-hover"
                            >
                                <Printer className="size-4" />
                                Print / save PDF
                            </button>
                        )}
                    </div>
                }
            >
                <div className="grid max-w-2xl gap-4">
                    <WithdrawalDetails
                        withdrawal={withdrawal}
                        reviewedBy={withdrawal.reviewedBy}
                        paidBy={withdrawal.paidBy}
                    />

                    {canCancel && (
                        <Form
                            {...WithdrawalController.cancel.form(withdrawal.id)}
                            options={{ preserveScroll: true }}
                            onBefore={() =>
                                window.confirm(
                                    'Cancel this withdrawal? The money goes back to your wallet.',
                                )
                            }
                            className="print-hidden"
                        >
                            {({ processing }) => (
                                <Button variant="outline" disabled={processing}>
                                    Cancel request
                                </Button>
                            )}
                        </Form>
                    )}
                </div>
            </MemberPage>
        </>
    );
}

WithdrawalShow.layout = {
    breadcrumbs: [{ title: 'Withdrawals', href: index() }],
};
