'use client'; // File: components/response-code/MatrixEditor.tsx // Visual editor สำหรับ Master Approval Matrix (T064, FR-022) import React, { useState } from 'react'; import { Check, X, AlertTriangle, Lock } from 'lucide-react'; import { Switch } from '@/components/ui/switch'; import { Badge } from '@/components/ui/badge'; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from '@/components/ui/table'; import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'; interface MatrixRule { publicId: string; responseCode: { publicId: string; code: string; descriptionEn: string; category: string; }; isEnabled: boolean; requiresComments: boolean; triggersNotification: boolean; isOverridden: boolean; isSystem?: boolean; } interface MatrixEditorProps { documentTypeCode: string; rules: MatrixRule[]; isProjectLevel?: boolean; onToggleEnabled: (rulePublicId: string, enabled: boolean) => void; onToggleRequiresComments: (rulePublicId: string, value: boolean) => void; onToggleNotification: (rulePublicId: string, value: boolean) => void; isLoading?: boolean; } const CATEGORY_ORDER = ['ENGINEERING', 'MATERIAL', 'CONTRACT', 'TESTING', 'ESG']; export function MatrixEditor({ documentTypeCode, rules, isProjectLevel = false, onToggleEnabled, onToggleRequiresComments, onToggleNotification, isLoading, }: MatrixEditorProps) { const [filter, setFilter] = useState('ALL'); const grouped = CATEGORY_ORDER.reduce>((acc, cat) => { const catRules = rules.filter( (r) => r.responseCode.category === cat && (filter === 'ALL' || r.responseCode.category === filter), ); if (catRules.length > 0) acc[cat] = catRules; return acc; }, {}); return (
Matrix: {documentTypeCode} {isProjectLevel && ( Project Override )}
Code Description Enabled Req. Comments Notify Status {Object.entries(grouped).map(([cat, catRules]) => ( {cat} {catRules.map((rule) => ( {rule.responseCode.code} {rule.responseCode.descriptionEn} {rule.isSystem ? ( ) : ( onToggleEnabled(rule.publicId, v)} disabled={isLoading} /> )} onToggleRequiresComments(rule.publicId, v)} disabled={isLoading || !rule.isEnabled} /> onToggleNotification(rule.publicId, v)} disabled={isLoading || !rule.isEnabled} /> {rule.isOverridden ? ( ) : rule.isEnabled ? ( ) : ( )} ))} ))} {Object.keys(grouped).length === 0 && ( No rules configured for this document type. )}
); }