import React, { useRef } from 'react';
import Table, { ProColumns, ActionType } from '@/components/Table';
import { getRecordList } from '@/services/recharge/record';
import { fetchTableData } from '@/utils/table';
import moment from 'moment';
import { ContractType } from '@/constants/enum/contract';

const NFTTradeList = () => {
  const tableRef = useRef<ActionType>();
  const columns: ProColumns<any>[] = [
    {
      title: '时间',
      dataIndex: 'time',
      hideInTable: true,
      valueType: 'dateRange',
      ellipsis: true,
    },
    {
      title: '交易哈希',
      dataIndex: 'tx_hash',
      hideInSearch: true,
      ellipsis: true,
    },
    {
      title: 'TokenID',
      dataIndex: 'token_id',
      width: '10%',
      hideInSearch: true,
    },
    {
      title: '块编号',
      dataIndex: 'block_number',
      hideInSearch: true,
    },
    {
      title: '发送地址',
      dataIndex: 'from_address',
      hideInSearch: true,
      ellipsis: true,
    },
    {
      title: '接受地址',
      dataIndex: 'to_address',
      hideInSearch: true,
      ellipsis: true,
    },
    {
      title: '充值时间',
      dataIndex: 'time',
      hideInSearch: true,
      ellipsis: true,
    },
  ];
  return (
    <Table
      columns={columns}
      rowKey="id"
      actionRef={tableRef}
      request={async (params) => {
        console.log('params = ', params);
        if ((params.time ?? '') !== '') {
          const start = Date.parse(params.time[0] + ' 00:00:00');
          const end = Date.parse(params.time[1] + ' 23:59:59');
          params.start_time = start / 1000;
          params.end_time = end / 1000;
        }
        params.erc = ContractType.NFT721;
        const res = await fetchTableData(getRecordList, params);
        for (const key in res.data) {
          if (Object.prototype.hasOwnProperty.call(res.data, key)) {
            const element = res.data[key];
            element.time = moment(element.time * 1000).format('YYYY-MM-DD hh:mm:ss');
          }
        }
        return res;
      }}
    />
  );
};

export default NFTTradeList;
