"use client"
import { useState } from "react"
import { Sidebar } from "@/components/dashboard/sidebar"
import { useBankingData } from "@/lib/hooks"
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 {
Plus,
MoreVertical,
Pencil,
Trash2,
Folder,
FolderOpen,
ChevronRight,
ChevronDown,
Building2,
RefreshCw,
} from "lucide-react"
import { addFolder, updateFolder, deleteFolder, updateAccount } from "@/lib/store-db"
import type { Folder as FolderType, Account } from "@/lib/types"
import { cn } from "@/lib/utils"
const folderColors = [
{ value: "#6366f1", label: "Indigo" },
{ value: "#22c55e", label: "Vert" },
{ value: "#f59e0b", label: "Orange" },
{ value: "#ec4899", label: "Rose" },
{ value: "#3b82f6", label: "Bleu" },
{ value: "#ef4444", label: "Rouge" },
]
interface FolderTreeItemProps {
folder: FolderType
accounts: Account[]
allFolders: FolderType[]
level: number
onEdit: (folder: FolderType) => void
onDelete: (folderId: string) => void
onEditAccount: (account: Account) => void
formatCurrency: (amount: number) => string
}
function FolderTreeItem({
folder,
accounts,
allFolders,
level,
onEdit,
onDelete,
onEditAccount,
formatCurrency,
}: FolderTreeItemProps) {
const [isExpanded, setIsExpanded] = useState(true)
// Pour le dossier "Mes Comptes" (folder-root), inclure aussi les comptes sans dossier
const folderAccounts = accounts.filter((a) =>
a.folderId === folder.id || (folder.id === "folder-root" && a.folderId === null)
)
const childFolders = allFolders.filter((f) => f.parentId === folder.id)
const hasChildren = childFolders.length > 0 || folderAccounts.length > 0
const folderTotal = folderAccounts.reduce((sum, a) => sum + a.balance, 0)
return (
0 && "ml-6")}>
{isExpanded ? (
) : (
)}
{folder.name}
{folderAccounts.length > 0 && (
= 0 ? "text-emerald-600" : "text-red-600")}
>
{formatCurrency(folderTotal)}
)}
onEdit(folder)}>
Modifier
{folder.id !== "folder-root" && (
onDelete(folder.id)} className="text-red-600">
Supprimer
)}
{isExpanded && (
{folderAccounts.map((account) => (
{account.name}
= 0 ? "text-emerald-600" : "text-red-600")}>
{formatCurrency(account.balance)}
))}
{childFolders.map((child) => (
))}
)}
)
}
const accountTypeLabels = {
CHECKING: "Compte courant",
SAVINGS: "Épargne",
CREDIT_CARD: "Carte de crédit",
OTHER: "Autre",
}
export default function FoldersPage() {
const { data, isLoading, refresh } = useBankingData()
const [isDialogOpen, setIsDialogOpen] = useState(false)
const [editingFolder, setEditingFolder] = useState(null)
const [formData, setFormData] = useState({
name: "",
parentId: "folder-root" as string | null,
color: "#6366f1",
})
// Account editing state
const [isAccountDialogOpen, setIsAccountDialogOpen] = useState(false)
const [editingAccount, setEditingAccount] = useState(null)
const [accountFormData, setAccountFormData] = 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 rootFolders = data.folders.filter((f) => f.parentId === null)
const handleNewFolder = () => {
setEditingFolder(null)
setFormData({ name: "", parentId: "folder-root", color: "#6366f1" })
setIsDialogOpen(true)
}
const handleEdit = (folder: FolderType) => {
setEditingFolder(folder)
setFormData({
name: folder.name,
parentId: folder.parentId || "folder-root",
color: folder.color,
})
setIsDialogOpen(true)
}
const handleSave = async () => {
const parentId = formData.parentId === "folder-root" ? null : formData.parentId
try {
if (editingFolder) {
await updateFolder({
...editingFolder,
name: formData.name,
parentId,
color: formData.color,
})
} else {
await addFolder({
name: formData.name,
parentId,
color: formData.color,
icon: "folder",
})
}
refresh()
setIsDialogOpen(false)
} catch (error) {
console.error("Error saving folder:", error)
alert("Erreur lors de la sauvegarde du dossier")
}
}
const handleDelete = async (folderId: string) => {
if (!confirm("Supprimer ce dossier ? Les comptes seront déplacés à la racine.")) return
try {
await deleteFolder(folderId)
refresh()
} catch (error) {
console.error("Error deleting folder:", error)
alert("Erreur lors de la suppression du dossier")
}
}
const handleEditAccount = (account: Account) => {
setEditingAccount(account)
setAccountFormData({
name: account.name,
type: account.type,
folderId: account.folderId || "folder-root",
})
setIsAccountDialogOpen(true)
}
const handleSaveAccount = async () => {
if (!editingAccount) return
try {
await updateAccount({
...editingAccount,
name: accountFormData.name,
type: accountFormData.type,
folderId: accountFormData.folderId === "folder-root" ? null : accountFormData.folderId,
})
refresh()
setIsAccountDialogOpen(false)
setEditingAccount(null)
} catch (error) {
console.error("Error updating account:", error)
alert("Erreur lors de la mise à jour du compte")
}
}
return (
Organisation
Organisez vos comptes en dossiers
Arborescence des comptes
{rootFolders.map((folder) => (
))}
)
}