import {useEffect} from 'react'
import {
  ModalForm,
  ProForm,
  ProFormTextArea,
  ProFormText,
  ProFormDigit,
  ProFormRadio,
  ProFormDateRangePicker
} from '@ant-design/pro-components';
import { Button, Form, message } from 'antd';
import {DictOptionType, DictValueEnumObj} from "@/components/DictTag";
import { useIntl, FormattedMessage } from '@umijs/max';

export type ListFormProps = {
  onCancel: (flag?: boolean, formVals?: unknown) => void;
  onSubmit: (values: API.JobFairList.JobFairListRows) => Promise<void>;
  open: boolean;
  values?: Partial<API.JobFairList.JobFairListRows>;
  jobFairType?:  DictValueEnumObj;
};
const waitTime = (time: number = 100) => {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve(true);
    }, time);
  });
};

const listEdit: React.FC<ListFormProps> = (props) => {
  const [form] = Form.useForm();
  const intl = useIntl();
  const { jobFairType } = props;

  useEffect(() => {
    form.resetFields();
    if(props.values) {
      form.setFieldsValue({
        ...props.values,
        createTimeRanger: [props.values.startTime, props.values.endTime],
      });
    }
  }, [form, props]);


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

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

  return (
    <ModalForm<{
      name: string;
      company: string;
    }>
      title={`${props.values ? '编辑' : '新增'}招聘会`}
      form={form}
      autoFocusFirstInput
      open={props.open}
      modalProps={{
        destroyOnClose: true,
        onCancel: () => handleCancel(),
      }}
      submitTimeout={2000}
      onFinish={handleFinish}
    >
      <ProFormDigit
        label="InputNumber"
        name="jobFairId"
        disabled
        hidden={true}
      />
      <ProForm.Group>
        <ProFormText
          width="md"
          name="name"
          label="招聘会名称"
          placeholder="请输入招聘会名称"
        />
        <ProFormRadio.Group
          valueEnum={jobFairType}
          name="jobFairType"
          label="招聘会类型"
          colProps={{ md: 24 }}
          placeholder="请选择招聘会类型"
          rules={[
            {
              required: false,
              message: <FormattedMessage id="请选择招聘会类型！" defaultMessage="请选择招聘会类型！" />,
            },
          ]}
        />
        <ProFormDateRangePicker
          transform={(values) => {
            return {
              startTime: values ? values[0] : undefined,
              endTime: values ? values[1] : undefined,
            };
          }}
          width="md"
          name="createTimeRanger"
          label="招聘会时间"
        />
      </ProForm.Group>
      <ProForm.Group>
        <ProFormTextArea
          width="xl"
          name="location"
          label="招聘会地点"
          placeholder="请输入招聘会地点"
        />
      </ProForm.Group>
    </ModalForm>
  );
};

export default listEdit
