import React from 'react'; import { exportReportToExcel } from '~/utils/excelExport'; interface ReportSheet { id: string; date: string; area: string; dredgerLocation: string; reclamationLocation: string; dayReport?: any; nightReport?: any; } interface ReportSheetViewModalProps { isOpen: boolean; onClose: () => void; sheet: ReportSheet | null; } export default function ReportSheetViewModal({ isOpen, onClose, sheet }: ReportSheetViewModalProps) { if (!isOpen || !sheet) return null; const handleExportExcel = async () => { // Export the entire sheet (both day and night reports combined) await exportReportToExcel(sheet); }; // Helper function to parse time string (HH:MM) to minutes const parseTimeToMinutes = (timeStr: string): number => { if (!timeStr || timeStr === '00:00') return 0; const [hours, minutes] = timeStr.split(':').map(Number); return (hours || 0) * 60 + (minutes || 0); }; // Helper function to format minutes back to HH:MM const formatMinutesToTime = (totalMinutes: number): string => { const hours = Math.floor(totalMinutes / 60); const minutes = totalMinutes % 60; return `${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}`; }; // Calculate stoppage statistics const calculateStoppageStats = () => { const dayStoppages = sheet.dayReport?.stoppages || []; const nightStoppages = sheet.nightReport?.stoppages || []; // Calculate total time for day shift stoppages const totalDayMinutes = dayStoppages.reduce((sum, stoppage) => { return sum + parseTimeToMinutes(stoppage.total || '00:00'); }, 0); // Calculate total time for night shift stoppages const totalNightMinutes = nightStoppages.reduce((sum, stoppage) => { return sum + parseTimeToMinutes(stoppage.total || '00:00'); }, 0); // Calculate total combined time const totalMinutes = totalDayMinutes + totalNightMinutes; // Calculate counted stoppages time (excluding "Brine" or "Change Shift" in notes) const countedDayMinutes = dayStoppages .filter(stoppage => { const note = (stoppage.note || '').toLowerCase(); return !note.includes('brine') && !note.includes('change shift') && !note.includes('shift change'); }) .reduce((sum, stoppage) => { return sum + parseTimeToMinutes(stoppage.total || '00:00'); }, 0); const countedNightMinutes = nightStoppages .filter(stoppage => { const note = (stoppage.note || '').toLowerCase(); return !note.includes('brine') && !note.includes('change shift') && !note.includes('shift change'); }) .reduce((sum, stoppage) => { return sum + parseTimeToMinutes(stoppage.total || '00:00'); }, 0); const totalCountedMinutes = countedDayMinutes + countedNightMinutes; return { totalDayTime: formatMinutesToTime(totalDayMinutes), totalNightTime: formatMinutesToTime(totalNightMinutes), totalTime: formatMinutesToTime(totalMinutes), countedDayTime: formatMinutesToTime(countedDayMinutes), countedNightTime: formatMinutesToTime(countedNightMinutes), totalCountedTime: formatMinutesToTime(totalCountedMinutes), totalMinutes, totalCountedMinutes }; }; const stoppageStats = calculateStoppageStats(); return (

Report Sheet - {new Date(sheet.date).toLocaleDateString('en-GB')}

{/* Combined Report Sheet Layout */}
{/* Day Shift Section */} {sheet.dayReport && ( <> )} {/* Night Shift Section */} {sheet.nightReport && ( <> )}
{/* Summary Statistics Section */}

Stoppage Summary Statistics

{/* Total Stoppage Time */}
Total Stoppage Time
Day Shift: {stoppageStats.totalDayTime}
Night Shift: {stoppageStats.totalNightTime}
Total: {stoppageStats.totalTime}
{/* Counted Stoppage Time */}
Counted Stoppage Time
{/* (Excluding "Brine" & "Change Shift") */}
Day Shift: {stoppageStats.countedDayTime}
Night Shift: {stoppageStats.countedNightTime}
Total: {stoppageStats.totalCountedTime}
{/* Time Analysis */}
Time Analysis
Counted Rate: {stoppageStats.totalMinutes > 0 ? `${Math.round((stoppageStats.totalCountedMinutes / stoppageStats.totalMinutes) * 100)}%` : '0%' }
0 ? `${(stoppageStats.totalCountedMinutes / stoppageStats.totalMinutes) * 100}%` : '0%' }} >
Excluded Time: {formatMinutesToTime(stoppageStats.totalMinutes - stoppageStats.totalCountedMinutes)}
{/*
Avg per Day: {formatMinutesToTime(Math.round(stoppageStats.totalCountedMinutes / 2))}
*/}
{/* Additional Info */}

Note: Counted stoppage time excludes entries with "Brine" or "Change Shift" in the notes field, as these are typically planned operational activities rather than unplanned stoppages. Times are displayed in HH:MM format.

); } // Header Section Component function ReportSheetHeader({ sheet }: { sheet: ReportSheet }) { return (
Company Logo { e.currentTarget.style.display = 'none'; }} />
Reclamation Work Diary
QF-3.6.1-08
Rev. 1.0
Arab Potash Logo { e.currentTarget.style.display = 'none'; }} />
); } // Report Info Component function ReportSheetInfo({ sheet }: { sheet: ReportSheet }) { return (
Date: {new Date(sheet.date).toLocaleDateString('en-GB')} Report No. {sheet.id}
); } // Dredger Section Component function ReportSheetDredgerSection({ sheet }: { sheet: ReportSheet }) { return (
{sheet.area} Dredger
); } // Location Data Component (using data from either available report) function ReportSheetLocationData({ sheet }: { sheet: ReportSheet }) { const report = sheet.dayReport || sheet.nightReport; if (!report) return null; return (
Dredger Location {report.dredgerLocation.name} Dredger Line Length {report.dredgerLineLength}
Reclamation Location {report.reclamationLocation.name} Shore Connection {report.shoreConnection}
Reclamation Height {report.reclamationHeight?.base || 0}m - {(report.reclamationHeight?.extra + report.reclamationHeight?.base || 0 || 0)}m
); } // Pipeline Length Component (using data from either available report) function ReportSheetPipelineLength({ sheet }: { sheet: ReportSheet }) { const report = sheet.dayReport || sheet.nightReport; if (!report) return null; return (
Pipeline Length "from Shore Connection" Main extension total Reserve extension total
{report.pipelineLength?.main || 0} {report.pipelineLength?.ext1 || 0} {(report.pipelineLength?.main || 0) + (report.pipelineLength?.ext1 || 0)} {report.pipelineLength?.reserve || 0} {report.pipelineLength?.ext2 || 0} {(report.pipelineLength?.reserve || 0) + (report.pipelineLength?.ext2 || 0)}
); } // Shift Header Component function ReportSheetShiftHeader({ shift }: { shift: string }) { return (
{shift.charAt(0).toUpperCase() + shift.slice(1)} Shift
); } // Equipment Statistics Component function ReportSheetEquipmentStats({ report }: { report: any }) { return (
Dozers Exc. Loader Foreman Laborer
{report.stats?.Dozers || 0} {report.stats?.Exc || 0} {report.stats?.Loaders || 0} {report.stats?.Foreman || ''} {report.stats?.Laborer || 0}
); } // Time Sheet Component function ReportSheetTimeSheet({ report }: { report: any }) { return (
{Array.isArray(report.timeSheet) && report.timeSheet.length > 0 ? ( report.timeSheet.map((entry: any, index: number) => ( )) ) : ( )}
Time Sheet From To From To Total Reason
{entry.machine} {entry.from1} {entry.to1} {entry.from2} {entry.to2} {entry.total} {entry.reason}
No time sheet entries
); } // Stoppages Component function ReportSheetStoppages({ report }: { report: any }) { return ( <>
Dredger Stoppages
{Array.isArray(report.stoppages) && report.stoppages.length > 0 ? ( report.stoppages.map((entry: any, index: number) => ( )) ) : ( )}
From To Total Reason Responsible Notes
{entry.from} {entry.to} {entry.total} {entry.reason} {entry.responsible} {entry.note}
No stoppages recorded
); } // Notes Component function ReportSheetNotes({ report }: { report: any }) { return ( <>
Notes & Comments
{report.notes || 'No additional notes'}
); } // Footer Component function ReportSheetFooter() { return (
{/* موقعة لأعمال الصيانة */}
); }