import React, { useState } from 'react';
import { createForm } from '@formily/core';
import { createSchemaField } from '@formily/react';
import { message, Spin } from 'antd';
import DetailPageContainer from '@/components/DetailPageContainer';
import { AddAddress } from '@/services/eth';
import { initWeb3, walletSign } from '../../../../utils/web3';
import { Form, FormItem, Input, FormButtonGroup, Submit } from '@formily/antd';

const SchemaField = createSchemaField({
  components: {
    FormItem,
    Input,
    Submit,
  },
});
const form = createForm({});
const WorkEdit = () => {
  const [loading, setLoading] = useState(false);

  const handleSubmit = async (val) => {
    setLoading(true);
    try {
      await initWeb3();
      const signInfo = await walletSign();
      val.key = signInfo.raw;
      val.sign = signInfo.sign;
      val.coinType = 'eth';
      val.num = parseInt(val.num);
      await AddAddress(val);
      message.success('操作成功');
      setLoading(false);
      history.back();
    } catch (e) {
      setLoading(false);
    }
  };
  return (
    <DetailPageContainer>
      <Spin spinning={loading}>
        <Form form={form} labelCol={4} wrapperCol={18} onAutoSubmit={handleSubmit}>
          <SchemaField>
            <SchemaField.String
              name="alias"
              title="备注"
              required
              x-decorator="FormItem"
              x-component="Input"
            />
            <SchemaField.Number
              name="num"
              title="数量"
              required
              x-decorator="FormItem"
              x-component="Input"
            />
          </SchemaField>
          <FormButtonGroup.FormItem>
            <Submit block size="large">
              提交
            </Submit>
          </FormButtonGroup.FormItem>
        </Form>
      </Spin>
    </DetailPageContainer>
  );
};

export default WorkEdit;
