import React, { useRef } from 'react';
import Table, { ProColumns, ActionType } from '@/components/Table';
import { getWithdrawList, solveWithdraw } from '@/services/withdraw';
import { fetchTableData } from '@/utils/table';
import { getBalanceAmount } from '@/utils/formatBalance';
import BigNumber from 'bignumber.js';
import ConfirmButton from '@/components/Table/ConfirmButton';

const WithdrawList = () => {
  const handleConfirm = async (uuid) => {
    await solveWithdraw({ uuid: uuid });
  };

  const tableRef = useRef<ActionType>();
  const columns: ProColumns<any>[] = [
    {
      title: '提现订单号',
      dataIndex: 'uuid',
      ellipsis: true,
    },
    {
      title: '金额',
      dataIndex: 'price',
      width: '10%',
      hideInSearch: true,
    },
    {
      title: '位数',
      dataIndex: 'decimals',
      width: '10%',
      hideInSearch: true,
    },
    {
      title: '发送地址',
      dataIndex: 'form_address',
      hideInSearch: true,
      ellipsis: true,
    },
    {
      title: '接受地址',
      dataIndex: 'to_address',
      hideInSearch: true,
      ellipsis: true,
    },
    {
      title: '游戏币类型',
      dataIndex: 'bc_type',
      width: '10%',
    },
    {
      title: '时间',
      dataIndex: 'time',
      valueType: 'dateRange',
      ellipsis: true,
    },
    {
      title: '操作',
      valueType: 'option',
      width: 150,
      render: (_, row) => [
        <ConfirmButton
          key="confirm"
          title="确认通知已提现?"
          buttonName="通知"
          onConfirm={() => {
            handleConfirm(row.uuid);
          }}
        />,
      ],
    },
  ];
  return (
    <div>
      <Table
        columns={columns}
        rowKey="id"
        actionRef={tableRef}
        request={async (params) => {
          console.log('params = ', params);
          const res = await fetchTableData(getWithdrawList, params);
          for (const key in res.data) {
            if (Object.prototype.hasOwnProperty.call(res.data, key)) {
              const element = res.data[key];
              element.price = getBalanceAmount(
                new BigNumber(element.price),
                element.decimals,
              ).toNumber();
            }
          }
          return res;
        }}
      />
    </div>
  );
};

export default WithdrawList;
