"use client"; import { useState, useMemo } from "react"; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter, } from "@/shared/components/ui/dialog"; import { Button } from "@/shared/components/ui/button"; import { Input } from "@/shared/components/ui/input"; import { Label } from "@/shared/components/ui/label"; import { Badge } from "@/shared/components/ui/badge"; import { DEADLINE_CATALOG, CATEGORY_LABELS, } from "../services/deadline-catalog"; import { computeDueDate } from "../services/working-days"; import type { DeadlineCategory, DeadlineTypeDef } from "../types"; interface DeadlineAddDialogProps { open: boolean; onOpenChange: (open: boolean) => void; entryDate: string; onAdd: (typeId: string, startDate: string) => void; } type Step = "category" | "type" | "date"; const CATEGORIES: DeadlineCategory[] = [ "certificat", "avize", "completari", "analiza", "autorizare", "publicitate", ]; export function DeadlineAddDialog({ open, onOpenChange, entryDate, onAdd, }: DeadlineAddDialogProps) { const [step, setStep] = useState("category"); const [selectedCategory, setSelectedCategory] = useState(null); const [selectedType, setSelectedType] = useState( null, ); const [startDate, setStartDate] = useState(entryDate); const typesForCategory = useMemo(() => { if (!selectedCategory) return []; return DEADLINE_CATALOG.filter((d) => d.category === selectedCategory); }, [selectedCategory]); const dueDatePreview = useMemo(() => { if (!selectedType || !startDate) return null; const start = new Date(startDate); start.setHours(0, 0, 0, 0); const due = computeDueDate( start, selectedType.days, selectedType.dayType, selectedType.isBackwardDeadline, ); return due.toLocaleDateString("ro-RO", { day: "2-digit", month: "2-digit", year: "numeric", }); }, [selectedType, startDate]); const handleClose = () => { setStep("category"); setSelectedCategory(null); setSelectedType(null); setStartDate(entryDate); onOpenChange(false); }; const handleCategorySelect = (cat: DeadlineCategory) => { setSelectedCategory(cat); setStep("type"); }; const handleTypeSelect = (typ: DeadlineTypeDef) => { setSelectedType(typ); if (!typ.requiresCustomStartDate) { setStartDate(entryDate); } setStep("date"); }; const handleBack = () => { if (step === "type") { setStep("category"); setSelectedCategory(null); } else if (step === "date") { setStep("type"); setSelectedType(null); } }; const handleConfirm = () => { if (!selectedType || !startDate) return; onAdd(selectedType.id, startDate); handleClose(); }; return ( { if (!o) handleClose(); }} > {step === "category" && "Adaugă termen legal — Categorie"} {step === "type" && `Adaugă termen legal — ${selectedCategory ? CATEGORY_LABELS[selectedCategory] : ""}`} {step === "date" && `Adaugă termen legal — ${selectedType?.label ?? ""}`} {step === "category" && (
{CATEGORIES.map((cat) => ( ))}
)} {step === "type" && (
{typesForCategory.map((typ) => ( ))}
)} {step === "date" && selectedType && (
{selectedType.startDateHint && (

{selectedType.startDateHint}

)} setStartDate(e.target.value)} className="mt-1" />
{dueDatePreview && (

{selectedType.isBackwardDeadline ? "Termen limită depunere" : "Termen limită calculat"}

{dueDatePreview}

{selectedType.days}{" "} {selectedType.dayType === "working" ? "zile lucrătoare" : "zile calendaristice"} {selectedType.isBackwardDeadline ? " ÎNAINTE" : " de la data start"}

{selectedType.legalReference && (

Ref: {selectedType.legalReference}

)}
)}
)} {step !== "category" && ( )} {step === "category" && ( )} {step === "date" && ( )}
); }