import type { LoaderFunctionArgs, MetaFunction } from "@remix-run/node"; import { json } from "@remix-run/node"; import { useLoaderData } from "@remix-run/react"; import { requireAuthLevel } from "~/utils/auth.server"; import DashboardLayout from "~/components/DashboardLayout"; import ReportSheetViewModal from "~/components/ReportSheetViewModal"; import { useState } from "react"; import { prisma } from "~/utils/db.server"; export const meta: MetaFunction = () => [{ title: "Report Sheets - Phosphat Report" }]; interface ReportSheet { id: string; date: string; area: string; dredgerLocation: string; reclamationLocation: string; status: string; dayReport?: any; nightReport?: any; } export const loader = async ({ request }: LoaderFunctionArgs) => { const user = await requireAuthLevel(request, 1); // Get sheets with related data let sheets = await prisma.sheet.findMany({ orderBy: { createdAt: 'desc' }, include: { area: { select: { name: true } }, dredgerLocation: { select: { name: true, class: true } }, reclamationLocation: { select: { name: true } }, dayShift: { include: { employee: { select: { name: true } }, area: { select: { name: true } }, dredgerLocation: { select: { name: true, class: true } }, reclamationLocation: { select: { name: true } } } }, nightShift: { include: { employee: { select: { name: true } }, area: { select: { name: true } }, dredgerLocation: { select: { name: true, class: true } }, reclamationLocation: { select: { name: true } } } } } }); // Filter sheets for level 1 users (only show sheets where user has at least one shift) if (user.authLevel === 1) { sheets = sheets.filter((sheet: any) => (sheet.dayShift && sheet.dayShift.employeeId === user.id) || (sheet.nightShift && sheet.nightShift.employeeId === user.id) ); } // Transform sheets to match the expected interface const transformedSheets = sheets.map((sheet: any) => ({ id: sheet.id.toString(), date: sheet.date, area: sheet.area.name, dredgerLocation: sheet.dredgerLocation.name, reclamationLocation: sheet.reclamationLocation.name, status: sheet.status, dayReport: sheet.dayShift, nightReport: sheet.nightShift })); return json({ user, sheets: transformedSheets }); }; export default function ReportSheet() { const { user, sheets } = useLoaderData(); const [viewingSheet, setViewingSheet] = useState(null); const [showViewModal, setShowViewModal] = useState(false); const handleView = (sheet: ReportSheet) => { setViewingSheet(sheet); setShowViewModal(true); }; const handleCloseViewModal = () => { setShowViewModal(false); setViewingSheet(null); }; const getShiftBadge = (shift: string) => { return shift === "day" ? "bg-yellow-100 text-yellow-800" : "bg-blue-100 text-blue-800"; }; const getShiftIcon = (shift: string) => { return shift === "day" ? ( ) : ( ); }; return (

Report Sheets

View grouped reports by location and date

{/* Report Sheets Table */}
{sheets.map((sheet) => ( ))}
Date Area Locations Available Shifts Status Employees Actions
{new Date(sheet.date).toLocaleDateString('en-GB')}
{sheet.area}
Dredger: {sheet.dredgerLocation}
Reclamation: {sheet.reclamationLocation}
{sheet.dayReport && ( {getShiftIcon('day')} Day )} {sheet.nightReport && ( {getShiftIcon('night')} Night )}
{sheet.status === 'completed' ? ( ) : ( )} {sheet.status.charAt(0).toUpperCase() + sheet.status.slice(1)}
{sheet.dayReport && (
Day: {sheet.dayReport.employee.name}
)} {sheet.nightReport && (
Night: {sheet.nightReport.employee.name}
)}
{sheets.length === 0 && (

No report sheets

Report sheets will appear here when reports are created.

)}
{/* View Modal */}
); }