"use client" import { useState } from "react" import { Sidebar } from "@/components/dashboard/sidebar" import { useBankingData } from "@/lib/hooks" import { updateAccount, deleteAccount } from "@/lib/store-db" import { Button } from "@/components/ui/button" import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card" import { Input } from "@/components/ui/input" import { Label } from "@/components/ui/label" import { Dialog, DialogContent, DialogHeader, DialogTitle } from "@/components/ui/dialog" import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select" import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger } from "@/components/ui/dropdown-menu" import { MoreVertical, Pencil, Trash2, Building2, CreditCard, Wallet, PiggyBank, RefreshCw } from "lucide-react" import type { Account } from "@/lib/types" import { cn } from "@/lib/utils" const accountTypeIcons = { CHECKING: Wallet, SAVINGS: PiggyBank, CREDIT_CARD: CreditCard, OTHER: Building2, } const accountTypeLabels = { CHECKING: "Compte courant", SAVINGS: "Épargne", CREDIT_CARD: "Carte de crédit", OTHER: "Autre", } export default function AccountsPage() { const { data, isLoading, refresh, update } = useBankingData() const [editingAccount, setEditingAccount] = useState(null) const [isDialogOpen, setIsDialogOpen] = useState(false) const [formData, setFormData] = useState({ name: "", type: "CHECKING" as Account["type"], folderId: "folder-root", }) if (isLoading || !data) { return (
) } const formatCurrency = (amount: number) => { return new Intl.NumberFormat("fr-FR", { style: "currency", currency: "EUR", }).format(amount) } const handleEdit = (account: Account) => { setEditingAccount(account) setFormData({ name: account.name, type: account.type, folderId: account.folderId || "folder-root", }) setIsDialogOpen(true) } const handleSave = async () => { if (!editingAccount) return try { const updatedAccount = { ...editingAccount, name: formData.name, type: formData.type, folderId: formData.folderId, } await updateAccount(updatedAccount) refresh() setIsDialogOpen(false) setEditingAccount(null) } catch (error) { console.error("Error updating account:", error) alert("Erreur lors de la mise à jour du compte") } } const handleDelete = async (accountId: string) => { if (!confirm("Supprimer ce compte et toutes ses transactions ?")) return try { await deleteAccount(accountId) refresh() } catch (error) { console.error("Error deleting account:", error) alert("Erreur lors de la suppression du compte") } } const getTransactionCount = (accountId: string) => { return data.transactions.filter((t) => t.accountId === accountId).length } const totalBalance = data.accounts.reduce((sum, a) => sum + a.balance, 0) return (

Comptes

Gérez vos comptes bancaires

Solde total

= 0 ? "text-emerald-600" : "text-red-600")}> {formatCurrency(totalBalance)}

{data.accounts.length === 0 ? (

Aucun compte

Importez un fichier OFX depuis le tableau de bord pour ajouter votre premier compte.

) : (
{data.accounts.map((account) => { const Icon = accountTypeIcons[account.type] const folder = data.folders.find((f) => f.id === account.folderId) return (
{account.name}

{accountTypeLabels[account.type]}

handleEdit(account)}> Modifier handleDelete(account.id)} className="text-red-600"> Supprimer
= 0 ? "text-emerald-600" : "text-red-600", )} > {formatCurrency(account.balance)}
{getTransactionCount(account.id)} transactions {folder && {folder.name}}
{account.lastImport && (

Dernier import: {new Date(account.lastImport).toLocaleDateString("fr-FR")}

)}
) })}
)}
Modifier le compte
setFormData({ ...formData, name: e.target.value })} />
) }