/* 扩展字段内容
 * @Author: Li Hanlin
 * @Date: 2022-11-29 14:03:07
 * @Last Modified by: Li Hanlin
 * @Last Modified time: 2022-12-16 16:42:47
 */

import { doFetch } from '@/utils/doFetch';
import { useRequest } from 'ahooks';
import { useState, useEffect } from 'react';
import InitForm from '../InitForm';

const App = ({
  setDrawer,
  onFinish,
  drawer,
  formRef,
  columns,
  urlParams,
  formId,
  defaultFormValue,
  onValuesChange,
}) => {
  const [value, setValue] = useState('');
  const [fieldscolumns, setfieldscolumns] = useState(columns);
  const selectValueType = (type, options) => {
    switch (type) {
      case 1:
        return {
          valueType: 'input',
        };
      case 2:
        return {
          valueType: 'select',
          options,
        };
      case 3:
        return {
          valueType: 'radio',
          options,
        };
      case 4:
        return {
          valueType: 'select',
          fieldProps: {
            placeholder: '请选择',
            mode: 'multiple',
          },
          options,
        };
      default:
        break;
    }
  };

  useEffect(() => {
    const fn = async () => {
      let res = await doFetch({
        url: '/base/paFormField/queryList',
        params: {
          formId: formId,
        },
      });
      if (res?.data?.dataList) {
        let column = [
          {
            title: '扩展字段',
            valueType: 'split',
          },
        ];
        res?.data?.dataList?.forEach?.((el) => {
          column.push({
            ...selectValueType(el.fieldChar, el.valueList),
            title: el.fieldName,
            dataIndex: el.fieldName,
            key: el.id,
          });
        });
        setfieldscolumns(fieldscolumns.concat(column));
      }
    };
    fn();
  }, []);

  const selectextendField = (type) => {
    switch (Number(type)) {
      case 1:
        return 'userCharList';
      case 2:
        return 'equipmentCharList';
      case 3:
        return 'equipmentSupplierCharList';
      default:
        break;
    }
  };
  return (
    <>
      <InitForm
        formRef={formRef}
        defaultFormValue={defaultFormValue}
        extendField={selectextendField(formId)}
        detailpath={urlParams?.detail || null}
        params={{ id: drawer?.item?.id || null }}
        fields={fieldscolumns}
        onFinish={onFinish}
        onValuesChange={onValuesChange}
      />
    </>
  );
};

export default App;
