776 lines
26 KiB
TypeScript
776 lines
26 KiB
TypeScript
import { useState, useMemo } from "react";
|
|
import { Link } from "react-router-dom";
|
|
import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
|
|
import type { ColumnDef } from "@tanstack/react-table";
|
|
import {
|
|
Plus,
|
|
Table as TableIcon,
|
|
Calendar as CalendarIcon,
|
|
CheckCircle2,
|
|
CalendarClock,
|
|
ChevronLeft,
|
|
ChevronRight,
|
|
Search,
|
|
Paperclip,
|
|
} from "lucide-react";
|
|
import {
|
|
pmApi,
|
|
type PMRecordItem,
|
|
type PMScheduleItem,
|
|
type ServiceMode,
|
|
type ScheduleStatus,
|
|
} from "@/api/pm";
|
|
import { departmentsApi } from "@/api/masters";
|
|
import { useAuthStore } from "@/hooks/useAuth";
|
|
import { can } from "@/lib/permissions";
|
|
import { formatDate } from "@/lib/format";
|
|
import { DataTable } from "@/components/DataTable";
|
|
import { Button } from "@/components/ui/button";
|
|
import { Badge } from "@/components/ui/badge";
|
|
import { Modal } from "@/components/ui/dialog";
|
|
import { ScheduleStatusBadge } from "@/components/StatusBadge";
|
|
import { Attachments } from "@/components/Attachments";
|
|
import {
|
|
NewPMScheduleModal,
|
|
CompletePMModal,
|
|
ReschedulePMModal,
|
|
} from "@/components/PMModals";
|
|
|
|
type ActiveTab = "records" | "schedules";
|
|
type ViewMode = "table" | "calendar";
|
|
|
|
function currentYearMonth(): string {
|
|
const d = new Date();
|
|
return `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, "0")}`;
|
|
}
|
|
|
|
function shiftYearMonth(ym: string, delta: number): string {
|
|
const [y, m] = ym.split("-").map(Number);
|
|
const d = new Date(y, m - 1 + delta, 1);
|
|
return `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, "0")}`;
|
|
}
|
|
|
|
function buildMonthGrid(ym: string) {
|
|
const [year, month] = ym.split("-").map(Number);
|
|
const firstDay = new Date(year, month - 1, 1);
|
|
const daysInMonth = new Date(year, month, 0).getDate();
|
|
// Convert Sunday=0..Saturday=6 into Monday=0..Sunday=6
|
|
const startOffset = (firstDay.getDay() + 6) % 7;
|
|
|
|
const cells: Array<{ dateStr: string | null; dayNum: number | null }> = [];
|
|
for (let i = 0; i < startOffset; i++) {
|
|
cells.push({ dateStr: null, dayNum: null });
|
|
}
|
|
for (let d = 1; d <= daysInMonth; d++) {
|
|
const dateStr = `${year}-${String(month).padStart(2, "0")}-${String(d).padStart(2, "0")}`;
|
|
cells.push({ dateStr, dayNum: d });
|
|
}
|
|
while (cells.length % 7 !== 0) {
|
|
cells.push({ dateStr: null, dayNum: null });
|
|
}
|
|
return cells;
|
|
}
|
|
|
|
const CHIP_COLOR: Record<string, string> = {
|
|
upcoming: "bg-blue-100 text-blue-800 border-blue-200",
|
|
due: "bg-amber-100 text-amber-800 border-amber-200",
|
|
overdue: "bg-red-100 text-red-800 border-red-200",
|
|
completed: "bg-green-100 text-green-800 border-green-200",
|
|
rescheduled: "bg-purple-100 text-purple-800 border-purple-200",
|
|
};
|
|
|
|
export function PMPage() {
|
|
const qc = useQueryClient();
|
|
const user = useAuthStore((s) => s.user);
|
|
const role = user?.role;
|
|
const isDeptUser = role === "department_user";
|
|
const canManagePm = can(role, "manage_pm") || role === "coordinator";
|
|
|
|
const [tab, setTab] = useState<ActiveTab>("records");
|
|
const [viewMode, setViewMode] = useState<ViewMode>("table");
|
|
const [monthFilter, setMonthFilter] = useState("");
|
|
const [departmentId, setDepartmentId] = useState("");
|
|
const [serviceMode, setServiceMode] = useState<ServiceMode | "">("");
|
|
const [statusFilter, setStatusFilter] = useState<ScheduleStatus | "">("");
|
|
const [q, setQ] = useState("");
|
|
|
|
const [newScheduleOpen, setNewScheduleOpen] = useState(false);
|
|
const [completingRec, setCompletingRec] = useState<PMRecordItem | null>(null);
|
|
const [reschedulingRec, setReschedulingRec] = useState<PMRecordItem | null>(null);
|
|
const [calendarDetailRec, setCalendarDetailRec] = useState<PMRecordItem | null>(null);
|
|
const [attachRec, setAttachRec] = useState<PMRecordItem | null>(null);
|
|
|
|
const effectiveCalendarMonth = monthFilter || currentYearMonth();
|
|
|
|
const deptsQuery = useQuery({
|
|
queryKey: ["departments", "active-only"],
|
|
queryFn: () => departmentsApi.list(false),
|
|
enabled: !isDeptUser,
|
|
});
|
|
|
|
const recordsQuery = useQuery({
|
|
queryKey: [
|
|
"pm-records",
|
|
viewMode === "calendar" ? effectiveCalendarMonth : monthFilter,
|
|
departmentId,
|
|
serviceMode,
|
|
statusFilter,
|
|
q,
|
|
],
|
|
queryFn: () =>
|
|
pmApi.listRecords({
|
|
q: q.trim() || undefined,
|
|
month:
|
|
viewMode === "calendar"
|
|
? effectiveCalendarMonth
|
|
: monthFilter || undefined,
|
|
department_id: !isDeptUser && departmentId ? Number(departmentId) : undefined,
|
|
service_mode: serviceMode || undefined,
|
|
status: statusFilter || undefined,
|
|
page_size: 200,
|
|
}),
|
|
});
|
|
|
|
const schedulesQuery = useQuery({
|
|
queryKey: ["pm-schedules", departmentId, serviceMode, q],
|
|
queryFn: () =>
|
|
pmApi.listSchedules({
|
|
q: q.trim() || undefined,
|
|
department_id: !isDeptUser && departmentId ? Number(departmentId) : undefined,
|
|
service_mode: serviceMode || undefined,
|
|
is_active: true,
|
|
page_size: 200,
|
|
}),
|
|
enabled: tab === "schedules",
|
|
});
|
|
|
|
const deactivateScheduleMutation = useMutation({
|
|
mutationFn: (id: number) => pmApi.deleteSchedule(id),
|
|
onSuccess: () => {
|
|
qc.invalidateQueries({ queryKey: ["pm-schedules"] });
|
|
},
|
|
});
|
|
|
|
const summary = recordsQuery.data?.summary ?? {
|
|
upcoming: 0,
|
|
due: 0,
|
|
overdue: 0,
|
|
completed_this_month: 0,
|
|
};
|
|
|
|
const recordColumns: ColumnDef<PMRecordItem, unknown>[] = [
|
|
{
|
|
accessorKey: "planned_date",
|
|
header: "Planned Date",
|
|
enableColumnFilter: false,
|
|
cell: ({ getValue }) => (
|
|
<span className="whitespace-nowrap font-medium">
|
|
{formatDate(getValue() as string)}
|
|
</span>
|
|
),
|
|
},
|
|
{
|
|
id: "asset_display",
|
|
accessorFn: (r) => `${r.asset_code} ${r.asset_name}`,
|
|
header: "Asset",
|
|
enableColumnFilter: true,
|
|
cell: ({ row }) => (
|
|
<Link
|
|
to={`/assets/${row.original.asset_id}`}
|
|
onClick={(e) => e.stopPropagation()}
|
|
className="hover:underline"
|
|
>
|
|
<div className="font-mono text-xs font-semibold text-[#1e3a8a]">
|
|
{row.original.asset_code}
|
|
</div>
|
|
<div className="text-xs text-gray-800">{row.original.asset_name}</div>
|
|
</Link>
|
|
),
|
|
},
|
|
{
|
|
accessorKey: "department_name",
|
|
header: "Department",
|
|
enableColumnFilter: true,
|
|
cell: ({ getValue }) => (getValue() as string | null) || "—",
|
|
},
|
|
{
|
|
accessorKey: "service_mode",
|
|
header: "Mode",
|
|
enableColumnFilter: true,
|
|
cell: ({ getValue }) => (
|
|
<Badge variant="outline">
|
|
{getValue() === "in_house" ? "In-House" : "Outsourced"}
|
|
</Badge>
|
|
),
|
|
},
|
|
{
|
|
id: "team_provider",
|
|
accessorFn: (r) => r.provider_name || r.responsible_team || "—",
|
|
header: "Team / Provider",
|
|
enableColumnFilter: true,
|
|
},
|
|
{
|
|
accessorKey: "status",
|
|
header: "Status",
|
|
enableColumnFilter: true,
|
|
cell: ({ row }) => (
|
|
<ScheduleStatusBadge
|
|
status={row.original.status}
|
|
severity={row.original.severity}
|
|
/>
|
|
),
|
|
},
|
|
{
|
|
accessorKey: "completed_date",
|
|
header: "Completed Date",
|
|
enableColumnFilter: false,
|
|
cell: ({ getValue }) => (
|
|
<span className="whitespace-nowrap text-xs">
|
|
{formatDate(getValue() as string | null)}
|
|
</span>
|
|
),
|
|
},
|
|
{
|
|
accessorKey: "service_details",
|
|
header: "Service Details",
|
|
cell: ({ getValue }) => (
|
|
<span className="text-xs text-gray-600 line-clamp-2">
|
|
{(getValue() as string | null) || "—"}
|
|
</span>
|
|
),
|
|
},
|
|
{
|
|
id: "attachments",
|
|
header: "Reports",
|
|
enableSorting: false,
|
|
enableColumnFilter: false,
|
|
cell: ({ row }) => (
|
|
<Button
|
|
size="sm"
|
|
variant="ghost"
|
|
onClick={() => setAttachRec(row.original)}
|
|
aria-label={`Attachments for PM ${row.original.id}`}
|
|
>
|
|
<Paperclip className="h-3.5 w-3.5 mr-1" />
|
|
Docs
|
|
</Button>
|
|
),
|
|
},
|
|
...(canManagePm
|
|
? [
|
|
{
|
|
id: "actions",
|
|
header: "Actions",
|
|
enableSorting: false,
|
|
enableColumnFilter: false,
|
|
cell: ({ row }: { row: { original: PMRecordItem } }) => {
|
|
const rec = row.original;
|
|
const actionable =
|
|
rec.status !== "completed" && rec.status !== "rescheduled";
|
|
if (!actionable) return <span className="text-xs text-gray-400">—</span>;
|
|
return (
|
|
<div className="flex items-center gap-1.5">
|
|
<Button
|
|
size="sm"
|
|
variant="success"
|
|
onClick={() => setCompletingRec(rec)}
|
|
>
|
|
<CheckCircle2 className="h-3.5 w-3.5 mr-1" />
|
|
Complete
|
|
</Button>
|
|
<Button
|
|
size="sm"
|
|
variant="outline"
|
|
onClick={() => setReschedulingRec(rec)}
|
|
>
|
|
<CalendarClock className="h-3.5 w-3.5 mr-1" />
|
|
Reschedule
|
|
</Button>
|
|
</div>
|
|
);
|
|
},
|
|
} satisfies ColumnDef<PMRecordItem, unknown>,
|
|
]
|
|
: []),
|
|
];
|
|
|
|
const scheduleColumns: ColumnDef<PMScheduleItem, unknown>[] = [
|
|
{
|
|
id: "asset_display",
|
|
accessorFn: (s) => `${s.asset_code} ${s.asset_name}`,
|
|
header: "Asset",
|
|
enableColumnFilter: true,
|
|
cell: ({ row }) => (
|
|
<Link to={`/assets/${row.original.asset_id}`} className="hover:underline">
|
|
<div className="font-mono text-xs font-semibold text-[#1e3a8a]">
|
|
{row.original.asset_code}
|
|
</div>
|
|
<div className="text-xs text-gray-800">{row.original.asset_name}</div>
|
|
</Link>
|
|
),
|
|
},
|
|
{
|
|
accessorKey: "department_name",
|
|
header: "Department",
|
|
enableColumnFilter: true,
|
|
cell: ({ getValue }) => (getValue() as string | null) || "—",
|
|
},
|
|
{
|
|
accessorKey: "service_mode",
|
|
header: "Service Mode",
|
|
enableColumnFilter: true,
|
|
cell: ({ getValue }) => (getValue() === "in_house" ? "In-House" : "Outsourced"),
|
|
},
|
|
{
|
|
accessorKey: "frequency_months",
|
|
header: "Frequency",
|
|
cell: ({ getValue }) => `Every ${String(getValue())} months`,
|
|
},
|
|
{
|
|
id: "team_provider",
|
|
accessorFn: (s) => s.provider_name || s.responsible_team || "—",
|
|
header: "Team / Provider",
|
|
enableColumnFilter: true,
|
|
},
|
|
{
|
|
accessorKey: "contract_summary",
|
|
header: "Contract",
|
|
cell: ({ getValue }) => (getValue() as string | null) || "—",
|
|
},
|
|
{
|
|
accessorKey: "next_planned_date",
|
|
header: "Next Planned",
|
|
cell: ({ getValue }) => formatDate(getValue() as string),
|
|
},
|
|
...(canManagePm
|
|
? [
|
|
{
|
|
id: "actions",
|
|
header: "Actions",
|
|
enableSorting: false,
|
|
enableColumnFilter: false,
|
|
cell: ({ row }: { row: { original: PMScheduleItem } }) => (
|
|
<Button
|
|
size="sm"
|
|
variant="ghost"
|
|
className="text-red-600 hover:bg-red-50"
|
|
disabled={deactivateScheduleMutation.isPending}
|
|
onClick={() => deactivateScheduleMutation.mutate(row.original.id)}
|
|
>
|
|
Deactivate
|
|
</Button>
|
|
),
|
|
} satisfies ColumnDef<PMScheduleItem, unknown>,
|
|
]
|
|
: []),
|
|
];
|
|
|
|
const recordsByDate = useMemo(() => {
|
|
const map = new Map<string, PMRecordItem[]>();
|
|
for (const r of recordsQuery.data?.items ?? []) {
|
|
const list = map.get(r.planned_date) ?? [];
|
|
list.push(r);
|
|
map.set(r.planned_date, list);
|
|
}
|
|
return map;
|
|
}, [recordsQuery.data?.items]);
|
|
|
|
const calendarCells = useMemo(
|
|
() => buildMonthGrid(effectiveCalendarMonth),
|
|
[effectiveCalendarMonth]
|
|
);
|
|
|
|
const departments = deptsQuery.data?.items ?? [];
|
|
|
|
return (
|
|
<div className="space-y-4">
|
|
<div className="flex flex-wrap items-center justify-between gap-3">
|
|
<div>
|
|
<h1 className="text-2xl font-bold text-gray-900">Preventive Maintenance</h1>
|
|
<p className="text-sm text-gray-500 mt-0.5">
|
|
Track in-house and outsourced PM schedules, due dates, and completion logs
|
|
</p>
|
|
</div>
|
|
{canManagePm && (
|
|
<Button onClick={() => setNewScheduleOpen(true)}>
|
|
<Plus className="h-4 w-4 mr-1.5" />
|
|
New PM Schedule
|
|
</Button>
|
|
)}
|
|
</div>
|
|
|
|
{/* KPI Strip */}
|
|
<div className="grid grid-cols-2 lg:grid-cols-4 gap-3">
|
|
<KpiCard label="Upcoming" value={summary.upcoming} color="text-blue-700 bg-blue-50 border-blue-200" />
|
|
<KpiCard label="Due" value={summary.due} color="text-amber-700 bg-amber-50 border-amber-200" />
|
|
<KpiCard label="Overdue" value={summary.overdue} color="text-red-700 bg-red-50 border-red-200" />
|
|
<KpiCard
|
|
label="Completed This Month"
|
|
value={summary.completed_this_month}
|
|
color="text-green-700 bg-green-50 border-green-200"
|
|
/>
|
|
</div>
|
|
|
|
{/* Filter Bar */}
|
|
<div className="bg-white border border-gray-200 rounded-lg p-3.5 grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-6 gap-2.5">
|
|
<div className="relative lg:col-span-2">
|
|
<Search className="absolute left-2.5 top-1/2 -translate-y-1/2 h-3.5 w-3.5 text-gray-400" />
|
|
<input
|
|
type="text"
|
|
value={q}
|
|
onChange={(e) => setQ(e.target.value)}
|
|
placeholder="Search asset code, name, team..."
|
|
className="w-full pl-8 pr-3 py-1.5 text-xs border border-gray-200 rounded-md"
|
|
/>
|
|
</div>
|
|
|
|
<div className="flex items-center gap-1">
|
|
<input
|
|
type="month"
|
|
aria-label="Filter by Month"
|
|
value={monthFilter}
|
|
onChange={(e) => setMonthFilter(e.target.value)}
|
|
className="flex-1 px-2.5 py-1.5 text-xs border border-gray-200 rounded-md"
|
|
/>
|
|
{monthFilter && (
|
|
<button
|
|
type="button"
|
|
onClick={() => setMonthFilter("")}
|
|
className="px-2 py-1 text-xs border border-gray-200 rounded hover:bg-gray-50"
|
|
>
|
|
All
|
|
</button>
|
|
)}
|
|
</div>
|
|
|
|
{!isDeptUser && (
|
|
<select
|
|
value={departmentId}
|
|
onChange={(e) => setDepartmentId(e.target.value)}
|
|
aria-label="Filter by Department"
|
|
className="px-2.5 py-1.5 text-xs border border-gray-200 rounded-md bg-white"
|
|
>
|
|
<option value="">All Departments</option>
|
|
{departments.map((d) => (
|
|
<option key={d.id} value={String(d.id)}>
|
|
{d.name}
|
|
</option>
|
|
))}
|
|
</select>
|
|
)}
|
|
|
|
<select
|
|
value={serviceMode}
|
|
onChange={(e) => setServiceMode(e.target.value as ServiceMode | "")}
|
|
aria-label="Filter by Service Mode"
|
|
className="px-2.5 py-1.5 text-xs border border-gray-200 rounded-md bg-white"
|
|
>
|
|
<option value="">All Modes</option>
|
|
<option value="in_house">In-House</option>
|
|
<option value="outsourced">Outsourced</option>
|
|
</select>
|
|
|
|
<select
|
|
value={statusFilter}
|
|
onChange={(e) => setStatusFilter(e.target.value as ScheduleStatus | "")}
|
|
aria-label="Filter by Status"
|
|
className="px-2.5 py-1.5 text-xs border border-gray-200 rounded-md bg-white"
|
|
>
|
|
<option value="">All Statuses</option>
|
|
<option value="upcoming">Upcoming</option>
|
|
<option value="due">Due</option>
|
|
<option value="overdue">Overdue</option>
|
|
<option value="completed">Completed</option>
|
|
<option value="rescheduled">Rescheduled</option>
|
|
</select>
|
|
</div>
|
|
|
|
{/* Tabs + View Switcher */}
|
|
<div className="flex flex-wrap items-center justify-between gap-3 border-b border-gray-200 pb-2">
|
|
<div className="flex gap-1">
|
|
<button
|
|
type="button"
|
|
onClick={() => setTab("records")}
|
|
className={`px-4 py-2 text-sm font-medium border-b-2 -mb-[9px] transition-colors ${
|
|
tab === "records"
|
|
? "border-[#1e3a8a] text-[#1e3a8a]"
|
|
: "border-transparent text-gray-500 hover:text-gray-800"
|
|
}`}
|
|
>
|
|
PM Records
|
|
</button>
|
|
<button
|
|
type="button"
|
|
onClick={() => setTab("schedules")}
|
|
className={`px-4 py-2 text-sm font-medium border-b-2 -mb-[9px] transition-colors ${
|
|
tab === "schedules"
|
|
? "border-[#1e3a8a] text-[#1e3a8a]"
|
|
: "border-transparent text-gray-500 hover:text-gray-800"
|
|
}`}
|
|
>
|
|
Active Schedules
|
|
</button>
|
|
</div>
|
|
|
|
{tab === "records" && (
|
|
<div className="inline-flex rounded-md border border-gray-200 bg-white p-0.5">
|
|
<button
|
|
type="button"
|
|
onClick={() => setViewMode("table")}
|
|
className={`inline-flex items-center gap-1.5 px-3 py-1 rounded text-xs font-medium transition-colors ${
|
|
viewMode === "table"
|
|
? "bg-[#1e3a8a] text-white"
|
|
: "text-gray-600 hover:bg-gray-50"
|
|
}`}
|
|
>
|
|
<TableIcon className="h-3.5 w-3.5" />
|
|
Table View
|
|
</button>
|
|
<button
|
|
type="button"
|
|
onClick={() => setViewMode("calendar")}
|
|
className={`inline-flex items-center gap-1.5 px-3 py-1 rounded text-xs font-medium transition-colors ${
|
|
viewMode === "calendar"
|
|
? "bg-[#1e3a8a] text-white"
|
|
: "text-gray-600 hover:bg-gray-50"
|
|
}`}
|
|
>
|
|
<CalendarIcon className="h-3.5 w-3.5" />
|
|
Calendar View
|
|
</button>
|
|
</div>
|
|
)}
|
|
</div>
|
|
|
|
{/* Main Content */}
|
|
{tab === "records" && viewMode === "table" && (
|
|
<DataTable
|
|
data={recordsQuery.data?.items ?? []}
|
|
columns={recordColumns}
|
|
loading={recordsQuery.isLoading}
|
|
globalFilterPlaceholder="Quick filter PM records..."
|
|
exportFilename="pm-records"
|
|
emptyMessage="No PM records match the current filters."
|
|
/>
|
|
)}
|
|
|
|
{tab === "records" && viewMode === "calendar" && (
|
|
<div className="bg-white border border-gray-200 rounded-xl p-4 space-y-3">
|
|
<div className="flex items-center justify-between">
|
|
<h2 className="text-sm font-semibold text-gray-800">
|
|
PM Calendar — {effectiveCalendarMonth}
|
|
</h2>
|
|
<div className="flex items-center gap-1.5">
|
|
<Button
|
|
size="sm"
|
|
variant="outline"
|
|
onClick={() => setMonthFilter(shiftYearMonth(effectiveCalendarMonth, -1))}
|
|
>
|
|
<ChevronLeft className="h-4 w-4" />
|
|
</Button>
|
|
<Button
|
|
size="sm"
|
|
variant="outline"
|
|
onClick={() => setMonthFilter(currentYearMonth())}
|
|
>
|
|
Today
|
|
</Button>
|
|
<Button
|
|
size="sm"
|
|
variant="outline"
|
|
onClick={() => setMonthFilter(shiftYearMonth(effectiveCalendarMonth, 1))}
|
|
>
|
|
<ChevronRight className="h-4 w-4" />
|
|
</Button>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="overflow-x-auto">
|
|
<div className="min-w-[640px]">
|
|
<div className="grid grid-cols-7 gap-1 text-center text-xs font-semibold text-gray-500 pb-1">
|
|
{["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"].map((d) => (
|
|
<div key={d} className="py-1">
|
|
{d}
|
|
</div>
|
|
))}
|
|
</div>
|
|
|
|
<div className="grid grid-cols-7 gap-1">
|
|
{calendarCells.map((cell, idx) => {
|
|
const items = cell.dateStr ? recordsByDate.get(cell.dateStr) ?? [] : [];
|
|
return (
|
|
<div
|
|
key={idx}
|
|
className={`min-h-24 rounded-md border p-1.5 text-left ${
|
|
cell.dayNum ? "bg-white border-gray-200" : "bg-gray-50 border-gray-100"
|
|
}`}
|
|
>
|
|
{cell.dayNum && (
|
|
<>
|
|
<div className="text-[11px] font-semibold text-gray-500 mb-1">
|
|
{cell.dayNum}
|
|
</div>
|
|
<div className="space-y-1">
|
|
{items.map((item) => (
|
|
<button
|
|
key={item.id}
|
|
type="button"
|
|
onClick={() => setCalendarDetailRec(item)}
|
|
className={`w-full text-left px-1.5 py-0.5 rounded border text-[10px] font-medium truncate ${
|
|
CHIP_COLOR[item.status] ?? "bg-gray-100 text-gray-700"
|
|
}`}
|
|
title={`${item.asset_code} — ${item.asset_name} (${item.status})`}
|
|
>
|
|
{item.asset_code}
|
|
</button>
|
|
))}
|
|
</div>
|
|
</>
|
|
)}
|
|
</div>
|
|
);
|
|
})}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
)}
|
|
|
|
{tab === "schedules" && (
|
|
<DataTable
|
|
data={schedulesQuery.data?.items ?? []}
|
|
columns={scheduleColumns}
|
|
loading={schedulesQuery.isLoading}
|
|
globalFilterPlaceholder="Quick filter PM schedules..."
|
|
exportFilename="pm-schedules"
|
|
emptyMessage="No active PM schedules found."
|
|
/>
|
|
)}
|
|
|
|
{/* Calendar Item Quick Detail Modal */}
|
|
<Modal
|
|
open={Boolean(calendarDetailRec)}
|
|
onOpenChange={(o) => !o && setCalendarDetailRec(null)}
|
|
title={
|
|
calendarDetailRec
|
|
? `${calendarDetailRec.asset_code} — ${calendarDetailRec.asset_name}`
|
|
: "PM Details"
|
|
}
|
|
>
|
|
{calendarDetailRec && (
|
|
<div className="space-y-3 text-sm">
|
|
<div className="flex items-center gap-2">
|
|
<ScheduleStatusBadge
|
|
status={calendarDetailRec.status}
|
|
severity={calendarDetailRec.severity}
|
|
/>
|
|
<Badge variant="outline">
|
|
{calendarDetailRec.service_mode === "in_house" ? "In-House" : "Outsourced"}
|
|
</Badge>
|
|
</div>
|
|
<div className="grid grid-cols-2 gap-2 text-xs">
|
|
<div>
|
|
<span className="text-gray-500">Planned Date:</span>{" "}
|
|
<strong>{formatDate(calendarDetailRec.planned_date)}</strong>
|
|
</div>
|
|
<div>
|
|
<span className="text-gray-500">Department:</span>{" "}
|
|
<strong>{calendarDetailRec.department_name || "—"}</strong>
|
|
</div>
|
|
<div>
|
|
<span className="text-gray-500">Team / Provider:</span>{" "}
|
|
<strong>
|
|
{calendarDetailRec.provider_name ||
|
|
calendarDetailRec.responsible_team ||
|
|
"—"}
|
|
</strong>
|
|
</div>
|
|
<div>
|
|
<span className="text-gray-500">Completed:</span>{" "}
|
|
<strong>{formatDate(calendarDetailRec.completed_date)}</strong>
|
|
</div>
|
|
</div>
|
|
<div className="flex justify-end gap-2 pt-2">
|
|
<Link to={`/assets/${calendarDetailRec.asset_id}`}>
|
|
<Button variant="outline" size="sm">
|
|
View Asset
|
|
</Button>
|
|
</Link>
|
|
{canManagePm &&
|
|
calendarDetailRec.status !== "completed" &&
|
|
calendarDetailRec.status !== "rescheduled" && (
|
|
<>
|
|
<Button
|
|
size="sm"
|
|
variant="outline"
|
|
onClick={() => {
|
|
setReschedulingRec(calendarDetailRec);
|
|
setCalendarDetailRec(null);
|
|
}}
|
|
>
|
|
Reschedule
|
|
</Button>
|
|
<Button
|
|
size="sm"
|
|
variant="success"
|
|
onClick={() => {
|
|
setCompletingRec(calendarDetailRec);
|
|
setCalendarDetailRec(null);
|
|
}}
|
|
>
|
|
Complete
|
|
</Button>
|
|
</>
|
|
)}
|
|
</div>
|
|
</div>
|
|
)}
|
|
</Modal>
|
|
|
|
<NewPMScheduleModal open={newScheduleOpen} onClose={() => setNewScheduleOpen(false)} />
|
|
|
|
<CompletePMModal
|
|
open={Boolean(completingRec)}
|
|
onClose={() => setCompletingRec(null)}
|
|
recordId={completingRec?.id ?? null}
|
|
assetLabel={completingRec ? `${completingRec.asset_code} ${completingRec.asset_name}` : ""}
|
|
assetId={completingRec?.asset_id}
|
|
/>
|
|
|
|
<ReschedulePMModal
|
|
open={Boolean(reschedulingRec)}
|
|
onClose={() => setReschedulingRec(null)}
|
|
recordId={reschedulingRec?.id ?? null}
|
|
assetLabel={
|
|
reschedulingRec ? `${reschedulingRec.asset_code} ${reschedulingRec.asset_name}` : ""
|
|
}
|
|
assetId={reschedulingRec?.asset_id}
|
|
/>
|
|
|
|
{attachRec && (
|
|
<Modal
|
|
open={Boolean(attachRec)}
|
|
onOpenChange={(o) => !o && setAttachRec(null)}
|
|
title={`PM Service Reports — ${attachRec.asset_code} (${attachRec.asset_name})`}
|
|
>
|
|
<Attachments
|
|
entityType="pm_record"
|
|
entityId={attachRec.id}
|
|
canUpload={canManagePm}
|
|
canDelete={canManagePm}
|
|
/>
|
|
</Modal>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
function KpiCard({ label, value, color }: { label: string; value: number; color: string }) {
|
|
return (
|
|
<div className={`rounded-xl border p-4 ${color}`}>
|
|
<div className="text-xs font-medium opacity-80">{label}</div>
|
|
<div className="text-2xl font-bold mt-1">{value}</div>
|
|
</div>
|
|
);
|
|
}
|