import React, { useEffect, useState } from 'react';
import {
  ProForm,
  ProFormDigit,
  ProFormText,
  ProFormRadio,
  ProFormSelect,
  ProFormCascader,
  ProFormTextArea,
  EditableProTable
} from '@ant-design/pro-components';
import { Form, Modal, Input } from 'antd';
import { useIntl, FormattedMessage } from '@umijs/max';
import { getMerchantSelect } from '@/services/merchant/index'
import { getOperatorRateConfigSelect } from '@/services/operator/config';



const UserForm: React.FC = (props: any) => {

  const [form] = Form.useForm();
  const { values } = props;





  useEffect(() => {
    form.resetFields();
    form.setFieldsValue(values);
  }, [form, props]);

  const intl = useIntl();
  const handleOk = () => {
    form.submit();
  };
  const handleCancel = () => {
    props.onCancel();
  };
  const handleFinish = async (values: any) => {
    let data = JSON.parse(JSON.stringify(values))
    if (!values.id) {
      delete data.merchantInfoUpdateParams
      data.merchantInfoAddParams = values.merchantInfoUpdateParams
    }
    props.onSubmit(data);
  };

  return (
    <Modal
      width={800}
      title={'运营商'}
      open={props.open}
      forceRender
      destroyOnClose
      onOk={handleOk}
      onCancel={handleCancel}
    >
      <ProForm
        form={form}
        grid={true}
        submitter={false}
        layout="horizontal"
        onFinish={handleFinish}>
        <ProFormDigit
          name="id"
          label={'ID'}
          disabled
          hidden={true}
        />
        <ProForm.Group>
          <ProFormSelect
            name="merchantId"
            label={'选择商户'}
            colProps={{ md: 8, xl: 8 }}
            placeholder="请选择商户"
            rules={[{ required: true, message: '请选择' }]}
            fieldProps={
              {
                fieldNames: {
                  label: 'label',
                  value: 'id'
                },
              }
            }
            request={async () => {
              let { data } = await getMerchantSelect()
              return data
            }}
          />
        </ProForm.Group>
        <ProForm.Group>
          <ProFormText
            name="userName"
            label={'用户名'}
            colProps={{ md: 12, xl: 12 }}
            placeholder="请输入用户名"
            rules={[{ required: true, message: '请输入' }]}
          />
          <ProFormText
            name="password"
            label={'密码'}
            colProps={{ md: 12, xl: 12 }}
            placeholder="请输入密码"
            rules={[{ required: true, message: '请输入' }]}
          />
          <ProFormText
            name="phone"
            label={'手机号'}
            colProps={{ md: 12, xl: 12 }}
            placeholder="请输入手机号"
            rules={[{ required: true, message: '请输入' }]}
          />

        </ProForm.Group>
        <ProForm.Group>
          <ProFormText
            name="electricityRate"
            label={'电费分成比例'}
            colProps={{ md: 12, xl: 12 }}
            placeholder="请输入电费分成比例"
            rules={[{ required: true, message: '请输入' }]}
          />

          <ProFormText
            name="serviceRate"
            label={'服务费分成比例'}
            colProps={{ md: 12, xl: 12 }}
            placeholder="请输入服务费分成比例"
            rules={[{ required: true, message: '请输入' }]}
          />

          {/* <ProFormSelect
            name="rateConfigId"
            label={'分成比例配置'}
            colProps={{ md: 8, xl: 8 }}
            placeholder="请选择分成比例配置"
            rules={[{ required: true, message: '请选择' }]}
            fieldProps={
              {
                fieldNames: {
                  label: 'label',
                  value: 'id'
                },
              }
            }
            request={async () => {
              let { data } = await getOperatorRateConfigSelect()
              return data
            }}
          /> */}
        </ProForm.Group>
        <ProForm.Group>
          {/* <ProFormDigit
            name="balance"
            label={'商户余额'}
            colProps={{ md: 12, xl: 12 }}
            placeholder="请输入商户余额"
            rules={[{ required: true, message: '请输入' }]}
          />
          <ProFormDigit
            name="version"
            label={'版本'}
            colProps={{ md: 12, xl: 12 }}
            placeholder="请输入版本"
          /> */}
          <ProFormTextArea
            name="deviceNo"
            label={'设备编号'}
            colProps={{ md: 12, xl: 12 }}
            placeholder="请输入设备编号英文逗号分割"
            rules={[{ required: true, message: '请输入' }]}

          />
        </ProForm.Group>
      </ProForm>
    </Modal>
  );
};

export default UserForm;
