import Layout from '@/components/Layout';
import {
    Table,
    TableBody,
    TableCell,
    TableFooter,
    TableHead,
    TableHeader,
    TableRow,
} from '@/components/ui/table';
import { Card, CardContent } from '@/components/ui/card';
import { Button } from '@/components/ui/button';
import { EllipsisVertical, FolderClosed } from 'lucide-react';
import {
    Empty,
    EmptyDescription,
    EmptyHeader,
    EmptyMedia,
    EmptyTitle,
} from '@/components/ui/empty';
import { router } from '@inertiajs/react';
import AppPagination from '@/components/AppPagination';
import {
    DropdownMenu,
    DropdownMenuContent,
    DropdownMenuItem,
    DropdownMenuSeparator,
    DropdownMenuTrigger,
} from '@/components/ui/dropdown-menu';
import PageHeader from '@/components/PageHeader';
import { Badge } from '@/components/ui/badge';

interface TableData {
    id: any;
    bank: {
        label: string;
        bank_name: string;
    };
    date: string;
    note?: string;
    amount: number | 0;
    type: 'in' | 'out' | 'other';
    manager?: { name: string; email: string };
}
interface LinkData {
    url: string | null;
    label: string;
    active: boolean;
}
interface PageProps {
    data: {
        data: TableData[];
        current_page: number;
        last_page: number;
        links: LinkData[];
        from: number | null;
        to: number | null;
        total: number;
    };
    totalBalance: number | 0;
    totalDeposit: number | 0;
    totalWithdrawal: number | 0;
    totalTransaction: number | 0;
}

export default function bankTransaction({
    data: initData,
    totalBalance,
    totalDeposit,
    totalWithdrawal,
    totalTransaction,
}: PageProps) {
    const data = [
        {
            name: 'Current Balance',
            stat: totalBalance?.toLocaleString('en-BD', {
                minimumFractionDigits: 1,
                maximumFractionDigits: 1,
            }),
        },
        {
            name: 'Total Deposit',
            stat: `+${totalDeposit?.toLocaleString('en-BD', {
                minimumFractionDigits: 1,
                maximumFractionDigits: 1,
            })}`,
        },
        {
            name: 'Total Withdrawal',
            stat: `-${totalWithdrawal?.toLocaleString('en-BD', {
                minimumFractionDigits: 1,
                maximumFractionDigits: 1,
            })}`,
        },
        {
            name: 'Total Transactions',
            stat: totalTransaction,
        },
    ];
    return (
        <Layout title="Bank Transactions">
            <PageHeader
                title="All Transactions"
                subtitle="Track and manage all your business transactions."
            >
                <Button
                    variant="destructive"
                    onClick={() => router.get(route('bank.index'))}
                >
                    Go back
                </Button>
            </PageHeader>

            <div className="mb-5 flex w-full items-center justify-center">
                <dl className="grid w-full grid-cols-1 gap-6 sm:grid-cols-2 lg:grid-cols-4">
                    {data.map((item) => (
                        <Card className="p-6 py-4 shadow-2xs" key={item.name}>
                            <CardContent className="p-0">
                                <dt className="text-sm font-medium text-muted-foreground">
                                    {item.name}
                                </dt>
                                <dd className="mt-2 flex items-baseline space-x-2.5">
                                    <span className="text-3xl font-semibold text-foreground tabular-nums">
                                        {item.stat}
                                    </span>
                                </dd>
                            </CardContent>
                        </Card>
                    ))}
                </dl>
            </div>

            <Card>
                <CardContent>
                    {initData?.data?.length > 0 ? (
                        <Table>
                            <TableHeader>
                                <TableRow>
                                    <TableHead className="w-25">ID</TableHead>
                                    <TableHead>Date</TableHead>
                                    <TableHead>Bank</TableHead>
                                    <TableHead>By</TableHead>
                                    <TableHead>Note</TableHead>
                                    <TableHead>Type</TableHead>
                                    <TableHead className="text-right">
                                        Amount
                                    </TableHead>
                                </TableRow>
                            </TableHeader>
                            <TableBody>
                                {initData?.data?.map((items) => (
                                    <TableRow key={items.id}>
                                        <TableCell className="font-medium">
                                            {items.id}
                                        </TableCell>
                                        <TableCell>{items.date}</TableCell>
                                        <TableCell>
                                            <p className="text-base font-medium">
                                                {items?.bank?.label}
                                            </p>
                                            <p className="text-sm">
                                                {items?.bank?.bank_name}
                                            </p>
                                        </TableCell>
                                        <TableCell>
                                            <p className="text-base font-medium">
                                                {items?.manager?.name}
                                            </p>
                                            <p className="text-sm">
                                                {items?.manager?.email}
                                            </p>
                                        </TableCell>
                                        <TableCell>
                                            {items?.note || '--'}
                                        </TableCell>
                                        <TableCell>
                                            <Badge
                                                variant={
                                                    items.type == 'in'
                                                        ? 'default'
                                                        : 'destructive'
                                                }
                                                className="capitalize"
                                            >
                                                {items.type == 'in'
                                                    ? 'Deposit'
                                                    : items.type == 'other'
                                                      ? 'Expense'
                                                      : 'Withdrawal'}
                                            </Badge>
                                        </TableCell>
                                        <TableCell
                                            className={`text-right font-bold ${items.type == 'in' ? 'text-green-700' : 'text-destructive'}`}
                                        >
                                            {items.type == 'in' ? '+' : '-'}{' '}
                                            {items.amount.toLocaleString(
                                                'en-BD',
                                                {
                                                    minimumFractionDigits: 1,
                                                    maximumFractionDigits: 1,
                                                },
                                            )}
                                        </TableCell>
                                    </TableRow>
                                ))}
                            </TableBody>
                        </Table>
                    ) : (
                        <Empty>
                            <EmptyHeader>
                                <EmptyMedia variant="icon">
                                    <FolderClosed />
                                </EmptyMedia>
                                <EmptyTitle>No Transactions Yet</EmptyTitle>
                                <EmptyDescription>
                                    You haven't recorded any transactions yet.
                                    Get started by adding your first
                                    transaction.
                                </EmptyDescription>
                            </EmptyHeader>
                        </Empty>
                    )}

                    <AppPagination data={initData} />
                </CardContent>
            </Card>
        </Layout>
    );
}
