import { render, screen } from "@testing-library/react";
import { expect, test, vi } from "vitest";
import { FinanceWithdrawalApplicationList } from "./FinanceWithdrawalApplicationList.jsx";

const baseProps = {
  applications: {
    items: [],
    page: 1,
    pageSize: 50,
    total: 0
  },
  error: "",
  filters: {
    appCode: "",
    keyword: "",
    page: 1
  },
  loading: false,
  onFiltersChange: vi.fn(),
  onReload: vi.fn(),
  options: { apps: [] }
};

test("renders withdrawal application table with requested finance columns", () => {
  render(
    <FinanceWithdrawalApplicationList
      {...baseProps}
      applications={{
        ...baseProps.applications,
        items: [
          {
            appCode: "lalu",
            approvedAtMs: 1760007200000,
            approverName: "财务一",
            approverUserId: 21,
            createdAtMs: 1760000000000,
            id: 7,
            userId: "10001",
            withdrawAddress: "TRX-address",
            withdrawAmount: 12.5,
            withdrawMethod: "USDT-TRC20"
          }
        ],
        total: 1
      }}
    />
  );

  expect(screen.getByRole("columnheader", { name: "APP" })).toBeInTheDocument();
  expect(screen.getByRole("columnheader", { name: "用户ID" })).toBeInTheDocument();
  expect(screen.getByRole("columnheader", { name: "提现金额" })).toBeInTheDocument();
  expect(screen.getByRole("columnheader", { name: "提现方式" })).toBeInTheDocument();
  expect(screen.getByRole("columnheader", { name: "提现地址" })).toBeInTheDocument();
  expect(screen.getByRole("columnheader", { name: "申请时间" })).toBeInTheDocument();
  expect(screen.getByRole("columnheader", { name: "审批人" })).toBeInTheDocument();
  expect(screen.getByRole("columnheader", { name: "审批时间" })).toBeInTheDocument();
  expect(screen.getByRole("columnheader", { name: "操作" })).toBeInTheDocument();
  expect(screen.getByText("lalu")).toBeInTheDocument();
  expect(screen.getByText("10001")).toBeInTheDocument();
  expect(screen.getByText("$ 12.5")).toBeInTheDocument();
  expect(screen.getByText("USDT-TRC20")).toBeInTheDocument();
  expect(screen.getByText("TRX-address")).toBeInTheDocument();
  expect(screen.getByText("财务一")).toBeInTheDocument();
  expect(screen.getByText("21")).toBeInTheDocument();
});

test("keeps empty withdrawal table colspan aligned with visible columns", () => {
  const { container } = render(<FinanceWithdrawalApplicationList {...baseProps} />);

  expect(container.querySelector("tbody td")?.colSpan).toBe(9);
  expect(screen.getByText("当前无数据")).toBeInTheDocument();
});
