import { useState } from "react"; import { trpc } from "@/lib/trpc"; import { Button } from "@/components/ui/button"; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"; import { Input } from "@/components/ui/input"; import { Badge } from "@/components/ui/badge"; import { toast } from "sonner"; import { Loader2, Plus, Trash2, List, Building2, Calculator, Sparkles } from "lucide-react"; import DashboardLayout from "@/components/DashboardLayout"; import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, } from "@/components/ui/alert-dialog"; export default function ListsAdmin() { const { data: departments, isLoading: loadingDepartments } = trpc.departments.getByUser.useQuery(); const { data: allocations, isLoading: loadingAllocations } = trpc.accountingAllocations.getByUser.useQuery(); const createDepartmentMutation = trpc.departments.create.useMutation(); const deleteDepartmentMutation = trpc.departments.delete.useMutation(); const createAllocationMutation = trpc.accountingAllocations.create.useMutation(); const deleteAllocationMutation = trpc.accountingAllocations.delete.useMutation(); const utils = trpc.useUtils(); const [newDepartment, setNewDepartment] = useState(""); const [newAllocation, setNewAllocation] = useState(""); const [deleteDialogOpen, setDeleteDialogOpen] = useState(false); const [itemToDelete, setItemToDelete] = useState<{ type: "department" | "allocation"; id: number; name: string } | null>(null); const handleCreateDepartment = async () => { if (!newDepartment.trim()) { toast.error("Le nom ne peut pas être vide"); return; } try { await createDepartmentMutation.mutateAsync({ name: newDepartment.trim() }); await utils.departments.getByUser.invalidate(); setNewDepartment(""); toast.success(`Service "${newDepartment}" ajouté avec succès`); } catch (error: any) { toast.error(error.message || "Impossible d'ajouter le service"); } }; const handleCreateAllocation = async () => { if (!newAllocation.trim()) { toast.error("Le nom ne peut pas être vide"); return; } try { await createAllocationMutation.mutateAsync({ name: newAllocation.trim() }); await utils.accountingAllocations.getByUser.invalidate(); setNewAllocation(""); toast.success(`Ventilation "${newAllocation}" ajoutée avec succès`); } catch (error: any) { toast.error(error.message || "Impossible d'ajouter la ventilation"); } }; const confirmDelete = (type: "department" | "allocation", id: number, name: string) => { setItemToDelete({ type, id, name }); setDeleteDialogOpen(true); }; const handleDelete = async () => { if (!itemToDelete) return; try { if (itemToDelete.type === "department") { await deleteDepartmentMutation.mutateAsync({ id: itemToDelete.id }); await utils.departments.getByUser.invalidate(); } else { await deleteAllocationMutation.mutateAsync({ id: itemToDelete.id }); await utils.accountingAllocations.getByUser.invalidate(); } toast.success(`"${itemToDelete.name}" supprimé avec succès`); } catch (error: any) { toast.error(error.message || "Impossible de supprimer"); } finally { setDeleteDialogOpen(false); setItemToDelete(null); } }; if (loadingDepartments || loadingAllocations) { return (

Chargement des listes...

); } const departmentCount = departments?.length || 0; const allocationCount = allocations?.length || 0; return (
{/* Header */}

Administration des listes

Gérez les listes de valeurs pour les champs métier des factures

{/* Statistics */}

Services

{departmentCount}

Actif{departmentCount > 1 ? 's' : ''}

Ventilations

{allocationCount}

Active{allocationCount > 1 ? 's' : ''}
{/* Department List */}
Service concerné Gérez la liste des services disponibles pour la classification des factures
setNewDepartment(e.target.value)} onKeyDown={(e) => { if (e.key === "Enter") { handleCreateDepartment(); } }} className="h-11" />
{departments && departments.length > 0 ? ( departments.map((dept) => (
{dept.name}
)) ) : (

Aucun service configuré

Ajoutez-en un pour commencer

)}
{/* Accounting Allocation List */}
Ventilation comptable Gérez la liste des ventilations comptables disponibles pour la classification des factures
setNewAllocation(e.target.value)} onKeyDown={(e) => { if (e.key === "Enter") { handleCreateAllocation(); } }} className="h-11" />
{allocations && allocations.length > 0 ? ( allocations.map((alloc) => (
{alloc.name}
)) ) : (

Aucune ventilation configurée

Ajoutez-en une pour commencer

)}
{/* Delete Confirmation Dialog */} Confirmer la suppression Êtes-vous sûr de vouloir supprimer "{itemToDelete?.name}" ?
Cette action est irréversible.
Annuler Supprimer
); }