'use client'; /* IMPORT */ import { Badge } from '@/components/ui/badge'; import { Button } from '@/components/ui/button'; import { Checkbox } from '@/components/ui/checkbox'; import { CircleCheckBig, CircleX, Ellipsis, Handshake, OctagonAlert } from 'lucide-react'; import { DataTableColumnHeaderSimple } from '@/components/data-table/data-table-column-simple-header'; import { Dispatch, SetStateAction } from 'react'; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger, } from '@/components/ui/dropdown-menu'; import { RISK_RATING_CASHFLOW_LIST, RISK_RATING_CREDIT_LIST, RISK_RATING_TOTAL_LIST, RISK_RATING_WATCH_LIST } from '@/config/risksConfig'; import { type AppRouterInstance } from 'next/dist/shared/lib/app-router-context.shared-runtime'; import { type ColumnDef } from '@tanstack/react-table'; import { type DataTableRowAction } from '@/types/table'; import { type RisksView } from '@/db/schema'; // ---------------------------------------------------------------------------------------------------- /* TYPES */ interface GetColumnsProps { setRowAction: Dispatch | null>>; router: AppRouterInstance; paramsLanguage: string; }; // ---------------------------------------------------------------------------------------------------- /* FUNCTION FOR GETTING COLUMNS SETTING */ function getColumns(props: GetColumnsProps): ColumnDef[] { const { setRowAction, router, paramsLanguage } = props; // [1] SELECT COLUMN - CHECKBOX const selectColumn: ColumnDef = { id: 'select', header: ({ table }) => ( table.toggleAllPageRowsSelected(!!value)} aria-label="select-all" className="translate-y-0.5" /> ), cell: ({ row }) => ( row.toggleSelected(!!value)} aria-label="select-row" className="translate-y-0.5" /> ), enableSorting: false, enableHiding: false, }; // [2] SOURCE COLUMNS const sourceColumns: ColumnDef[] = [ { accessorKey: 'eventType', header: ({ column }) => ( ), cell: ({ row }) => { const value = row.getValue('eventType'); return ( {value} ); }, enableSorting: true, enableHiding: false, meta: { excelHeader: 'Risk Category', group: 'Risk Information', type: 'select', }, }, { accessorKey: 'vendorCode', header: ({ column }) => ( ), cell: ({ row }) => { const value = row.getValue('vendorCode'); return (
{value}
); }, enableSorting: true, enableHiding: false, meta: { excelHeader: 'Vendor Code', group: 'Risk Information', type: 'text', }, }, { accessorKey: 'vendorName', header: ({ column }) => ( ), cell: ({ row }) => { const value = row.getValue('vendorName'); return (
{value}
); }, enableSorting: true, enableHiding: false, meta: { excelHeader: 'Vendor Name', group: 'Risk Information', type: 'text', }, }, { accessorKey: 'businessNumber', header: ({ column }) => ( ), cell: ({ row }) => { const value = row.getValue('businessNumber'); const digits = value.replace(/\D/g, ''); const formattedValue = digits.length === 10 ? `${digits.slice(0, 3)}-${digits.slice(3, 5)}-${digits.slice(5)}` : value; return (
{formattedValue}
); }, enableSorting: true, enableHiding: false, meta: { excelHeader: 'Business Number', group: 'Risk Information', type: 'text', }, }, { accessorKey: 'provider', header: ({ column }) => ( ), cell: ({ row }) => { const value = row.getValue('provider'); return ( {value} ); }, enableSorting: true, enableHiding: false, meta: { excelHeader: 'Provider', group: 'Risk Information', type: 'text', }, }, { accessorKey: 'content', header: ({ column }) => ( ), cell: ({ row }) => { const value = row.getValue('content'); return (
{value}
); }, enableSorting: true, enableHiding: false, meta: { excelHeader: 'Risk Content', group: 'Risk Information', type: 'text', }, }, { accessorKey: 'occuredAt', header: ({ column }) => ( ), cell: ({ row }) => { const date = row.getValue('occuredAt'); return (
{date ? new Date(date).toLocaleDateString('ko-KR') : '-'}
); }, enableSorting: true, enableHiding: false, meta: { excelHeader: 'Occured At', group: 'Risk Information', type: 'date', }, }, ]; // [3] TOTAL RATING COLUMNS const ratingTotalColumns: ColumnDef[] = [ { accessorKey: 'prevRatingTotal', header: ({ column }) => ( ), cell: ({ row }) => { const value = row.getValue('prevRatingTotal'); return (
{value}
); }, enableSorting: true, enableHiding: false, meta: { excelHeader: 'Previous', group: 'Total Rating', type: 'text', }, }, { accessorKey: 'curRatingTotal', header: ({ column }) => ( ), cell: ({ row }) => { const value = row.getValue('curRatingTotal'); return (
{value}
); }, enableSorting: true, enableHiding: false, meta: { excelHeader: 'Current', group: 'Total Rating', type: 'text', }, }, { accessorKey: 'changeRatingTotal', header: ({ column }) => ( ), cell: ({ row }) => { const prevValue = row.getValue('prevRatingTotal'); const curValue = row.getValue('curRatingTotal'); if (prevValue === '-' || curValue === '-' || !prevValue || !curValue) { return (
-
); } const prevRating = RISK_RATING_TOTAL_LIST.find(rating => rating.label === prevValue); const curRating = RISK_RATING_TOTAL_LIST.find(rating => rating.label === curValue); if (!prevRating || !curRating || prevRating === curRating) { return (
-
); } else if (prevRating.value > curRating.value) { return (
); } else { return (
); } }, enableSorting: true, enableHiding: false, meta: { excelHeader: 'Change', group: 'Total Rating', type: 'text', }, }, ]; // [4] CREDIT RATING COLUMNS const ratingCreditColumns: ColumnDef[] = [ { accessorKey: 'prevRatingCredit', header: ({ column }) => ( ), cell: ({ row }) => { const value = row.getValue('prevRatingCredit'); return (
{value}
); }, enableSorting: true, enableHiding: false, meta: { excelHeader: 'Previous', group: 'Credit Rating', type: 'text', }, }, { accessorKey: 'curRatingCredit', header: ({ column }) => ( ), cell: ({ row }) => { const value = row.getValue('curRatingCredit'); return (
{value}
); }, enableSorting: true, enableHiding: false, meta: { excelHeader: 'Current', group: 'Credit Rating', type: 'text', }, }, { accessorKey: 'changeRatingCredit', header: ({ column }) => ( ), cell: ({ row }) => { const prevValue = row.getValue('prevRatingCredit'); const curValue = row.getValue('curRatingCredit'); if (prevValue === '-' || curValue === '-' || !prevValue || !curValue) { return (
-
); } const prevRating = RISK_RATING_CREDIT_LIST.find(rating => rating.label === prevValue); const curRating = RISK_RATING_CREDIT_LIST.find(rating => rating.label === curValue); if (!prevRating || !curRating || prevRating === curRating) { return (
-
); } else if (prevRating.value > curRating.value) { return (
); } else { return (
); } }, enableSorting: true, enableHiding: false, meta: { excelHeader: 'Change', group: 'Credit Rating', type: 'text', }, }, ]; // [5] CASHFLOW RATING COLUMNS const ratingCashflowColumns: ColumnDef[] = [ { accessorKey: 'prevRatingCashflow', header: ({ column }) => ( ), cell: ({ row }) => { const value = row.getValue('prevRatingCashflow'); return (
{value}
); }, enableSorting: true, enableHiding: false, meta: { excelHeader: 'Previous', group: 'Cashflow Rating', type: 'text', }, }, { accessorKey: 'curRatingCashflow', header: ({ column }) => ( ), cell: ({ row }) => { const value = row.getValue('curRatingCashflow'); return (
{value}
); }, enableSorting: true, enableHiding: false, meta: { excelHeader: 'Current', group: 'Cashflow Rating', type: 'text', }, }, { accessorKey: 'changeRatingCashflow', header: ({ column }) => ( ), cell: ({ row }) => { const prevValue = row.getValue('prevRatingCashflow'); const curValue = row.getValue('curRatingCashflow'); if (prevValue === '-' || curValue === '-' || !prevValue || !curValue) { return (
-
); } const prevRating = RISK_RATING_CASHFLOW_LIST.find(rating => rating.label === prevValue); const curRating = RISK_RATING_CASHFLOW_LIST.find(rating => rating.label === curValue); if (!prevRating || !curRating || prevRating === curRating) { return (
-
); } else if (prevRating.value > curRating.value) { return (
); } else { return (
); } }, enableSorting: true, enableHiding: false, meta: { excelHeader: 'Change', group: 'Cashflow Rating', type: 'text', }, }, ]; // [6] WATCH RATING COLUMNS const ratingWatchColumns: ColumnDef[] = [ { accessorKey: 'prevRatingWatch', header: ({ column }) => ( ), cell: ({ row }) => { const value = row.getValue('prevRatingWatch'); return (
{value}
); }, enableSorting: true, enableHiding: false, meta: { excelHeader: 'Previous', group: 'Watch Rating', type: 'text', }, }, { accessorKey: 'curRatingWatch', header: ({ column }) => ( ), cell: ({ row }) => { const value = row.getValue('curRatingWatch'); return (
{value}
); }, enableSorting: true, enableHiding: false, meta: { excelHeader: 'Current', group: 'Watch Rating', type: 'text', }, }, { accessorKey: 'changeRatingWatch', header: ({ column }) => ( ), cell: ({ row }) => { const prevValue = row.getValue('prevRatingWatch'); const curValue = row.getValue('curRatingWatch'); if (prevValue === '-' || curValue === '-' || !prevValue || !curValue) { return (
-
); } const prevRating = RISK_RATING_WATCH_LIST.find(rating => rating.label === prevValue); const curRating = RISK_RATING_WATCH_LIST.find(rating => rating.label === curValue); if (!prevRating || !curRating || prevRating === curRating) { return (
-
); } else if (prevRating.value > curRating.value) { return (
); } else { return (
); } }, enableSorting: true, enableHiding: false, meta: { excelHeader: 'Change', group: 'Watch Rating', type: 'text', }, }, ]; // [7] INPUT COLUMNS const inputColumns: ColumnDef[] = [ { accessorKey: 'eventStatus', header: ({ column }) => ( ), cell: ({ row }) => { const value = row.getValue('eventStatus'); if (value) { return (
아니오
); } return (
); }, enableSorting: true, enableHiding: false, meta: { excelHeader: 'Risk Not Cleared', group: 'Risk Management', type: 'text', }, }, { accessorKey: 'managerName', header: ({ column }) => ( ), cell: ({ row }) => { const value = row.getValue('managerName') ?? '-'; return (
{value}
); }, enableSorting: true, enableHiding: false, meta: { excelHeader: 'Procurement Manager Name', group: 'Risk Management', type: 'text', }, }, { accessorKey: 'adminComment', header: ({ column }) => ( ), cell: ({ row }) => { const value = row.getValue('adminComment') ?? '-'; return (
{value}
); }, enableSorting: true, enableHiding: false, meta: { excelHeader: 'Risk Manager Comment', group: 'Risk Management', type: 'text', }, }, ]; // [8] ACTIONS COLUMN - DROPDOWN MENU WITH VIEW ACTION const actionsColumn: ColumnDef = { id: 'actions', enableHiding: false, cell: function Cell({ row }) { return ( setRowAction({ row, type: "update" })} className="cursor-pointer" > 리스크 관리 { const vendorId = row.original.vendorId; router.push(`/${paramsLanguage}/evcp/vendors/${vendorId}/info/credit`); }} className="cursor-pointer" > 신용정보 확인 ) }, }; return [ selectColumn, { id: 'riskSource', header: '리스크 정보', columns: sourceColumns, }, { id: 'ratingTotal', header: '종합등급', columns: ratingTotalColumns, }, { id: 'ratingCredit', header: '신용등급', columns: ratingCreditColumns, }, { id: 'ratingCashflow', header: '현금흐름등급', columns: ratingCashflowColumns, }, { id: 'ratingWatch', header: 'WATCH등급', columns: ratingWatchColumns, }, { id: 'riskInput', header: '리스크 관리', columns: inputColumns, }, actionsColumn, ]; }; // ---------------------------------------------------------------------------------------------------- /* EXPORT */ export default getColumns;