import { Tag } from 'antd';
import { ProTable, type ProColumns } from '@ant-design/pro-components';
import PageContainer from '@/components/common/PageContainer';
import { ruleApi, ExceptionCodeResp } from '@/api/rule';

const DIRECTION_COLOR = { PLUS: 'success', MINUS: 'error' } as const;

export default function ExceptionCodeList() {
  const columns: ProColumns<ExceptionCodeResp>[] = [
    { title: '예외코드', dataIndex: 'exceptionCode', width: 120, search: false,
      render: (_, r) => <strong>{r.exceptionCode}</strong> },
    { title: '예외명', dataIndex: 'exceptionName', width: 160, search: false },
    { title: '카테고리', dataIndex: 'category', width: 110, search: false },
    {
      title: '방향', dataIndex: 'direction', width: 80, align: 'center', search: false,
      render: (_, r) => <Tag color={DIRECTION_COLOR[r.direction as 'PLUS' | 'MINUS'] ?? 'default'}>
        {r.direction === 'PLUS' ? '가산 (+)' : '차감 (-)'}</Tag>,
    },
    {
      title: '자동계산', dataIndex: 'autoCalcYn', width: 90, align: 'center', search: false,
      render: (_, r) => r.autoCalcYn === 'Y' ? <Tag color="processing">자동</Tag> : <Tag>수동</Tag>,
    },
    {
      title: '승인 필요', dataIndex: 'approvalRequired', width: 90, align: 'center', search: false,
      render: (_, r) => r.approvalRequired === 'Y' ? <Tag color="warning">필요</Tag> : <Tag>불요</Tag>,
    },
    {
      title: '과세', dataIndex: 'taxIncluded', width: 80, align: 'center', search: false,
      render: (_, r) => r.taxIncluded === 'Y' ? <Tag color="success">과세</Tag> : <Tag>비과세</Tag>,
    },
    { title: '설명', dataIndex: 'description', ellipsis: true, search: false },
    {
      title: '활성', dataIndex: 'isActive', width: 70, align: 'center', search: false,
      render: (_, r) => <Tag color={r.isActive === 'Y' ? 'success' : 'default'}>
        {r.isActive === 'Y' ? 'Y' : 'N'}</Tag>,
    },
  ];

  return (
    <PageContainer title="예외 코드" description="가산/차감/승인/세금 정책 마스터 (예외금액 원장에서 사용)">
      <ProTable<ExceptionCodeResp>
        rowKey="exceptionCode"
        columns={columns}
        search={false}
        request={async () => {
          const list = await ruleApi.exceptionCodes();
          return { data: list, total: list.length, success: true };
        }}
        pagination={{ pageSize: 50, showTotal: (t) => `총 ${t}건` }}
        options={{ density: false, fullScreen: true, reload: true, setting: true }}
        dateFormatter="string"
      />
    </PageContainer>
  );
}
