"use client"; import { useEffect, useState } from "react"; import { ColumnDef, flexRender, getCoreRowModel, getSortedRowModel, useReactTable, SortingState, } from "@tanstack/react-table"; import * as XLSX from "xlsx"; import { Dialog, DialogContent, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from "@/components/ui/table"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { getTerraTechFacilitySummary, TerraTechSummaryRow } from "@/app/actions/imports"; import { Fuel, AlertTriangle, Download } from "lucide-react"; interface TerraTechSummaryDialogProps { open: boolean; onOpenChange: (open: boolean) => void; importId: number; } interface SummaryData { importId: number; importName: string; layoutName: string; rows: TerraTechSummaryRow[]; } function formatNumber(value: number | string | null | undefined, decimals: number = 2): string { if (value === null || value === undefined) return '-'; const num = typeof value === 'string' ? parseFloat(value) : value; if (isNaN(num)) return '-'; return num.toLocaleString('en-US', { minimumFractionDigits: decimals, maximumFractionDigits: decimals }); } function formatPercent(value: number | string | null | undefined): string { if (value === null || value === undefined) return '-'; const num = typeof value === 'string' ? parseFloat(value) : value; if (isNaN(num)) return '-'; return `${num.toFixed(1)}%`; } const columns: ColumnDef[] = [ { accessorKey: "wellName", header: "Well Name", cell: ({ row }) => (
{row.getValue("wellName") || '-'}
), }, { accessorKey: "corpId", header: "CorpID", cell: ({ row }) => row.getValue("corpId") || '-', }, { accessorKey: "facilityId", header: "Facility ID", cell: ({ row }) => { const value = row.getValue("facilityId") as string | null; return value || -; }, }, { accessorKey: "gas", header: "Gas", cell: ({ row }) => (
{formatNumber(row.getValue("gas"))}
), }, { accessorKey: "oil", header: "Oil", cell: ({ row }) => (
{formatNumber(row.getValue("oil"))}
), }, { accessorKey: "water", header: "Water", cell: ({ row }) => (
{formatNumber(row.getValue("water"))}
), }, { accessorKey: "state", header: "State", cell: ({ row }) => row.getValue("state") || '-', }, { accessorKey: "county", header: "County", cell: ({ row }) => { const value = row.getValue("county") as string | null; return value || -; }, }, { accessorKey: "daysQ1", header: "Days Q1", cell: ({ row }) => (
{formatNumber(row.getValue("daysQ1"), 0)}
), }, { accessorKey: "daysQ2", header: "Days Q2", cell: ({ row }) => (
{formatNumber(row.getValue("daysQ2"), 0)}
), }, { accessorKey: "daysQ3", header: "Days Q3", cell: ({ row }) => (
{formatNumber(row.getValue("daysQ3"), 0)}
), }, { accessorKey: "daysQ4", header: "Days Q4", cell: ({ row }) => (
{formatNumber(row.getValue("daysQ4"), 0)}
), }, { accessorKey: "daysQ1Pct", header: "Q1 %", cell: ({ row }) => (
{formatPercent(row.getValue("daysQ1Pct"))}
), }, { accessorKey: "daysQ2Pct", header: "Q2 %", cell: ({ row }) => (
{formatPercent(row.getValue("daysQ2Pct"))}
), }, { accessorKey: "daysQ3Pct", header: "Q3 %", cell: ({ row }) => (
{formatPercent(row.getValue("daysQ3Pct"))}
), }, { accessorKey: "daysQ4Pct", header: "Q4 %", cell: ({ row }) => (
{formatPercent(row.getValue("daysQ4Pct"))}
), }, { accessorKey: "gasCorr", header: "Gas-corr", cell: ({ row }) => (
{formatNumber(row.getValue("gasCorr"))}
), }, { accessorKey: "oilCorr", header: "Oil-corr", cell: ({ row }) => (
{formatNumber(row.getValue("oilCorr"))}
), }, { accessorKey: "waterCorr", header: "Water-corr", cell: ({ row }) => (
{formatNumber(row.getValue("waterCorr"))}
), }, { accessorKey: "isMissingFacilityId", header: "Missing Facility ID", cell: ({ row }) => { const value = row.getValue("isMissingFacilityId"); return value ? Yes : null; }, }, { accessorKey: "isMissingCounty", header: "Missing County", cell: ({ row }) => { const value = row.getValue("isMissingCounty"); return value ? Yes : null; }, }, ]; function SummaryTable({ data, importName }: { data: TerraTechSummaryRow[]; importName: string }) { const [sorting, setSorting] = useState([ { id: "wellName", desc: false }, ]); const table = useReactTable({ data, columns, state: { sorting, }, onSortingChange: setSorting, getCoreRowModel: getCoreRowModel(), getSortedRowModel: getSortedRowModel(), }); function exportToExcel() { // Prepare data for Excel export const exportData = data.map(row => ({ "Well Name": row.wellName || '', "CorpID": row.corpId || '', "Facility ID": row.facilityId || '', "Gas": row.gas, "Oil": row.oil, "Water": row.water, "State": row.state || '', "County": row.county || '', "Days Q1": row.daysQ1, "Days Q2": row.daysQ2, "Days Q3": row.daysQ3, "Days Q4": row.daysQ4, "Q1 %": row.daysQ1Pct, "Q2 %": row.daysQ2Pct, "Q3 %": row.daysQ3Pct, "Q4 %": row.daysQ4Pct, "Gas-corr": row.gasCorr, "Oil-corr": row.oilCorr, "Water-corr": row.waterCorr, "Missing Facility ID": row.isMissingFacilityId || '', "Missing County": row.isMissingCounty || '', })); const worksheet = XLSX.utils.json_to_sheet(exportData); const workbook = XLSX.utils.book_new(); XLSX.utils.book_append_sheet(workbook, worksheet, "Facility Summary"); // Generate filename with import name and date const date = new Date().toISOString().split('T')[0]; const filename = `${importName.replace(/[^a-z0-9]/gi, '_')}_Summary_${date}.xlsx`; XLSX.writeFile(workbook, filename); } return (
{data.length} records
{table.getHeaderGroups().map((headerGroup) => ( {headerGroup.headers.map((header) => (
{flexRender( header.column.columnDef.header, header.getContext() )} {header.column.getIsSorted() && ( {header.column.getIsSorted() === "asc" ? "↑" : "↓"} )}
))}
))}
{table.getRowModel().rows?.length ? ( table.getRowModel().rows.map((row) => ( {row.getVisibleCells().map((cell) => ( {flexRender(cell.column.columnDef.cell, cell.getContext())} ))} )) ) : ( No summary data available for this import. )}
); } export function TerraTechSummaryDialog({ open, onOpenChange, importId }: TerraTechSummaryDialogProps) { const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const [summaryData, setSummaryData] = useState(null); useEffect(() => { if (open && importId) { loadSummary(); } // eslint-disable-next-line react-hooks/exhaustive-deps }, [open, importId]); async function loadSummary() { setLoading(true); setError(null); try { const result = await getTerraTechFacilitySummary(importId); if (result.success && result.data) { setSummaryData(result.data); } else { setError(result.error || 'Failed to load summary'); } } catch { setError('An unexpected error occurred'); } finally { setLoading(false); } } return (
Facility Summary {summaryData && ( {summaryData.importName} )}
{loading && (
)} {error && (

{error}

)} {!loading && !error && summaryData && ( )}
); }