import React, { useState, useEffect } from 'react'; import { useParams, useNavigate, useSearchParams } from 'react-router-dom'; import { usePPMDetails, usePPMMutations } from '../hooks/usePPM'; import { FaArrowLeft, FaSave, FaEdit, FaTools } from 'react-icons/fa'; import type { CreatePPMData } from '../services/ppmService'; const PPMDetail: React.FC = () => { const { ppmName } = useParams<{ ppmName: string }>(); const navigate = useNavigate(); const [searchParams] = useSearchParams(); const duplicateFromPPM = searchParams.get('duplicate'); const isNewPPM = ppmName === 'new'; const isDuplicating = isNewPPM && !!duplicateFromPPM; const { ppm, loading, error, refetch } = usePPMDetails( isDuplicating ? duplicateFromPPM : (isNewPPM ? null : ppmName || null) ); const { createPPM, updatePPM, loading: saving } = usePPMMutations(); const [isEditing, setIsEditing] = useState(isNewPPM); const [formData, setFormData] = useState({ company: '', asset_name: '', custom_asset_type: '', maintenance_team: '', custom_frequency: '', custom_total_amount: 0, custom_no_of_pms: 0, custom_price_per_pm: 0, }); useEffect(() => { if (ppm) { setFormData({ company: ppm.company || '', asset_name: ppm.asset_name || '', custom_asset_type: ppm.custom_asset_type || '', maintenance_team: ppm.maintenance_team || '', custom_frequency: ppm.custom_frequency || '', custom_total_amount: ppm.custom_total_amount || 0, custom_no_of_pms: ppm.custom_no_of_pms || 0, custom_price_per_pm: ppm.custom_price_per_pm || 0, }); } }, [ppm, isDuplicating]); const handleChange = (e: React.ChangeEvent) => { const { name, value } = e.target; setFormData(prev => ({ ...prev, [name]: name.includes('amount') || name.includes('pms') || name.includes('price') ? parseFloat(value) || 0 : value })); }; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); if (!formData.asset_name) { alert('Please enter Asset Name'); return; } try { if (isNewPPM || isDuplicating) { const result = await createPPM(formData); const successMessage = isDuplicating ? 'PPM schedule duplicated successfully!' : 'PPM schedule created successfully!'; alert(successMessage); if (result.asset_maintenance?.name) { navigate(`/ppm/${result.asset_maintenance.name}`); } else { refetch(); navigate('/ppm'); } } else if (ppmName) { await updatePPM(ppmName, formData); alert('PPM schedule updated successfully!'); setIsEditing(false); refetch(); } } catch (err) { console.error('PPM save error:', err); alert('Failed to save: ' + (err instanceof Error ? err.message : 'Unknown error')); } }; if (loading) { return (

Loading PPM schedule...

); } if (error && !isNewPPM && !isDuplicating) { return (

Error: {error}

); } return (
{/* Header */}
{!isNewPPM && !isEditing && ( )}
{/* Main Form */}
{/* Basic Information */}

Basic Information

{isEditing ? ( ) : (

{ppm?.company || '-'}

)}
{isEditing ? ( ) : (

{ppm?.asset_name || '-'}

)}
{isEditing ? ( ) : (

{ppm?.custom_asset_type || '-'}

)}
{isEditing ? ( ) : (

{ppm?.maintenance_team || '-'}

)}
{isEditing ? ( ) : (

{ppm?.custom_frequency || '-'}

)}
{/* Financial Information */}

Financial Information

{isEditing ? ( ) : (

{ppm?.custom_no_of_pms || '-'}

)}
{isEditing ? ( ) : (

{ppm?.custom_price_per_pm ? `$${ppm.custom_price_per_pm.toLocaleString()}` : '-'}

)}
{isEditing ? ( ) : (

{ppm?.custom_total_amount ? `$${ppm.custom_total_amount.toLocaleString()}` : '-'}

)}
{/* Sidebar Info */}

Schedule Information

{!isNewPPM && ppm && ( <>

PPM ID

{ppm.name}

Created

{ppm.creation ? new Date(ppm.creation).toLocaleString() : '-'}

)} {isNewPPM && (

Schedule information will appear after creation

)}
{/* Action Buttons */} {isEditing && (
)}
); }; export default PPMDetail;