// import { useIntl } from '@umijs/max';
// import { Form, Input, InputNumber, Select } from 'antd';
// import { useEffect } from 'react';
// import style from '../index.less';

// type DicSettingFormType = {
//   initialValues?: {
//     [key: string]: any;
//   };
//   onSave: (values: any) => void;
//   handelSubmit: number;
//   addType?: number;
// };
// type Value = {
//   [key: string]: any;
// };

// const DicSettingForm = ({
//   onSave,
//   initialValues,
//   handelSubmit,
//   addType,
// }: DicSettingFormType) => {
//   const [form] = Form.useForm();
//   const intl = useIntl();
//   const { TextArea } = Input;
//   //表单内容
//   const dictionaries: { [key: string]: any }[] = [
//     {
//       label: intl.formatMessage({
//         id: 'dictionaries.columns.title.name',
//       }),
//       name: 'dictLabel',
//       key: 'dictLabel',
//       type: 'input',
//       required: true,
//     },
//     {
//       label: intl.formatMessage({
//         id: 'dictionaries.columns.title.number',
//       }),
//       name: 'dictValue',
//       key: 'dictValue',
//       type: 'input',
//       required: true,
//       hidden: false,
//       disabled: addType === 1 ? false : true,
//     },
//     {
//       label: intl.formatMessage({
//         id: 'dictionaries.label.parentId',
//       }),
//       name: 'parentLabel',
//       key: 'parentLabel',
//       type: 'input',
//       required: addType === 1 ? false : true,
//       hidden: addType === 1 ? true : false,
//       disabled: true,
//     },
//     {
//       label: intl.formatMessage({
//         id: 'dictionaries.label.parentId',
//       }),
//       name: 'parentId',
//       key: 'parentId',
//       type: 'input',
//       required: addType === 1 ? false : true,
//       hidden: true,
//       disabled: true,
//     },

//     // {
//     //   label: intl.formatMessage({
//     //     id: 'dictionaries.label.keyValue',
//     //   }),
//     //   name: 'dictValue',
//     //   key: 'dictValue',
//     //   type: 'input',
//     //   required: addType === 1 ? false : true,
//     //   hidden: addType === 1 ? true : false,
//     // },
//     // {
//     //   label: intl.formatMessage({
//     //     id: 'form.title.remarks',
//     //   }),
//     //   name: 'remark',
//     //   key: 'remark',
//     //   type: 'textArea',
//     //   required: false,
//     // },
//     {
//       label: 'id',
//       name: 'id',
//       key: 'id',
//       type: 'input',
//       required: false,
//       hidden: true,
//     },
//   ];

//   //表单具体内容
//   const formItem = (item: any) => {
//     switch (item.type) {
//       case 'input':
//         return (
//           <Input
//             placeholder={intl.formatMessage({ id: 'form.info.text.entrt' })}
//             disabled={item.disabled}
//           />
//         );
//       case 'inputNumber':
//         return (
//           <InputNumber
//             className={style['input-number']}
//             min={1}
//             // placeholder={'0'}
//             style={{ width: '40%' }}
//             placeholder={intl.formatMessage({ id: 'form.info.text.entrt' })}
//           />
//         );
//       case 'select':
//         return (
//           <Select
//             allowClear
//             disabled={item.disabled}
//             placeholder={intl.formatMessage({ id: 'form.select.rules.pleaseSelect' })}
//           >
//             {item.options.map((option: any) => (
//               <Select.Option
//                 key={option.label + option.value}
//                 value={option.value}
//               >
//                 {option['label']}
//               </Select.Option>
//             ))}
//           </Select>
//         );
//       case 'textArea':
//         return (
//           <TextArea
//             rows={4}
//             placeholder={intl.formatMessage({ id: 'dictionaries.input.rules.comment' })}
//             maxLength={300}
//             showCount
//           />
//         );
//       // case 'select':
//       //   return (
//       //     <Select
//       //       allowClear
//       //       mode={item.mode || null}
//       //       disabled={item.disabled}
//       //       placeholder={item.placeholder || ''}
//       //     >
//       //       {item.options.map((option:any) => (
//       //         <Select.Option key={option.label + option.value} value={option.value}>
//       //           {option['label']}
//       //         </Select.Option>
//       //       ))}
//       //     </Select>);
//       default:
//         return null;
//     }
//   };

//   useEffect(() => {
//     // 校验表单，成功提交表单的数据给onSave
//     if (handelSubmit !== 0) {
//       form
//         .validateFields()
//         .then((values) => {
//           onSave(values);
//         })
//         .catch((errorInfo) => {
//           console.log('Failed:', errorInfo);
//         });
//     } else {
//       form.resetFields();
//     }
//   }, [handelSubmit]);

//   useEffect(() => {
//     // console.log(initialValues);
//     //数据回显
//     if (Object.keys(initialValues as Value).length) {
//       form.setFieldsValue({ ...initialValues });
//     } else {
//       form.resetFields();
//     }
//   }, [initialValues]);

//   return (
//     <Form form={form} layout="vertical">
//       <div>
//         {dictionaries.map((item) => {
//           return (
//             <Form.Item
//               label={item.name !== 'id' ? item.label : null}
//               name={item.name}
//               hidden={item.hidden}
//               labelCol={{ span: item.col || 24 }}
//               wrapperCol={{ span: item.col || 24 }}
//               key={item.key}
//               rules={[
//                 item.required
//                   ? {
//                       required: true,
//                       message: item.message,
//                     }
//                   : { required: false },
//               ]}
//             >
//               {formItem(item)}
//             </Form.Item>
//           );
//         })}
//       </div>
//     </Form>
//   );
// };

// export default DicSettingForm;
import BaseForm from '@/components/BaseForm';
import { useIntl } from '@umijs/max';
import type { RadioChangeEvent } from 'antd';
import { Form, FormProps } from 'antd';
import { useEffect } from 'react';
type RoleSettingFormType = {
  initialValues?: {
    [key: string]: any;
  };
  onSave: (values: any) => void;
  isEdit?: boolean;
  handelSubmit: number;
};

const DicSettingForm = ({
  onSave,
  initialValues,
  isEdit,
  handelSubmit,
}: RoleSettingFormType) => {
  const [form] = Form.useForm();
  const intl = useIntl();
  const config: FormProps = {
    form: form,
    layout: 'vertical',
    initialValues: { ...initialValues },
  };
  let itemColumn = [
    {
      type: 'input',
      itemProps: {
        name: 'dictId',
        rules: [{ required: isEdit }],
        hidden: true,
      },
    },
    {
      type: 'input',
      itemProps: {
        name: 'version',
        rules: [{ required: isEdit }],
        hidden: true,
      },
    },
    {
      type: 'input',
      itemProps: {
        name: 'dictLabel',
        label: intl.formatMessage({ id: 'dictionaries.columns.title.name' }),
        rules: [
          {
            required: true,
            message: intl.formatMessage(
              {
                id: 'form.rules.required',
              },
              {
                label: intl.formatMessage({
                  id: 'dictionaries.columns.title.name',
                }),
              },
            ),
          },
          // () => ({
          //   async validator(_: any, value: string) {
          //     if (value) {
          //       if (isEdit && value === initialValues.roleName) {
          //         return Promise.resolve();
          //       } else {
          //         const data = await get(DETERMINE_ROLENAME, {
          //           roleName: value,
          //         });
          //         if (!data.data) {
          //           return Promise.resolve();
          //         } else {
          //           return Promise.reject(
          //             intl.formatMessage({
          //               id: 'role.info.text.repeatName',
          //             }),
          //           );
          //         }
          //       }
          //     } else {
          //       return Promise.resolve();
          //     }
          //   },
          // }),
        ],
      },
      elementProps: {
        placeholder: intl.formatMessage({
          id: 'form.info.text.entrt',
        }),
        maxLength: 30,
      },
    },
    {
      type: 'input',
      itemProps: {
        name: 'dictValue',
        label: intl.formatMessage({ id: 'dictionaries.columns.title.number' }),
        rules: [
          {
            required: true,
            message: intl.formatMessage(
              {
                id: 'form.rules.required',
              },
              {
                label: intl.formatMessage({
                  id: 'dictionaries.columns.title.number',
                }),
              },
            ),
          },
        ],
      },
      elementProps: {
        placeholder: intl.formatMessage({
          id: 'form.info.text.entrt',
        }),
        maxLength: 30,
      },
    },
    {
      type: 'radio',
      itemProps: {
        name: 'isDefault',
        label: intl.formatMessage({
          id: 'dictionaries.columns.title.isDefault',
        }),
        rules: [{ required: true }],
        hidden:parseInt(initialValues?.parentId)===0
      },
      elementProps: {
        placeholder: intl.formatMessage({
          id: 'form.select.rules.pleaseSelect',
        }),
        onChange: (e: RadioChangeEvent) => {
          console.log(e);
          // setIsFrame(!event.target.value);
        },
      },
      options: [
        {
          key: true,
          label: intl.formatMessage({ id: 'columns.handel.yes' }),
          value: 1,
        },
        {
          key: false,
          label: intl.formatMessage({ id: 'columns.handel.no' }),
          value: 0,
        },
      ],
    },
    {
      type: 'radio',
      itemProps: {
        name: 'isDisabled',
        label: intl.formatMessage({
          id: 'dictionaries.columns.title.isDisabled',
        }),
        rules: [{ required: true }],
        hidden:parseInt(initialValues?.parentId)===0
      },
      elementProps: {
        placeholder: intl.formatMessage({
          id: 'form.select.rules.pleaseSelect',
        }),
        onChange: (e: RadioChangeEvent) => {
          console.log(e);
          // setIsFrame(!event.target.value);
        },
      },
      options: [
        {
          key: true,
          label: intl.formatMessage({ id: 'columns.handel.yes' }),
          value: 1,
        },
        {
          key: false,
          label: intl.formatMessage({ id: 'columns.handel.no' }),
          value: 0,
        },
      ],
    },
  ];
  useEffect(() => {
    // 校验表单，成功提交表单的数据给onSave
    // console.log(handelSubmit);
    if (handelSubmit) {
      form
        .validateFields()
        .then((values) => {
          if (!isEdit) {
            values.parentId = values.dictId;
            delete values.dictId;
          }
          values.dictType='zh_cn';
          // console.log(values);
          onSave(values);
        })
        .catch((errorInfo) => {
          console.log('Failed:', errorInfo);
        });
    } else {
      form.resetFields();
    }
  }, [handelSubmit]);
  // useEffect(() => {
  //   console.log(initialValues);
  // }, [initialValues]);

  // useEffect(() => {
  //   findMenus({ menuId: initialValues?.menuId }).then((resp) => {
  //     const data = resp.data;
  //     form.setFieldsValue({ ...data });
  //     setSelected(data?.icon);
  //     setIsFrame(!data.isFrame);
  //   });
  // }, [initialValues]);

  return <BaseForm config={config} itemColumn={itemColumn} />;
};

export default DicSettingForm;
