WithdrawApprove.tsx 6.08 KB
import { useMemo, useState } from 'react';
import { Skeleton, UAlert, UButton, UCard, UResult, UText } from '@uj/ui';
import { DataTable, SearchForm, type RowActionSchema } from '@/components';
import { withdrawApi, type ApprovalRow } from '@/api';
import { PP } from '@/permissions/codes';
import { usePointsAccess } from '@/features/shared/access';
import { useApprovalChannelOptions } from '@/features/shared/options';
import { firstPage, useExport, useList, usePageClamp, useRefresh } from '@/features/shared/queries';
import { goTo } from '@/features/shared/navigate';
import { buildApprovalColumns } from './tools/approval-columns';
import { buildApprovalFields, toApprovalParams } from './tools/approval-fields';
import { ApprovalDetailDialog } from './components/ApprovalDetailDialog';
import { PayApprovalDialog } from './components/PayApprovalDialog';

/** 待审批(0)与审批驳回(3)的批次还能改:可以编辑,也可以重新提付款审批。 */
const EDITABLE = new Set(['0', '3']);
/** 只有渠道已到账(5 用户到账 / 6 到账异常)的批次有到账明细可查。 */
const ARRIVED = new Set(['5', '6']);

/**
 * 积分提现审批:批次列表 + 查看弹窗 + 付款审批弹窗,另外两个动作跳到子页面(批次编辑、到账明细)。
 *
 * 老系统这一屏没有工具栏按钮,五个动作全部逐行给出(approve.vue 的 tableButtonList),
 * 「导出」也是逐行的:导出的是这一批的提现明细。
 */
export default function WithdrawApprove() {
  const { can } = usePointsAccess();
  const allowed = can(PP.APPROVAL_VIEW);
  const [filters, setFilters] = useState<Record<string, unknown>>({});
  const [page, setPage] = useState(firstPage);
  const [picked, setPicked] = useState<{ channel?: string; entity?: string }>({});
  const [detailId, setDetailId] = useState<string>();
  const [payRow, setPayRow] = useState<ApprovalRow>();
  const channels = useApprovalChannelOptions(picked.channel, picked.entity);
  const refresh = useRefresh(['points', 'approvals']);
  const list = useList<ApprovalRow>({
    scope: ['points', 'approvals'],
    filters,
    page,
    fetcher: withdrawApi.pageApprovals,
    enabled: allowed,
  });
  usePageClamp(list.total, page, setPage);
  const exportAction = useExport(withdrawApi.exportTransfers);

  const fields = useMemo(
    () =>
      buildApprovalFields({
        channels: channels.channels,
        entities: channels.entities,
        levies: channels.levies,
        channel: picked.channel,
        entity: picked.entity,
        onChannelChange: (channel) => setPicked({ channel }),
        onEntityChange: (entity) => setPicked((current) => ({ ...current, entity })),
      }),
    [channels.channels, channels.entities, channels.levies, picked.channel, picked.entity],
  );

  if (!allowed) {
    return (
      <UResult status="403" title="无积分提现审批访问权限" subTitle="请联系管理员确认账号权限。" />
    );
  }

  const rowActions: RowActionSchema<ApprovalRow>[] = [
    { label: '查看', onClick: (row) => setDetailId(row.id) },
    {
      label: '编辑',
      visible: (row) => EDITABLE.has(String(row.status ?? '')),
      onClick: (row) => goTo(`batch/${row.id}`),
    },
    {
      label: '付款审批',
      visible: (row) => can(PP.APPROVAL_PAY_AUDIT) && EDITABLE.has(String(row.status ?? '')),
      onClick: (row) => setPayRow(row),
    },
    {
      label: '到账明细',
      visible: (row) => ARRIVED.has(String(row.status ?? '')),
      onClick: (row) => goTo(`arrival/${row.id}`),
    },
    {
      label: '导出',
      // 权限走适配层的三态闸门,因此不用 RowActionSchema.permission(那条路要求后端已下发 points:*)。
      visible: () => can(PP.WITHDRAW_EXPORT),
      onClick: (row) => exportAction.mutate({ approvalId: row.id }),
    },
  ];

  return (
    <>
      <UCard className="uj-points-filter-card" size="small">
        <SearchForm
          fields={fields}
          layout="vertical"
          className="uj-points-search-form"
          initialValues={{
            batchNo: '',
            oaSpNo: '',
            channel: undefined,
            paymentSubjectId: undefined,
            paymentChannelId: undefined,
            status: '',
          }}
          onSearch={(values) => {
            setFilters(toApprovalParams(values));
            setPage(firstPage);
          }}
          onReset={() => {
            setFilters({});
            setPicked({});
            setPage(firstPage);
          }}
        />
      </UCard>
      <div className="uj-points-table-card">
        {list.query.isError ? (
          <UAlert
            type="error"
            showIcon
            title="审批批次加载失败"
            description={list.error}
            action={
              <UButton type="link" onClick={() => void list.query.refetch()}>
                重试
              </UButton>
            }
          />
        ) : list.query.isPending ? (
          <Skeleton variant="table" />
        ) : (
          <DataTable<ApprovalRow>
            columns={buildApprovalColumns()}
            rowKey="id"
            actionColumnWidth={200}
            dataSource={list.records}
            loading={list.query.isFetching}
            rowActions={rowActions}
            toolbarTitle={<UText type="secondary">共 {list.total} 个批次</UText>}
            pagination={{
              current: page.current,
              pageSize: page.size,
              total: list.total,
              showSizeChanger: true,
              pageSizeOptions: [10, 20, 50, 100],
              showTotal: (total) => `共 ${total} 条`,
              onChange: (current, size) =>
                setPage({ current: size === page.size ? current : 1, size }),
            }}
          />
        )}
      </div>

      <ApprovalDetailDialog
        open={!!detailId}
        id={detailId}
        onClose={() => setDetailId(undefined)}
      />
      <PayApprovalDialog
        open={!!payRow}
        row={payRow}
        onClose={() => setPayRow(undefined)}
        onSuccess={() => {
          setPayRow(undefined);
          void refresh();
        }}
      />
    </>
  );
}