// 添加代币类型弹窗
import React, { useRef } from 'react';
import { createForm } from '@formily/core';
import { createSchemaField } from '@formily/react';
import Modal, { ModalProps } from '@/components/Modal';
import { Form, FormItem, Input, Select } from '@formily/antd';

import { NoticeType } from '@/constants/enum/notice';

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,
  },
];

interface AddNoticeModalPropsType extends ModalProps {
  onOk: (val: any) => void;
  onCancel: () => void;
}

const SchemaField = createSchemaField({
  components: {
    FormItem,
    Input,
    Select,
  },
});

const form = createForm({});

const AddNoticeModal = ({ onOk, onCancel, ...rest }: AddNoticeModalPropsType) => {
  const handleOk = () => {
    form.submit(async () => {
      const formState = form.getFormState();
      formState.values.code = parseInt(formState.values.code);
      onOk(formState.values);
    });
  };

  const handleCancel = () => {
    onCancel();
  };

  return (
    <Modal title="添加通知" onOk={handleOk} onCancel={handleCancel} width={800} {...rest}>
      <Form form={form} labelCol={4} wrapperCol={18}>
        <SchemaField>
          <SchemaField.Number
            name="code"
            title="通知码"
            required
            x-component-props={{
              options: noticeTypeList,
            }}
            x-decorator="FormItem"
            x-component="Select"
          />
          <SchemaField.String
            name="addr"
            title="通知地址"
            required
            x-decorator="FormItem"
            x-component="Input"
          />
          <SchemaField.Boolean
            name="status"
            title="状态"
            required
            x-decorator="FormItem"
            x-component="Select"
            enum={[
              { label: '启用', value: true },
              { label: '禁用', value: false },
            ]}
          />
        </SchemaField>
      </Form>
    </Modal>
  );
};

export default AddNoticeModal;
