'use client'; import { useState, useEffect } from 'react'; import { Card } from '@/components/ui/card'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { Button } from '@/components/ui/button'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from '@/components/ui/select'; import { Checkbox } from '@/components/ui/checkbox'; import { Badge } from '@/components/ui/badge'; import { NumberingTemplate } from '@/lib/api/numbering'; import { HoverCard, HoverCardContent, HoverCardTrigger, } from '@/components/ui/hover-card'; // Aligned with Backend replacement logic const VARIABLES = [ { key: '{PROJECT}', name: 'Project Code', example: 'LCBP3' }, { key: '{ORG}', name: 'Originator Code', example: 'PAT' }, { key: '{RECIPIENT}', name: 'Recipient Code', example: 'CN' }, { key: '{TYPE}', name: 'Type Code', example: 'RFA' }, { key: '{DISCIPLINE}', name: 'Discipline Code', example: 'STR' }, { key: '{SUBTYPE}', name: 'Sub-Type Code', example: 'GEN' }, { key: '{SUBTYPE_NUM}', name: 'Sub-Type Number', example: '01' }, { key: '{YEAR:BE}', name: 'Year (B.E.)', example: '2568' }, { key: '{YEAR:CE}', name: 'Year (C.E.)', example: '2025' }, { key: '{SEQ:4}', name: 'Sequence (4-digit)', example: '0001' }, ]; export interface TemplateEditorProps { template?: NumberingTemplate; projectId: number; projectName: string; /* eslint-disable @typescript-eslint/no-explicit-any */ correspondenceTypes: any[]; disciplines: any[]; /* eslint-enable @typescript-eslint/no-explicit-any */ onSave: (data: Partial) => void; onCancel: () => void; } export function TemplateEditor({ template, projectId, projectName, correspondenceTypes, // eslint-disable-next-line @typescript-eslint/no-unused-vars disciplines, onSave, onCancel }: TemplateEditorProps) { const [format, setFormat] = useState(template?.formatTemplate || ''); const [typeId, setTypeId] = useState(template?.correspondenceTypeId?.toString() || ''); const [reset, setReset] = useState(template?.resetSequenceYearly ?? true); const [preview, setPreview] = useState(''); useEffect(() => { // Generate preview let previewText = format || ''; VARIABLES.forEach((v) => { // Simple mock replacement for preview let replacement = v.example; if (v.key === '{YEAR:BE}') replacement = (new Date().getFullYear() + 543).toString(); if (v.key === '{YEAR:CE}') replacement = new Date().getFullYear().toString(); // Dynamic context based on selection (optional visual enhancement) if (v.key === '{TYPE}' && typeId) { const t = correspondenceTypes.find(ct => ct.id.toString() === typeId); if (t) replacement = t.typeCode; } previewText = previewText.replace(new RegExp(v.key.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'g'), replacement); }); setPreview(previewText); }, [format, typeId, correspondenceTypes]); const insertVariable = (variable: string) => { setFormat((prev: string) => prev + variable); }; const handleSave = () => { onSave({ ...template, projectId: projectId, correspondenceTypeId: typeId && typeId !== '__default__' ? Number(typeId) : null, formatTemplate: format, resetSequenceYearly: reset, }); }; const isValid = format.length > 0; // typeId is optional (null = default for all types) return (

{template ? 'Edit Template' : 'New Template'}

Define how document numbers are generated for this project.

Project: {projectName}
{/* Configuration Column */}

Leave empty to create a default template for this project.

{/* Format Column */}
setFormat(e.target.value)} placeholder="{ORG}-{TYPE}-{SEQ:4}" className="font-mono text-base mb-2" />
{VARIABLES.map((v) => (

{v.name}

Example: {v.example}

))}

Preview Output

{preview || '...'}

* This is an approximation. Actual numbers depend on runtime context.

); }