'use client'; import { GenericCrudTable } from '@/components/admin/reference/generic-crud-table'; import { masterDataService } from '@/lib/services/master-data.service'; import { useContracts } from '@/hooks/use-master-data'; import { ColumnDef } from '@tanstack/react-table'; import { Discipline } from '@/types/master-data'; import { useState } from 'react'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select'; import { Contract, getContractPublicId } from '@/types/contract'; export default function DisciplinesPage() { const [selectedContractId, setSelectedContractId] = useState(null); const { data: contractsData = [] } = useContracts(); // Ensure we consistently use an array const contracts = (Array.isArray(contractsData) ? contractsData : []) as Contract[]; const columns: ColumnDef[] = [ { accessorKey: 'disciplineCode', header: 'Code', cell: ({ row }) => {row.getValue('disciplineCode')}, }, { accessorKey: 'contract', header: 'Contract', cell: ({ row }) => { const contract = row.original.contract; return contract ? ( {contract.contractName} ({contract.contractCode}) ) : ( - ); }, }, { accessorKey: 'codeNameTh', header: 'Name (TH)', }, { accessorKey: 'codeNameEn', header: 'Name (EN)', }, { accessorKey: 'isActive', header: 'Status', cell: ({ row }) => ( {row.getValue('isActive') ? 'Active' : 'Inactive'} ), }, ]; const contractOptions = contracts .map((c) => { const contractUuid = getContractPublicId(c); if (!contractUuid) { return null; } return { label: `${c.contractName} (${c.contractCode})`, value: contractUuid, }; }) .filter((option): option is { label: string; value: string } => option !== null); return (
{ const items = await masterDataService.getDisciplines(selectedContractId ? selectedContractId : undefined); // ADR-019: Map contract.publicId UUID for edit mode select matching return items.map((item) => { const rec = item as Discipline & { contract?: { publicId?: string }; contractId?: number | string }; return { ...item, contractId: rec.contract?.publicId || (rec.contractId ? String(rec.contractId) : null), } as Discipline; }); }} createFn={(data) => masterDataService.createDiscipline( data as unknown as Parameters[0] ) } updateFn={(id, data) => masterDataService.updateDiscipline(id, data)} deleteFn={(id) => masterDataService.deleteDiscipline(id)} columns={columns} filters={
} fields={[ { name: 'contractId', label: 'Contract', type: 'select', required: true, options: contractOptions, }, { name: 'disciplineCode', label: 'Code', type: 'text', required: true, }, { name: 'codeNameTh', label: 'Name (TH)', type: 'text', required: true, }, { name: 'codeNameEn', label: 'Name (EN)', type: 'text' }, { name: 'isActive', label: 'Active', type: 'checkbox' }, ]} />
); }