import { ProTable, type ProColumns } from '@ant-design/pro-components';
import PageContainer from '@/components/common/PageContainer';
import CodeBadge from '@/components/common/CodeBadge';
import MoneyText from '@/components/common/MoneyText';
import ExcelExportButton from '@/components/common/ExcelExportButton';
import { useCommonCodes } from '@/hooks/useCommonCodes';
import { ledgerApi, LedgerResp, LedgerSearchParam } from '@/api/ledger';

export default function MaintainLedger() {
  const { data: insuranceCodes = [] } = useCommonCodes('INSURANCE_TYPE');
  const { data: reconCodes = [] }     = useCommonCodes('RECON_STATUS');

  const columns: ProColumns<LedgerResp>[] = [
    { title: '정산월', dataIndex: 'settleMonth', width: 90, fixed: 'left',
      valueType: 'dateMonth', fieldProps: { format: 'YYYYMM' } },
    { title: '증권번호', dataIndex: 'policyNo', width: 130, fixed: 'left',
      fieldProps: { placeholder: '증권번호' } },
    {
      title: '보험종류', dataIndex: 'insuranceType', width: 100, valueType: 'select',
      valueEnum: Object.fromEntries(insuranceCodes.map((c) => [c.code, { text: c.codeName }])),
      render: (_, r) => <CodeBadge groupCode="INSURANCE_TYPE" value={r.insuranceType} />,
    },
    {
      title: '대사상태', dataIndex: 'reconcileStatus', width: 100, valueType: 'select',
      valueEnum: Object.fromEntries(reconCodes.map((c) => [c.code, { text: c.codeName }])),
      render: (_, r) => <CodeBadge groupCode="RECON_STATUS" value={r.reconcileStatus} />,
    },
    { title: '설계사', dataIndex: 'agentName', width: 100, search: false },
    { title: '소속',   dataIndex: 'orgName',   width: 160, search: false },
    { title: '보험사', dataIndex: 'companyName', width: 120, search: false },
    { title: '상품',   dataIndex: 'productName', width: 200, search: false },
    {
      title: '보험료', dataIndex: 'premium', width: 110, align: 'right', search: false,
      render: (_, r) => <MoneyText value={r.premium} />,
    },
    {
      title: '회사율', dataIndex: 'companyRate', width: 80, align: 'right', search: false,
      render: (_, r) => <MoneyText value={r.companyRate} fraction={2} />,
    },
    {
      title: '회사수수료', dataIndex: 'companyAmount', width: 120, align: 'right', search: false,
      render: (_, r) => <MoneyText value={r.companyAmount} />,
    },
    {
      title: '지급율', dataIndex: 'payoutRate', width: 80, align: 'right', search: false,
      render: (_, r) => <MoneyText value={r.payoutRate} fraction={2} />,
    },
    {
      title: '설계사수수료', dataIndex: 'agentAmount', width: 120, align: 'right', search: false,
      render: (_, r) => <MoneyText value={r.agentAmount} />,
    },
    {
      title: '세액', dataIndex: 'taxAmount', width: 100, align: 'right', search: false,
      render: (_, r) => <MoneyText value={r.taxAmount} />,
    },
  ];

  return (
    <PageContainer title="유지수수료 원장" description="2~5년차 유지 계약에 대한 유지수수료">
      <ProTable<LedgerResp, LedgerSearchParam>
        rowKey="ledgerId"
        columns={columns}
        scroll={{ x: 1500 }}
        request={async (params) => {
          const { current, pageSize, ...rest } = params as any;
          const res = await ledgerApi.maintain({ ...rest, pageNum: current, pageSize });
          return { data: res.list, total: res.total, success: true };
        }}
        pagination={{ pageSize: 50, showTotal: (t) => `총 ${t.toLocaleString()}건` }}
        search={{ labelWidth: 'auto', searchText: '검색', resetText: '초기화', collapsed: false, collapseRender: false }}
        options={{ density: false, fullScreen: true, reload: true, setting: true }}
        toolBarRender={() => [
          <ExcelExportButton key="export" url="/api/ledger/maintain/export" fileName="유지수수료원장" />,
        ]}
        dateFormatter="string"
      />
    </PageContainer>
  );
}
