import { ModalForm, ProForm, ProFormDigit, ProFormText } from '@ant-design/pro-components';
import { Form } from 'antd';
import { DictOptionType, DictValueEnumObj } from '@/components/DictTag';
import { useEffect } from 'react';

export type ListFormProps = {
  onCancel: (flag?: boolean, formVals?: unknown) => void;
  onSubmit: (values: API.AreaSubWay.Line) => Promise<void>;
  open: boolean;
  values?: Partial<API.AreaSubWay.Line>;
  jobGroupOptions?: DictOptionType[];
  statusOptions?: DictValueEnumObj;
};

const waitTime = (time: number = 100) => {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve(true);
    }, time);
  });
};

const SubWayEdit: React.FC<ListFormProps> = (props) => {
  const [form] = Form.useForm();

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

  const handleCancel = () => {
    props.onCancel();
    form.resetFields();
  };

  const handleFinish = async (values: Record<string, any>) => {
    props.onSubmit(values as API.AreaSubWay.Line);
  };

  return (
    <ModalForm<{
      name: string;
      company: string;
    }>
      title={`${props.values ? '编辑' : '新增'}区域`}
      form={form}
      // layout="inline"
      autoFocusFirstInput
      open={props.open}
      modalProps={{
        destroyOnClose: true,
        onCancel: () => handleCancel(),
      }}
      submitTimeout={2000}
      onFinish={handleFinish}
    >
      <ProFormDigit
        name="regionalId"
        label={'字典主键'}
        placeholder="请输入字典主键"
        disabled
        hidden={true}
      />
      <ProForm.Group>
        <ProFormText width="xl" name="regionalName" label="区域名称:" placeholder="请输入名称" />
      </ProForm.Group>
    </ModalForm>
  );
};

export default SubWayEdit;
