import React, { useState, useRef } from 'react';
import Table, { ProColumns, ActionType } from '@/components/Table';
import { getNoticeList, createNotice, updateNotice, deleteNotice } from '@/services/system/notice';
import { fetchTableData } from '@/utils/table';
import DeleteButton from '@/components/Table/DeleteButton';
import { Switch } from 'antd';
import AddNoticeModal from '../components/AddNoticeModal';
import EditNoticeModal from '../components/EditNoticeModal';
import { NoticeType } from '@/constants/enum/notice';

const NoticeList = () => {
  const tableRef = useRef<ActionType>();

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

  const handleEdit = (row: any) => {
    setModalData(row);
    setIsEditModalVisible(true);
    tableRef.current?.reload();
  };

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

  const noticeTypeList = [
    {
      label: '充值',
      value: NoticeType.RECHARGE,
    },
    {
      label: '提现',
      value: NoticeType.WITHDRAW,
    },
    {
      label: 'NFT创建',
      value: NoticeType.NFTCREATE,
    },
    {
      label: 'NFT转移',
      value: NoticeType.NFTTRANSFER,
    },
    {
      label: 'NFT销毁',
      value: NoticeType.NFTTDISPOSE,
    },
  ];

  const columns: ProColumns<any>[] = [
    {
      title: '通知码',
      dataIndex: 'code',
      valueEnum: () => {
        const options = {};
        noticeTypeList.forEach((item) => {
          options[item.value] = item.label;
        });
        return options;
      },
    },
    {
      title: '通知地址',
      dataIndex: 'addr',
      hideInSearch: true,
      ellipsis: true,
    },
    {
      title: '操作',
      valueType: 'option',
      width: 180,
      render: (_, row) => [
        <a
          key="edit"
          onClick={() => {
            handleEdit(row);
          }}
        >
          编辑
        </a>,
        <Switch
          key="switch"
          defaultChecked={row.status}
          size="small"
          onChange={async (checked) => {
            row.status = checked;
            await updateNotice(row);
          }}
        />,
        <DeleteButton
          key="delete"
          onDelete={() => {
            handleDelete(row.code);
          }}
        />,
      ],
    },
  ];
  return (
    <div>
      <Table
        columns={columns}
        rowKey="id"
        search={false}
        toolBarActions={[
          {
            type: 'add',
            onConfirm: () => {
              setIsModalVisible(true);
            },
          },
        ]}
        actionRef={tableRef}
        request={async (params) => {
          const res = await fetchTableData(getNoticeList, params);
          return res;
        }}
      />
      <AddNoticeModal
        visible={isModalVisible}
        onCancel={function () {
          setIsModalVisible(false);
        }}
        onOk={async function (val: any): Promise<void> {
          await createNotice(val);
          setIsModalVisible(false);
          tableRef.current?.reload();
        }}
      />
      <EditNoticeModal
        visible={isEditModalVisible}
        editModalData={modalData}
        onCancel={function () {
          setIsEditModalVisible(false);
        }}
        onOk={async function (val: any): Promise<void> {
          await updateNotice(val);
          setIsEditModalVisible(false);
          tableRef.current?.reload();
        }}
      />
    </div>
  );
};

export default NoticeList;
