"use client"; import { useState, useTransition } from "react"; import { Archive, ArchiveRestore, Building2, Loader2, MoreHorizontal, Pencil, Plus, Trash2, } from "lucide-react"; import { toast } from "sonner"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { Card, CardContent } from "@/components/ui/card"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuSeparator, DropdownMenuTrigger, } from "@/components/ui/dropdown-menu"; import { archiveBankAccountAction, deleteBankAccountAction, } from "@/lib/appwrite/bank-account-actions"; import { formatTRY } from "@/lib/format"; import { cn } from "@/lib/utils"; import { BankFormSheet } from "./bank-form-sheet"; import type { BankAccountRow } from "./types"; type Props = { accounts: BankAccountRow[] }; export function BanksClient({ accounts }: Props) { const [formOpen, setFormOpen] = useState(false); const [editing, setEditing] = useState(null); const [deleting, setDeleting] = useState(null); const [busy, startTransition] = useTransition(); const active = accounts.filter((a) => !a.archived); const archived = accounts.filter((a) => a.archived); const totalBalance = active.reduce((s, a) => s + a.balance, 0); const toggleArchive = (acc: BankAccountRow) => { startTransition(async () => { const fd = new FormData(); fd.set("id", acc.id); const result = await archiveBankAccountAction(fd); if (result.ok) { toast.success(acc.archived ? "Hesap geri açıldı." : "Hesap arşivlendi."); } else { toast.error(result.error ?? "İşlem başarısız."); } }); }; const handleDelete = () => { if (!deleting) return; startTransition(async () => { const fd = new FormData(); fd.set("id", deleting.id); const result = await deleteBankAccountAction(fd); if (result.ok) { toast.success("Hesap silindi."); setDeleting(null); } else { toast.error(result.error ?? "Silme başarısız."); } }); }; return (

Toplam bakiye (aktif hesaplar)

= 0 ? "text-emerald-600 dark:text-emerald-400" : "text-red-600 dark:text-red-400", )} > {formatTRY(totalBalance)}

{active.length === 0 && archived.length === 0 ? (

Henüz banka hesabı eklenmemiş.

) : ( <>
{active.map((a) => ( { setEditing(a); setFormOpen(true); }} onArchiveToggle={() => toggleArchive(a)} onDelete={() => setDeleting(a)} busy={busy} /> ))}
{archived.length > 0 && (
Arşivlenmiş hesaplar ({archived.length})
{archived.map((a) => ( { setEditing(a); setFormOpen(true); }} onArchiveToggle={() => toggleArchive(a)} onDelete={() => setDeleting(a)} busy={busy} /> ))}
)} )} { setFormOpen(v); if (!v) setEditing(null); }} account={editing} /> !v && setDeleting(null)}> Hesabı sil {deleting?.bankName} — {deleting?.accountName} kalıcı olarak silinecek. Bağlı finans hareketi varsa silme reddedilir; o durumda arşivlemeyi tercih edin.
); } function AccountCard({ account, onEdit, onArchiveToggle, onDelete, busy, }: { account: BankAccountRow; onEdit: () => void; onArchiveToggle: () => void; onDelete: () => void; busy: boolean; }) { const positive = account.balance >= 0; return (

{account.bankName}

{account.archived && ( Arşivli )}

{account.accountName}

{account.iban && (

{account.iban.replace(/(.{4})/g, "$1 ").trim()}

)}
Düzenle {account.archived ? ( <> Arşivden çıkar ) : ( <> Arşivle )} Sil

Güncel bakiye

{formatTRY(account.balance)}

{account.balance !== account.openingBalance && (

Açılış: {formatTRY(account.openingBalance)}

)}
); }