import { ModalForm } from '@ant-design/pro-components';
import { Form, message } from 'antd';
import React from 'react';
import { RegionController } from '../../../services';
import BaseFormText from '@/components/BaseFormText';
import BaseFormTextArea from '@/components/BaseFormTextArea';

interface EditFormProps {
  title: string;
  type: 'add' | 'update';
  trigger: JSX.Element;
  supRegion: RegionController.RegionFields[];
  value?: RegionController.RegionFields;
  HandleonSubmit?: () => void;
}

const EditForm: React.FC<EditFormProps> = ({
  trigger,
  title,
  value,
  type,
  supRegion,
  HandleonSubmit,
}) => {
  const [form] = Form.useForm<RegionController.RegionFields>();

  /**
   * @name  编辑or新增
   * @param values
   */
  async function handleChangeFinish(values: RegionController.RegionFields) {
    const regionCode = supRegion[supRegion.length - 1]?.regionCode || '';
    if (type === 'add') {
      // const regionUuidPath = `/${supRegionUuid}/${uuid}`;
      const params = {
        ...values,
        supRegionCode: regionCode,
      };
      const res = await RegionController.addApi(params);
      if (res.code === 200) {
        message.success(res.message);
        HandleonSubmit?.();
      } else {
        message.success(res.message);
      }
    } else if (type === 'update') {
      const params = {
        ...value,
        ...values,
        supRegionCode: regionCode,
      };
      const res = await RegionController.updateApi(params);
      if (res.code === 200) {
        message.success(res.message);
        HandleonSubmit?.();
      } else {
        message.success(res.message);
      }
    }
  }
  return (
    <ModalForm<RegionController.RegionFields>
      labelCol={{ span: 6 }}
      wrapperCol={{ span: 14 }}
      layout="horizontal"
      title={title}
      trigger={trigger}
      form={form}
      autoFocusFirstInput
      modalProps={{ destroyOnClose: true }}
      submitTimeout={800}
      onFinish={async (values) => {
        await handleChangeFinish(values);
        return true;
      }}
    >
      {/* <ProFormSelect
        disabled
        name="regionName"
        label="上级地区"
        placeholder="请选择上级地区"
        request={async () => {
          const res = await RegionController.getSupList({
            uuid: supRegion[supRegion.length - 1]?.uuid || '0',
          });
          const { regionCode } = res.data || {};
          form.setFieldValue('regionCode', regionCode);
          return [res.data]?.map((item) => {
            return {
              ...item,
              value: item?.regionCode,
              label: item?.regionName,
            };
          });
        }}
      /> */}
      <BaseFormText
        name="supRegionCode"
        disabled
        label="上级地区"
        initialValue={supRegion[supRegion.length - 1]?.regionName}
        // placeholder="请输入区划代码"
      />
      <BaseFormText
        name="regionCode"
        label="区划代码"
        disabled={type === 'update'}
        initialValue={value?.regionCode}
        placeholder="请输入区划代码"
        tooltip="最长为 12 位"
        rules={[
          {
            required: true,
          },
          {
            max: 12,
            message: '区划代码长度不能超过12位',
          },
        ]}
      />
      <BaseFormText
        name="regionDesc"
        label="区划简称"
        initialValue={value?.regionDesc}
        placeholder="请输入区划简称"
        rules={[
          {
            required: true,
          },
        ]}
      />
      <BaseFormText
        name="regionName"
        label="区划名称"
        placeholder="请输入区划名称"
        initialValue={value?.regionName}
        rules={[
          {
            required: true,
          },
        ]}
      />
      {/* <ProFormRadio.Group
        name="deleteTag"
        label="是否启用"
        placeholder="例子：v2.0"
        initialValue={value?.deleteTag}
        options={[
          {
            label: '启用',
            value: '1',
          },
          {
            label: '停用',
            value: '2',
          },
          {
            label: '待审核',
            value: '3',
          },
        ]}
        rules={[
          {
            required: true,
          },
        ]}
      /> */}
      <BaseFormText
        name="version"
        label="版本号"
        placeholder="例子：v2.0"
        initialValue={value?.version}
        rules={[
          {
            required: true,
          },
        ]}
      />
      <BaseFormTextArea
        label="区划简介"
        placeholder="请输入区划简介内容"
        name="regionValidity"
        initialValue={value?.regionValidity}
      />
    </ModalForm>
  );
};

export default EditForm;
