"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 { Info, Building2, Calendar } from "lucide-react"; import { CATEGORY_LABELS, getCategoriesForDirection, getSelectableDeadlines, } from "../services/deadline-catalog"; import { computeDueDate } from "../services/working-days"; import type { DeadlineCategory, DeadlineTypeDef, RegistryDirection, } from "../types"; interface DeadlineAddDialogProps { open: boolean; onOpenChange: (open: boolean) => void; entryDate: string; direction: RegistryDirection; /** Document type — filters which deadline categories are available */ documentType?: string; /** Callback: typeId, startDate, options (CJ toggle etc.) */ onAdd: ( typeId: string, startDate: string, options?: { isCJ?: boolean }, ) => void; } type Step = "category" | "type" | "date"; export function DeadlineAddDialog({ open, onOpenChange, entryDate, direction, documentType, onAdd, }: DeadlineAddDialogProps) { const [step, setStep] = useState("category"); const [selectedCategory, setSelectedCategory] = useState(null); const [selectedType, setSelectedType] = useState( null, ); const [startDate, setStartDate] = useState(entryDate); const [isCJ, setIsCJ] = useState(false); // ── Prelungire helper state ── const [cuIssueDate, setCuIssueDate] = useState(""); const [cuDurationMonths, setCuDurationMonths] = useState(null); const categories = useMemo( () => getCategoriesForDirection(direction, documentType), [direction, documentType], ); const typesForCategory = useMemo(() => { if (!selectedCategory) return []; return getSelectableDeadlines(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]); // Compute CU expiry when user uses the prelungire helper const computedExpiryDate = useMemo(() => { if (!cuIssueDate || !cuDurationMonths) return null; const issue = new Date(cuIssueDate); issue.setMonth(issue.getMonth() + cuDurationMonths); return issue; }, [cuIssueDate, cuDurationMonths]); const handleClose = () => { setStep("category"); setSelectedCategory(null); setSelectedType(null); setStartDate(entryDate); setIsCJ(false); setCuIssueDate(""); setCuDurationMonths(null); 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); setIsCJ(false); } else if (step === "date") { setStep("type"); setSelectedType(null); setCuIssueDate(""); setCuDurationMonths(null); } }; const handleConfirm = () => { if (!selectedType || !startDate) return; const isCUType = selectedType.id === "cu-emitere-l50" || selectedType.id === "cu-emitere-l350"; onAdd(selectedType.id, startDate, { isCJ: isCUType ? isCJ : undefined, }); handleClose(); }; // Apply prelungire helper: set start date from computed expiry const handleApplyExpiryHelper = () => { if (computedExpiryDate) { const y = computedExpiryDate.getFullYear(); const m = String(computedExpiryDate.getMonth() + 1).padStart(2, "0"); const d = String(computedExpiryDate.getDate()).padStart(2, "0"); setStartDate(`${y}-${m}-${d}`); } }; const isPrelungireType = selectedType?.id === "cu-prelungire-emitere"; const isCUEmitere = selectedType?.id === "cu-emitere-l50" || selectedType?.id === "cu-emitere-l350"; return ( { if (!o) handleClose(); }} > {step === "category" && "Adauga termen legal — Categorie"} {step === "type" && `Adauga termen legal — ${selectedCategory ? CATEGORY_LABELS[selectedCategory] : ""}`} {step === "date" && `Adauga termen legal — ${selectedType?.label ?? ""}`} {/* ── Step 1: Category selection ── */} {step === "category" && (
{categories.map((cat) => { const count = getSelectableDeadlines(cat).length; return ( ); })} {/* Direction info */}

{direction === "iesit" ? "Categoriile afisate sunt pentru demersuri depuse de noi (iesiri) — termene pe care le urmarim la institutii." : "Categoriile afisate sunt pentru acte administrative primite (intrari) — termene de contestare/raspuns."}

)} {/* ── Step 2: Type selection ── */} {step === "type" && (
{/* CJ toggle for CU category */} {selectedCategory === "certificat" && ( )}
{typesForCategory.map((typ) => ( ))}
{/* Info about auto-tracked deadlines for CU */} {selectedCategory === "certificat" && (

Termene automate (in fundal):

Verificare cerere CU (10 zile lucr.) — se creeaza automat. Dupa expirare, institutia nu mai poate returna documentatia.

{isCJ && (

Aviz primar CJ (3z + 5z) — se creeaza automat la bifarea optiunii CJ.

)}
)}
)} {/* ── Step 3: Date confirmation + preview ── */} {step === "date" && selectedType && (
{/* Prelungire helper: compute expiry from CU issue date */} {isPrelungireType && (
Calculator data expirare CU

Introdu data emiterii CU si selecteaza durata de valabilitate pentru a calcula automat data expirarii.

setCuIssueDate(e.target.value)} className="mt-0.5 h-8 text-xs" />
{[6, 12, 24].map((months) => ( ))}
{computedExpiryDate && (
Data expirare CU:{" "} {computedExpiryDate.toLocaleDateString("ro-RO", { day: "2-digit", month: "2-digit", year: "numeric", })}
)}
)} {/* Start date input */}
{selectedType.startDateHint && (

{selectedType.startDateHint}

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

{selectedType.isBackwardDeadline ? "Termen limita depunere" : "Termen limita calculat"}

{dueDatePreview}

{selectedType.days}{" "} {selectedType.dayType === "working" ? "zile lucratoare" : "zile calendaristice"} {selectedType.isBackwardDeadline ? " INAINTE" : " de la data start"}

{selectedType.legalReference && (

Ref: {selectedType.legalReference}

)}
)} {/* CJ info reminder */} {isCUEmitere && isCJ && (

Solicitat la CJ — se vor crea automat sub-termenele de aviz primar (3z solicitat + 5z emis).

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