"use client"; import { useState } from "react"; import { useRouter } from "next/navigation"; import { PlusIcon } from "lucide-react"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger, } from "@/components/ui/dialog"; import { createBankAccount } from "@/app/(frontend)/logistics/banking/actions"; interface CreateAccountDialogProps { isManager: boolean; factions: { id: number; name: string }[]; open?: boolean; onOpenChange?: (open: boolean) => void; } type AccountType = "treasury" | "faction" | "personal"; export function CreateAccountDialog({ isManager, factions, open: externalOpen, onOpenChange: externalOnOpenChange, }: CreateAccountDialogProps) { const router = useRouter(); const [internalOpen, setInternalOpen] = useState(false); const open = externalOpen ?? internalOpen; const setOpen = externalOnOpenChange ?? setInternalOpen; const [name, setName] = useState(""); const [accountType, setAccountType] = useState("personal"); const [ownerFactionId, setOwnerFactionId] = useState(""); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); async function handleSubmit() { if (!name.trim()) { setError("Account name is required."); return; } if (accountType === "faction" && !ownerFactionId) { setError("Select an owner faction."); return; } setLoading(true); setError(null); const res = await createBankAccount({ name: name.trim(), accountType, ownerFactionId: accountType === "faction" ? Number(ownerFactionId) : undefined, }); if (res.success) { setOpen(false); setName(""); setAccountType("personal"); setOwnerFactionId(""); router.refresh(); } else { setError(res.error ?? "Failed to create account."); setLoading(false); } } return ( Create Account Create a new bank account for the unit, a faction, or yourself.
setName(e.target.value)} placeholder="e.g., Unit Treasury" />
{accountType === "faction" && (
)} {accountType === "personal" && !isManager && (

This will create a personal account for you.

)} {error &&

{error}

}
); }