import React, { useState, useEffect } from 'react'; import { Container, Row, Col, Card, Badge, Button, Spinner, Alert, ListGroup, Modal, Form, Breadcrumb, Table } from 'react-bootstrap'; import { Link, useParams, useNavigate } from 'react-router-dom'; import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import { faWrench, faHome, faCalendarAlt, faUser, faInfoCircle, faExclamationTriangle, faEdit, faTimes, faCheckCircle, faHistory, faComments, faClock, faMapMarkerAlt } from '@fortawesome/free-solid-svg-icons'; import axios from 'axios'; const ServiceRequestDetail = () => { const { requestId } = useParams(); const navigate = useNavigate(); const [serviceRequest, setServiceRequest] = useState(null); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [showCancelModal, setShowCancelModal] = useState(false); const [cancelReason, setCancelReason] = useState(''); const [cancelling, setCancelling] = useState(false); useEffect(() => { // Load service request details const fetchServiceRequestDetails = async () => { setLoading(true); setError(null); try { const response = await axios.get(`/api/v1/service-requests/${requestId}`); setServiceRequest(response.data.data); } catch (err) { setError('Failed to load service request details. Please try again.'); console.error('Error fetching service request:', err); } finally { setLoading(false); } }; fetchServiceRequestDetails(); }, [requestId]); const handleCancelRequest = () => { setShowCancelModal(true); }; const confirmCancelRequest = async () => { setCancelling(true); try { await axios.put(`/api/v1/service-requests/${requestId}`, { status: 'cancelled', technician_notes: cancelReason || 'Cancelled by homeowner' }); // Close modal and refresh data setShowCancelModal(false); setCancelReason(''); // Reload service request details const response = await axios.get(`/api/v1/service-requests/${requestId}`); setServiceRequest(response.data.data); } catch (err) { setError('Failed to cancel service request. Please try again.'); console.error('Error cancelling service request:', err); } finally { setCancelling(false); } }; // Render status badge const renderStatusBadge = (status) => { const statusMap = { 'pending': { bg: 'secondary', text: 'Pending' }, 'assigned': { bg: 'info', text: 'Assigned' }, 'scheduled': { bg: 'primary', text: 'Scheduled' }, 'in_progress': { bg: 'warning', text: 'In Progress' }, 'completed': { bg: 'success', text: 'Completed' }, 'cancelled': { bg: 'danger', text: 'Cancelled' } }; const { bg, text } = statusMap[status] || { bg: 'secondary', text: status }; return {text}; }; // Render priority badge const renderPriorityBadge = (priority) => { const priorityMap = { 'low': { variant: 'success', text: 'Low Priority' }, 'medium': { variant: 'info', text: 'Medium Priority' }, 'high': { variant: 'warning', text: 'High Priority' }, 'emergency': { variant: 'danger', text: 'Emergency' } }; const { variant, text } = priorityMap[priority] || { variant: 'secondary', text: priority }; return {text}; }; // Render service type badge const renderServiceTypeBadge = (serviceType) => { const serviceTypeMap = { 'repair': { variant: 'danger', text: 'Repair' }, 'maintenance': { variant: 'primary', text: 'Maintenance' }, 'installation': { variant: 'success', text: 'Installation' }, 'inspection': { variant: 'info', text: 'Inspection' } }; const { variant, text } = serviceTypeMap[serviceType] || { variant: 'secondary', text: serviceType }; return {text}; }; if (loading) { return (
Loading...

Loading service request details...

); } if (error) { return ( {error}
); } if (!serviceRequest) { return ( Service request not found or you don't have permission to view it.
); } return ( Home Service Requests Request #{serviceRequest.id}

{renderServiceTypeBadge(serviceRequest.service_type)} {serviceRequest.equipment ? serviceRequest.equipment.name : 'General Service'}

{renderStatusBadge(serviceRequest.status)} {renderPriorityBadge(serviceRequest.priority)}
Description

{serviceRequest.description}

{serviceRequest.technician_notes && (
Technician Notes

{serviceRequest.technician_notes}

)}
Details
{serviceRequest.preferred_date && ( )}
Request ID #{serviceRequest.id}
Created On {new Date(serviceRequest.created_at).toLocaleString()}
Last Updated {new Date(serviceRequest.updated_at).toLocaleString()}
Preferred Date {serviceRequest.preferred_date} {serviceRequest.preferred_time_window && ( ({serviceRequest.preferred_time_window}) )}
{serviceRequest.meta.can_cancel && ( )} {!serviceRequest.meta.is_completed && ( )}
{/* Property Information */}
Property Information
{serviceRequest.property.name}

{serviceRequest.property.address}

View Property
{/* Equipment Information (if any) */} {serviceRequest.equipment && (
Equipment Information
{serviceRequest.equipment.name}

Model: {serviceRequest.equipment.model}

Manufacturer: {serviceRequest.equipment.manufacturer}

View Equipment
)} {/* Technician Information (if assigned) */} {serviceRequest.technician && (
Assigned Technician
{serviceRequest.technician.name}
{/* More technician details would be displayed here */}
)} {/* Status Timeline */}
Status Timeline
Request Created
by {serviceRequest.requester.name}
{new Date(serviceRequest.created_at).toLocaleString()}
{/* Additional timeline items would be dynamically generated based on service request history */} {serviceRequest.status !== 'pending' && (
Status Updated
to {serviceRequest.status_display}
{new Date(serviceRequest.updated_at).toLocaleString()}
)}
{/* Cancel Confirmation Modal */} setShowCancelModal(false)}> Confirm Cancellation

Are you sure you want to cancel this service request?

Reason for cancellation (optional) setCancelReason(e.target.value)} placeholder="Please provide a reason for cancellation..." />
); }; export default ServiceRequestDetail;