import React, { useState, useEffect, useRef } from 'react'; import { License, Renewal, Contact, NotificationGroup } from '../types'; import { TrashIcon, PlusIcon, BriefcaseIcon, SaveIcon, BellIcon, ArchiveIcon } from './Icons'; import { fetchContacts, fetchGroups, deleteFile } from '../graphService'; interface LicenseFormProps { isOpen: boolean; onClose: () => void; onSubmit: (license: License & { newFiles?: any[] }) => void; initialData?: License | null; } const emptyLicense: Omit = { licenseName: '', companyName: '', responsiblePerson: '', purchaseDate: new Date().toISOString().split('T')[0], endDate: '', purchasePrice: 0, vendorName: '', vendorContact: { name: '', email: '', phone: '' }, notificationGroupId: null, comments: '', renewals: [], tags: [], isActive: true, image: '' }; const LicenseForm: React.FC = ({ isOpen, onClose, onSubmit, initialData }) => { const [license, setLicense] = useState>({ ...emptyLicense }); const [tagsInput, setTagsInput] = useState(''); const [newFiles, setNewFiles] = useState<{ name: string; type: string; data: string }[]>([]); const [contacts, setContacts] = useState([]); const [groups, setGroups] = useState([]); const [loading, setLoading] = useState(false); const logoInputRef = useRef(null); useEffect(() => { if (isOpen) { fetchContacts().then(d => setContacts(Array.isArray(d) ? d : [])); fetchGroups().then(d => setGroups(Array.isArray(d) ? d : [])); setNewFiles([]); } if (initialData) { setLicense({ ...initialData }); setTagsInput(initialData.tags?.join(', ') || ''); } else { setLicense({ ...emptyLicense, renewals: [], tags: [] }); setTagsInput(''); } }, [initialData, isOpen]); if (!isOpen) return null; const handleLogoUpload = (e: React.ChangeEvent) => { const file = e.target.files?.[0]; if (file) { const reader = new FileReader(); reader.onload = (evt) => { const result = evt.target?.result; if (typeof result === 'string') { setLicense(prev => ({ ...prev, image: result })); } }; reader.readAsDataURL(file); } }; const handleFileUpload = (e: React.ChangeEvent) => { if (e.target.files) { Array.from(e.target.files).forEach((file: File) => { const reader = new FileReader(); reader.onload = (evt: ProgressEvent) => { const result = evt.target?.result; if (typeof result === 'string') { setNewFiles(prev => [...prev, { name: file.name, type: file.type, data: result }]); } }; reader.readAsDataURL(file); }); } }; const handleVendorSelection = (vendorName: string) => { if (!vendorName) { setLicense(prev => ({ ...prev, vendorName: '', vendorContact: { name: '', email: '', phone: '' } })); return; } const vendor = contacts.find(c => c.vendorName === vendorName); if (vendor) { setLicense(prev => ({ ...prev, vendorName: vendor.vendorName, vendorContact: { name: vendor.contactName || '', email: vendor.contactEmail || '', phone: vendor.contactPhone || '' } })); } else { setLicense(prev => ({ ...prev, vendorName })); } }; const handleMigrateToHistory = () => { if (!license.purchaseDate) { alert("Current contract needs a start date to migrate."); return; } const newRenewal: Renewal = { id: `renewal-migrated-${Date.now()}`, renewalDate: license.purchaseDate, endDate: license.endDate, renewalPrice: license.purchasePrice, notes: 'Archived from primary record' }; setLicense(prev => ({ ...prev, renewals: [newRenewal, ...prev.renewals], purchaseDate: new Date().toISOString().split('T')[0], endDate: '', purchasePrice: 0 })); }; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); if (!license.licenseName) return; setLoading(true); try { const finalId = initialData?.id || `license-${Date.now()}`; // Parse tags from the raw input string const parsedTags = tagsInput .split(',') .map(t => t.trim()) .filter(t => t.length > 0); await onSubmit({ ...license, id: finalId, tags: parsedTags, newFiles } as any); } catch (err: any) { console.error("Submit error", err); } finally { setLoading(false); } }; return (

{initialData ? 'Update License Details' : 'Register New License'}

logoInputRef.current?.click()}>
Change Logo
setLicense({...license, licenseName: e.target.value})} required className="input-style text-lg font-bold" placeholder="e.g. Photoshop Pro" />
setLicense({...license, companyName: e.target.value})} className="input-style" placeholder="e.g. Marketing Dept" />
setTagsInput(e.target.value)} className="input-style" placeholder="IT, SaaS, Creative" />