'use client'; import { useState, useEffect } from 'react'; import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'; import { Button } from '@/components/ui/button'; import { Badge } from '@/components/ui/badge'; import { Label } from '@/components/ui/label'; import { Input } from '@/components/ui/input'; import { Textarea } from '@/components/ui/textarea'; import { Switch } from '@/components/ui/switch'; import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, AlertDialogTrigger, } from '@/components/ui/alert-dialog'; import { Server, Edit, Save, Power, Info, Cpu, RefreshCw, User, Receipt, Ticket as TicketIcon } from 'lucide-react'; import { format } from 'date-fns'; import { ConfigurationItem } from '@/lib/types/autotask'; import { PurchaseHistoryModal } from './purchase-history-modal'; import { RelatedTicketsModal } from './related-tickets-modal'; interface PSATabProps { device?: ConfigurationItem; onUpdate: (device: ConfigurationItem) => void; } export function PSATab({ device, onUpdate }: PSATabProps) { const [editMode, setEditMode] = useState(false); const [saving, setSaving] = useState(false); const [editedData, setEditedData] = useState>(device || {}); const [error, setError] = useState(null); const [contactName, setContactName] = useState(null); const [loadingContact, setLoadingContact] = useState(false); const [purchaseHistoryOpen, setPurchaseHistoryOpen] = useState(false); const [relatedTicketsOpen, setRelatedTicketsOpen] = useState(false); // Fetch contact information if contactID exists useEffect(() => { if (!device?.contactID) { setContactName(null); return; } const fetchContact = async () => { setLoadingContact(true); try { const response = await fetch(`/api/contacts/${device.contactID}`); if (response.ok) { const data = await response.json(); setContactName(data.contact ? `${data.contact.firstName} ${data.contact.lastName}` : null); } } catch (err) { console.error('Failed to fetch contact:', err); } finally { setLoadingContact(false); } }; fetchContact(); }, [device?.contactID]); const handleSave = async () => { if (!device) return; setSaving(true); setError(null); try { const response = await fetch(`/api/configuration-items/${device.id}`, { method: 'PATCH', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(editedData), }); if (!response.ok) { throw new Error('Failed to update configuration item'); } const updated = await response.json(); onUpdate(updated.configurationItem); setEditMode(false); } catch (err) { setError(err instanceof Error ? err.message : 'Failed to save changes'); } finally { setSaving(false); } }; const handleMakeInactive = async () => { if (!device) return; setSaving(true); setError(null); try { const response = await fetch(`/api/configuration-items/${device.id}`, { method: 'PATCH', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ isActive: false }), }); if (!response.ok) { throw new Error('Failed to update configuration item'); } const updated = await response.json(); onUpdate(updated.configurationItem); setEditedData({ ...editedData, isActive: false }); } catch (err) { setError(err instanceof Error ? err.message : 'Failed to make inactive'); } finally { setSaving(false); } }; return ( <>
PSA Configuration Item
{!editMode ? ( <> {device?.isActive && ( Make Configuration Item Inactive? This will mark the configuration item as inactive in Autotask PSA. You can reactivate it later if needed. Cancel Make Inactive )} ) : ( <> )}
{error && (
{error}
)} {device ? (
{/* Basic Information */}

Basic Information

{editMode ? ( setEditedData({...editedData, referenceTitle: e.target.value})} className="mt-1" /> ) : (

{device.referenceTitle}

)}
{editMode ? ( setEditedData({...editedData, referenceNumber: e.target.value})} className="mt-1" /> ) : (

{device.referenceNumber || '-'}

)}
{editMode ? ( setEditedData({...editedData, serialNumber: e.target.value})} className="mt-1" /> ) : (

{device.serialNumber || '-'}

)}
{editMode ? ( setEditedData({...editedData, location: e.target.value})} className="mt-1" /> ) : (

{device.location || '-'}

)}
{editMode ? (
setEditedData({...editedData, isActive: checked})} />
) : (
{device.isActive ? ( Active ) : ( Inactive )}
)}
{loadingContact ? (

Loading...

) : contactName ? (
{contactName}
) : device.contactID ? (

Contact ID: {device.contactID}

) : (

No contact assigned

)}
{/* Technical Details */}

Technical Details

{editMode ? ( setEditedData({...editedData, modelNumber: e.target.value})} className="mt-1" /> ) : (

{device.modelNumber || '-'}

)}
{editMode ? ( setEditedData({...editedData, macAddress: e.target.value})} className="mt-1" /> ) : (

{device.macAddress || '-'}

)}

{device.installDate ? format(new Date(device.installDate), 'MMM d, yyyy') : '-'}

{device.warrantyExpirationDate ? format(new Date(device.warrantyExpirationDate), 'MMM d, yyyy') : '-'}

{device.rmmDeviceUID || '-'}

{/* Notes */}

Notes

{editMode ? (