import React, { useState, useRef } from 'react';
import Table, { ProColumns, ActionType } from '@/components/Table';
import {
  getAddressList,
  deleteAddress,
  createAddress,
  modifyAddress,
} from '@/services/recharge/wallet';

import { fetchTableData } from '@/utils/table';
import DeleteButton from '@/components/Table/DeleteButton';
import AddWalletModal from '../components/AddWalletModal';
import EditWalletModal from '../components/EditWalletModal';
import { Access, useAccess } from 'umi';
import { message } from 'antd';

const CollectionAddressList = () => {
  const tableRef = useRef<ActionType>();
  const access = useAccess();

  const [isModalVisible, setIsModalVisible] = useState(false);
  const [isEditModalVisible, setIsEditModalVisible] = useState(false);
  const [modalData, setModalData] = useState({});

  const handleEdit = (row: any) => {
    row.new_address = '';
    setModalData(row);
    setIsEditModalVisible(true);
  };

  const handleDelete = async (address: any) => {
    await deleteAddress({ address: address });
    tableRef.current?.reload();
  };

  const columns: ProColumns<any>[] = [
    {
      title: '收款地址',
      dataIndex: 'address',
      hideInSearch: true,
      ellipsis: true,
    },
    {
      title: '收款游戏币名称',
      dataIndex: 'name',
      hideInSearch: true,
    },
    {
      title: '操作',
      valueType: 'option',
      width: 150,
      render: (_, row) => [
        <Access accessible={access.canShowButton('收款地址编辑')}>
          <a
            key="edit"
            onClick={() => {
              handleEdit(row);
            }}
          >
            编辑
          </a>
        </Access>,
        <Access accessible={access.canShowButton('收款地址删除')}>
          <DeleteButton
            key="delete"
            onDelete={() => {
              handleDelete(row.address);
            }}
          />
        </Access>,
      ],
    },
  ];
  return (
    <div>
      <Table
        columns={columns}
        search={false}
        rowKey="address"
        toolBarActions={[
          {
            type: 'add',
            permission: '收款地址添加',
            onConfirm: () => {
              setIsModalVisible(true);
            },
          },
        ]}
        actionRef={tableRef}
        request={async (params) => {
          const res = await fetchTableData(getAddressList, params);
          return res;
        }}
      />
      <AddWalletModal
        visible={isModalVisible}
        onCancel={function () {
          setIsModalVisible(false);
        }}
        onOk={async function (val: any): Promise<void> {
          await createAddress(val);
          setIsModalVisible(false);
          message.success('收款地址添加成功');
          tableRef.current?.reload();
        }}
      />
      <EditWalletModal
        visible={isEditModalVisible}
        editModalData={modalData}
        onCancel={function () {
          setIsEditModalVisible(false);
        }}
        onOk={async function (val: any): Promise<void> {
          await modifyAddress(val);
          message.success('收款地址修改成功');
          setIsEditModalVisible(false);
          tableRef.current?.reload();
        }}
      />
    </div>
  );
};

export default CollectionAddressList;
