import {ProForm, ProFormInstance, ProFormText} from '@ant-design/pro-components';
import {message} from 'antd';
import React, {useRef} from 'react';
import {getConfigGetApi, postConfigSaveApi} from "@/services/swagger/configAdmin";


type RedPacketSend = {
  minLimit: number
};

export default () => {
  const formRef = useRef<ProFormInstance>();
  return (
    <ProForm
      layout="horizontal"
      title="签到设置"
      formRef={formRef}
      submitter={{}}
      request={async () => {
        const res = await getConfigGetApi({"code": "platform.redPacket.send"})
        if (res.success && res.data.content) {
          const result: RedPacketSend = JSON.parse(res.data.content)
          return result;
        }

        return {
          minLimit: 0,
        }
      }}
      onFinish={async (values) => {
        const result: RedPacketSend = {
          minLimit: parseInt(values.minLimit),
        };
        console.log("value " + values);

        const res = await postConfigSaveApi({
          "type": "json",
          "code": "platform.redPacket.send",
          "remark": "红包发送设置",
          "content": JSON.stringify(result)
        })

        if (res.success) {
          message.success('提交成功');
        } else {
          message.error(res.errorMessage)
        }
        return res.success;
      }}
    >

      <ProFormText name="minLimit" width="md" label={`每次发送红包至少`} addonAfter={"个"}/>
    </ProForm>
  );
};
