import type { ActionFunctionArgs, LoaderFunctionArgs, MetaFunction } from "@remix-run/node"; import { json, redirect } from "@remix-run/node"; import { Form, useActionData, useLoaderData, useNavigation, Link } from "@remix-run/react"; import { requireAuthLevel } from "~/utils/auth.server"; import DashboardLayout from "~/components/DashboardLayout"; import { useState, useEffect } from "react"; import { manageSheet } from "~/utils/sheet.server"; import { prisma } from "~/utils/db.server"; export const meta: MetaFunction = () => [{ title: "New Report - Alhaffer Report System" }]; export const loader = async ({ request }: LoaderFunctionArgs) => { const user = await requireAuthLevel(request, 1); // All employees can create reports // Get dropdown data for form const [areas, dredgerLocations, reclamationLocations, foremen, equipment] = await Promise.all([ prisma.area.findMany({ orderBy: { name: 'asc' } }), prisma.dredgerLocation.findMany({ orderBy: { name: 'asc' } }), prisma.reclamationLocation.findMany({ orderBy: { name: 'asc' } }), prisma.foreman.findMany({ orderBy: { name: 'asc' } }), prisma.equipment.findMany({ orderBy: [{ category: 'asc' }, { model: 'asc' }, { number: 'asc' }] }) ]); return json({ user, areas, dredgerLocations, reclamationLocations, foremen, equipment }); }; export const action = async ({ request }: ActionFunctionArgs) => { const user = await requireAuthLevel(request, 1); const formData = await request.formData(); // Debug logging console.log("Form data received:", Object.fromEntries(formData.entries())); const shift = formData.get("shift"); const areaId = formData.get("areaId"); const dredgerLocationId = formData.get("dredgerLocationId"); const dredgerLineLength = formData.get("dredgerLineLength"); const reclamationLocationId = formData.get("reclamationLocationId"); const shoreConnection = formData.get("shoreConnection"); const notes = formData.get("notes"); // Complex JSON fields const reclamationHeightBase = formData.get("reclamationHeightBase"); const reclamationHeightExtra = formData.get("reclamationHeightExtra"); const pipelineMain = formData.get("pipelineMain"); const pipelineExt1 = formData.get("pipelineExt1"); const pipelineReserve = formData.get("pipelineReserve"); const pipelineExt2 = formData.get("pipelineExt2"); const statsDozers = formData.get("statsDozers"); const statsExc = formData.get("statsExc"); const statsLoaders = formData.get("statsLoaders"); const statsForeman = formData.get("statsForeman"); const statsLaborer = formData.get("statsLaborer"); const timeSheetData = formData.get("timeSheetData"); const stoppagesData = formData.get("stoppagesData"); // Validation // console.log("Validating fields:", { shift, areaId, dredgerLocationId, dredgerLineLength, reclamationLocationId, shoreConnection }); if (typeof shift !== "string" || !["day", "night"].includes(shift)) { console.log("Shift validation failed:", shift); return json({ errors: { shift: "Valid shift is required" } }, { status: 400 }); } if (typeof areaId !== "string" || !areaId) { return json({ errors: { areaId: "Area is required" } }, { status: 400 }); } if (typeof dredgerLocationId !== "string" || !dredgerLocationId) { return json({ errors: { dredgerLocationId: "Dredger location is required" } }, { status: 400 }); } if (typeof dredgerLineLength !== "string" || isNaN(parseInt(dredgerLineLength))) { return json({ errors: { dredgerLineLength: "Valid dredger line length is required" } }, { status: 400 }); } if (typeof reclamationLocationId !== "string" || !reclamationLocationId) { return json({ errors: { reclamationLocationId: "Reclamation location is required" } }, { status: 400 }); } if (typeof shoreConnection !== "string" || isNaN(parseInt(shoreConnection))) { return json({ errors: { shoreConnection: "Valid shore connection is required" } }, { status: 400 }); } try { // Parse JSON arrays let timeSheet = []; let stoppages = []; if (timeSheetData && typeof timeSheetData === "string") { try { timeSheet = JSON.parse(timeSheetData); } catch (e) { timeSheet = []; } } if (stoppagesData && typeof stoppagesData === "string") { try { stoppages = JSON.parse(stoppagesData); } catch (e) { stoppages = []; } } // Build automatic notes for pipeline extensions const ext1Value = parseInt(pipelineExt1 as string) || 0; const ext2Value = parseInt(pipelineExt2 as string) || 0; const shiftText = shift === 'day' ? 'Day' : 'Night'; let automaticNotes = []; // Add Extension 1 note if value > 0 if (ext1Value > 0) { automaticNotes.push(`Main Extension ${ext1Value}m ${shiftText}`); } // Add Extension 2 note if value > 0 if (ext2Value > 0) { automaticNotes.push(`Reserve Extension ${ext2Value}m ${shiftText}`); } // Combine automatic notes with user notes let finalNotes = notes || ''; if (automaticNotes.length > 0) { const automaticNotesText = automaticNotes.join(', '); if (finalNotes.trim()) { finalNotes = `${automaticNotesText}. ${finalNotes}`; } else { finalNotes = automaticNotesText; } } const report = await prisma.report.create({ data: { employeeId: user.id, shift, areaId: parseInt(areaId), dredgerLocationId: parseInt(dredgerLocationId), dredgerLineLength: parseInt(dredgerLineLength), reclamationLocationId: parseInt(reclamationLocationId), shoreConnection: parseInt(shoreConnection), reclamationHeight: { base: parseInt(reclamationHeightBase as string) || 0, extra: parseInt(reclamationHeightExtra as string) || 0 }, pipelineLength: { main: parseInt(pipelineMain as string) || 0, ext1: ext1Value, reserve: parseInt(pipelineReserve as string) || 0, ext2: ext2Value }, stats: { Dozers: parseInt(statsDozers as string) || 0, Exc: parseInt(statsExc as string) || 0, Loaders: parseInt(statsLoaders as string) || 0, Foreman: statsForeman as string || "", Laborer: parseInt(statsLaborer as string) || 0 }, timeSheet, stoppages, notes: finalNotes || null } }); // Manage sheet creation/update await manageSheet( report.id, shift, parseInt(areaId), parseInt(dredgerLocationId), parseInt(reclamationLocationId), report.createdDate ); // Redirect to reports page with success message return redirect("/reports?success=Report created successfully!"); } catch (error) { return json({ errors: { form: "Failed to create report. Please try again." } }, { status: 400 }); } }; export default function NewReport() { const { user, areas, dredgerLocations, reclamationLocations, foremen, equipment } = useLoaderData(); const actionData = useActionData(); const navigation = useNavigation(); // Form state to preserve values across steps const [formData, setFormData] = useState({ shift: '', areaId: '', dredgerLocationId: '', dredgerLineLength: '', reclamationLocationId: '', shoreConnection: '', reclamationHeightBase: '0', reclamationHeightExtra: '0', pipelineMain: '0', pipelineExt1: '0', pipelineReserve: '0', pipelineExt2: '0', statsDozers: '0', statsExc: '0', statsLoaders: '0', statsForeman: '', statsLaborer: '0', notes: '' }); // Dynamic arrays state const [timeSheetEntries, setTimeSheetEntries] = useState>([]); const [stoppageEntries, setStoppageEntries] = useState>([]); const [currentStep, setCurrentStep] = useState(1); const totalSteps = 4; const [showZeroEquipmentConfirm, setShowZeroEquipmentConfirm] = useState(false); const isSubmitting = navigation.state === "submitting"; // Function to update form data const updateFormData = (field: string, value: string) => { setFormData(prev => ({ ...prev, [field]: value })); }; // Handle form submission - only allow on final step const handleSubmit = (event: React.FormEvent) => { // console.log("Form submit triggered, current step:", currentStep); if (currentStep !== totalSteps) { console.log("Preventing form submission - not on final step"); event.preventDefault(); event.stopPropagation(); return false; } // Validate reclamation stoppages have notes const invalidStoppages = stoppageEntries.filter(entry => entry.responsible === 'reclamation' && !entry.note.trim() ); if (invalidStoppages.length > 0) { alert('Please add notes for all reclamation stoppages before submitting.'); event.preventDefault(); event.stopPropagation(); return false; } // console.log("Allowing form submission"); // console.log("Form being submitted with data:", formData); // console.log("Time sheet entries:", timeSheetEntries); // console.log("Stoppage entries:", stoppageEntries); }; // Helper functions for time calculations const calculateTimeDifference = (from1: string, to1: string, from2: string, to2: string) => { if (!from1 || !to1) return "00:00"; const parseTime = (timeStr: string) => { const [hours, minutes] = timeStr.split(':').map(Number); return hours * 60 + minutes; }; const formatTime = (minutes: number) => { const hours = Math.floor(minutes / 60); const mins = minutes % 60; return `${hours.toString().padStart(2, '0')}:${mins.toString().padStart(2, '0')}`; }; let totalMinutes = 0; if (from1 && to1) { const start1 = parseTime(from1); let end1 = parseTime(to1); if (end1 < start1) { end1 += 24 * 60; } totalMinutes += end1 - start1; } if (from2 && to2) { const start2 = parseTime(from2); let end2 = parseTime(to2); if (end2 < start2) { end2 += 24 * 60; } totalMinutes += end2 - start2; } return formatTime(Math.max(0, totalMinutes)); //return formatTime(Math.max(totalMinutes * -1, totalMinutes)); }; const calculateStoppageTime = (from: string, to: string) => { if (!from || !to) return "00:00"; const parseTime = (timeStr: string) => { const [hours, minutes] = timeStr.split(':').map(Number); return hours * 60 + minutes; }; const formatTime = (minutes: number) => { const hours = Math.floor(minutes / 60); const mins = minutes % 60; return `${hours.toString().padStart(2, '0')}:${mins.toString().padStart(2, '0')}`; }; const startMinutes = parseTime(from); let endMinutes = parseTime(to); if (endMinutes < startMinutes) endMinutes += 24 * 60; const totalMinutes = Math.max(0, endMinutes - startMinutes); return formatTime(totalMinutes); }; // Time Sheet management const addTimeSheetEntry = () => { const newEntry = { id: Date.now().toString(), machine: '', from1: '', to1: '', from2: '', to2: '', total: '00:00', reason: '' }; setTimeSheetEntries([...timeSheetEntries, newEntry]); }; const removeTimeSheetEntry = (id: string) => { setTimeSheetEntries(timeSheetEntries.filter(entry => entry.id !== id)); }; const updateTimeSheetEntry = (id: string, field: string, value: string) => { setTimeSheetEntries(timeSheetEntries.map(entry => { if (entry.id === id) { const updatedEntry = { ...entry, [field]: value }; if (['from1', 'to1', 'from2', 'to2'].includes(field)) { updatedEntry.total = calculateTimeDifference( updatedEntry.from1, updatedEntry.to1, updatedEntry.from2, updatedEntry.to2 ); } return updatedEntry; } return entry; })); }; // Auto-calculate equipment counts based on time sheet entries useEffect(() => { const counts = { dozers: 0, excavators: 0, loaders: 0 }; timeSheetEntries.forEach(entry => { if (entry.machine) { const equipmentItem = equipment.find(item => `${item.model} (${item.number})` === entry.machine ); if (equipmentItem) { const category = equipmentItem.category.toLowerCase(); if (category.includes('dozer')) { counts.dozers++; } else if (category.includes('excavator')) { counts.excavators++; } else if (category.includes('loader')) { counts.loaders++; } } } }); // Update form data with calculated counts setFormData(prev => ({ ...prev, statsDozers: counts.dozers.toString(), statsExc: counts.excavators.toString(), statsLoaders: counts.loaders.toString() })); }, [timeSheetEntries, equipment]); // Stoppage management const addStoppageEntry = () => { const newEntry = { id: Date.now().toString(), from: '', to: '', total: '00:00', reason: '', // Will be set to 'none' for reclamation by default responsible: 'reclamation', // Default to reclamation note: '' }; setStoppageEntries([...stoppageEntries, newEntry]); }; const removeStoppageEntry = (id: string) => { setStoppageEntries(stoppageEntries.filter(entry => entry.id !== id)); }; const updateStoppageEntry = (id: string, field: string, value: string) => { setStoppageEntries(stoppageEntries.map(entry => { if (entry.id === id) { const updatedEntry = { ...entry, [field]: value }; if (['from', 'to'].includes(field)) { updatedEntry.total = calculateStoppageTime(updatedEntry.from, updatedEntry.to); } // Handle responsible party change if (field === 'responsible') { if (value === 'reclamation') { updatedEntry.reason = ''; // Set to empty for reclamation (will show as "None") } // If changing to dredger, keep current reason or allow user to select } return updatedEntry; } return entry; })); }; const nextStep = (event?: React.MouseEvent) => { if (event) { event.preventDefault(); event.stopPropagation(); } // Check if we're on step 3 (Equipment & Time Sheet) and have zero equipment if (currentStep === 3) { const totalEquipment = parseInt(formData.statsDozers) + parseInt(formData.statsExc) + parseInt(formData.statsLoaders); if (totalEquipment === 0) { setShowZeroEquipmentConfirm(true); return; } } // console.log("Next step clicked, current step:", currentStep); if (currentStep < totalSteps) { setCurrentStep(currentStep + 1); // console.log("Moving to step:", currentStep + 1); } }; const confirmZeroEquipmentAndProceed = () => { setShowZeroEquipmentConfirm(false); if (currentStep < totalSteps) { setCurrentStep(currentStep + 1); } }; const cancelZeroEquipmentConfirm = () => { setShowZeroEquipmentConfirm(false); }; const prevStep = () => { if (currentStep > 1) { setCurrentStep(currentStep - 1); } }; const getStepTitle = (step: number) => { switch (step) { case 1: return "Basic Information"; case 2: return "Location & Pipeline Details"; case 3: return "Equipment & Time Sheet"; case 4: return "Stoppages & Notes"; default: return ""; } }; // Validation functions for each step const isStep1Valid = () => { return formData.shift && formData.areaId && formData.dredgerLocationId && formData.dredgerLineLength && !isNaN(parseInt(formData.dredgerLineLength)); }; const isStep2Valid = () => { return formData.reclamationLocationId && formData.shoreConnection && !isNaN(parseInt(formData.shoreConnection)); }; const isStep3Valid = () => { // Step 3 has no required fields - equipment and time sheet are optional return true; }; const isCurrentStepValid = () => { switch (currentStep) { case 1: return isStep1Valid(); case 2: return isStep2Valid(); case 3: return isStep3Valid(); case 4: return true; // Step 4 has no required fields default: return false; } }; return (
{/* Header */}

Create New Shifts

Fill out the operational shift details step by step

Back to Shifts
{/* Progress Steps */}
{[1, 2, 3, 4].map((step) => (
{step < currentStep ? ( ) : ( {step} )}
{step < totalSteps && (
)}
))}

{getStepTitle(currentStep)}

Step {currentStep} of {totalSteps}

{/* Form */}
{/* Step 1: Basic Information */} {currentStep === 1 && (
{actionData?.errors?.shift && (

{actionData.errors.shift}

)}
{actionData?.errors?.areaId && (

{actionData.errors.areaId}

)}
{actionData?.errors?.dredgerLocationId && (

{actionData.errors.dredgerLocationId}

)}
updateFormData('dredgerLineLength', e.target.value)} className="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500" placeholder="Enter length in meters" /> {actionData?.errors?.dredgerLineLength && (

{actionData.errors.dredgerLineLength}

)}
)} {/* Step 2: Location & Pipeline Details */} {currentStep === 2 && (
{actionData?.errors?.reclamationLocationId && (

{actionData.errors.reclamationLocationId}

)}
updateFormData('shoreConnection', e.target.value)} className="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500" placeholder="Enter connection length" /> {actionData?.errors?.shoreConnection && (

{actionData.errors.shoreConnection}

)}

Reclamation Height

updateFormData('reclamationHeightBase', e.target.value)} className="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500" />
updateFormData('reclamationHeightExtra', e.target.value)} className="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500" />

Pipeline Length

updateFormData('pipelineMain', e.target.value)} className="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500" />
updateFormData('pipelineExt1', e.target.value)} className="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500" />
updateFormData('pipelineReserve', e.target.value)} className="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500" />
updateFormData('pipelineExt2', e.target.value)} className="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500" />
)} {/* Step 3: Equipment & Time Sheet */} {currentStep === 3 && (

Equipment Statistics

updateFormData('statsLaborer', e.target.value)} className="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500" />

Time Sheet

{timeSheetEntries.length > 0 ? (
{timeSheetEntries.map((entry) => (
updateTimeSheetEntry(entry.id, 'from1', e.target.value)} className="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500" />
updateTimeSheetEntry(entry.id, 'to1', e.target.value)} className="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500" />
updateTimeSheetEntry(entry.id, 'from2', e.target.value)} className="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500" />
updateTimeSheetEntry(entry.id, 'to2', e.target.value)} className="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500" />
updateTimeSheetEntry(entry.id, 'reason', e.target.value)} className="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500" placeholder="Reason for downtime (if any)" />
))}
) : (

No time sheet entries yet. Click "Add Entry" to get started.

)}
)} {/* Step 4: Stoppages & Notes */} {currentStep === 4 && (

Dredger Stoppages

{stoppageEntries.length > 0 ? (
{stoppageEntries.map((entry) => (
updateStoppageEntry(entry.id, 'from', e.target.value)} className="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500" />
updateStoppageEntry(entry.id, 'to', e.target.value)} className="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500" />
updateStoppageEntry(entry.id, 'note', e.target.value)} className={`w-full px-3 py-2 border rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 ${entry.responsible === 'reclamation' && !entry.note.trim() ? 'border-red-300 bg-red-50' : 'border-gray-300'}`} placeholder={entry.responsible === 'reclamation' ? 'Notes required for reclamation stoppages' : 'Additional notes'} />
))}
) : (

No stoppages recorded. Click "Add Stoppage" if there were any.

)}