import {
  IFormItemProps,
  FormInstance,
  DformInput,
  DformSelect,
  DformDatePicker,
  DformCheckBox,
  DformTextArea,
  DformRadio,
  DformPicker,
  DformCustom,
  DformSwitch,
  RangeDatePicker,
  DformFile,
  DformImagePicker,
  DformText,
  AddressPicker,
} from '@alitajs/dform';
import { Toast } from 'antd-mobile-v2';
import React from 'react';

const initBCMCDFormState = ({
  form,
  isEdit = true,
  onChange,
  onBlur,
  optionObjs = {},
  extra,
  onClick,
  onChangeLevel,
  changeFormObj = {},
  properties = [],
}: {
  form: FormInstance;
  isEdit?: boolean;
  onChange: (value: any, key: string) => void;
  onBlur: (value: any, key: string) => void;
  onClick: (key: string) => void;
  onChangeLevel: (value: any, key: string) => void;
  optionObjs?: any;
  extra?: any;
  changeFormObj?: any;
  properties?: any[];
}) => {
  const eleArr = [] as any[];
  properties.forEach((row: any, index: number) => {
    const {
      componentType,
      elementCode,
      editable,
      rules = {},
      initialValue,
      label,
      disabled,
      visible,
      dataSource = [],
      showFlag = 1, //是否展示
      readOnlyFlag = 0,
    } = row;
    const { requiredRule = {}, patternRule } = rules;
    const { required = false } = requiredRule;
    const mDisabled = !!readOnlyFlag;
    const req = mDisabled ? false : required;
    let pushItem = {
      fieldProps: elementCode,
      title: label,
      required: req,
      onChange: (value: any) => onChange(value, elementCode),
      defaultValue: initialValue ? initialValue : undefined,
      disabled: mDisabled,
      // positionType: label.length > 10 ? 'vertical' : 'horizontal',
      labelNumber: 7,
      hidden: !showFlag,
      rules: [
        {
          required: required,
          message: `【${label}】：` + (requiredRule?.message || ''),
        },
      ],
    } as any;
    if (patternRule) {
      pushItem.rules.push({
        pattern: new RegExp(patternRule.pattern.replace(/(\/)/g, '')),
        message: `【${label}】：` + (patternRule?.message || ''),
      });
    }
    let DFormComponent: any = null;
    switch (componentType) {
      case 'ComboAny':
      case 'ComboModal':
        DFormComponent = DformText;
        Object.assign(pushItem, {
          placeholder: !mDisabled ? '请选择' : '',
          onClick: () => {
            onClick(pushItem.fieldProps);
          },
          extra: isEdit && extra.arrRight,
        });
        break;
      case 'Input':
      case 'NumberPicker':
        DFormComponent = !mDisabled ? DformInput : DformText;
        Object.assign(pushItem, {
          placeholder: !mDisabled ? '请输入' : '',
          editable: !mDisabled,
        });
        break;
      case 'Select':
        DFormComponent = DformPicker;
        Object.assign(pushItem, {
          onClick: () => {
            if (dataSource.length > 0) return;
            Toast.fail('暂无数据');
          },
          placeholder: !mDisabled ? '请选择' : '',
          data: dataSource,
        });
        break;
      case 'TreeSelect':
        DFormComponent = AddressPicker;
        Object.assign(pushItem, {
          placeholder: !mDisabled ? '请选择' : '',
          data: dataSource,
          placeholderList: ['请选择', '请选择'],
          onChangeLevel: (values: (string | number)[]) =>
            onChangeLevel(values, elementCode),
          noData: !mDisabled && extra.addressPickerNoData,
        });
        break;
      case 'TextArea':
        DFormComponent = DformTextArea;
        Object.assign(pushItem, {
          placeholder: !mDisabled ? '请输入' : '',
          positionType: 'vertical',
          coverStyle: {
            background: 'rgba(237, 240, 245, 0.5)',
          },
          rows: 3,
          editable: !mDisabled,
        });
        break;
      case 'RadioGroup':
        DFormComponent = DformRadio;
        Object.assign(pushItem, {
          data: dataSource,
        });
        break;
      case 'DatePicker':
        DFormComponent = DformDatePicker;
        Object.assign(pushItem, {
          placeholder: !mDisabled ? '请输入' : '',
          modeType: 'date',
        });
        break;
      default:
        break;
    }
    if (changeFormObj[elementCode]) {
      Object.assign(pushItem, changeFormObj[elementCode]);
    }
    if (DFormComponent)
      eleArr.push(
        <DFormComponent key={elementCode} {...pushItem}></DFormComponent>,
      );
  });
  return eleArr;
};

//  商机流程处理环节处理
const initLinkInfoFormState = ({
  form,
  isEdit = false,
  onChange,
  onBlur,
  optionObjs = {},
  extra,
  onClick,
  formsValues = {},
  content = {},
}: {
  form: FormInstance;
  isEdit?: boolean;
  onChange: (value: any, key: string) => void;
  onBlur: (value: any, key: string) => void;
  onClick: (key: string) => void;
  optionObjs?: any;
  extra?: any;
  formsValues: any;
  content?: any; //额外参数
}) => {
  const { backTacheOptions = [] } = optionObjs;
  const { backTache } = formsValues;
  const { isNewOpp = false } = content;
  const formItemProps = { labelNumber: 7 };
  const eleArr = [
    <DformRadio
      fieldProps="auditPass"
      required={isEdit}
      title="处理结果"
      data={[
        { label: '是', value: '1' },
        { label: '否', value: '0' },
      ]}
      defaultValue="1"
      onChange={(value: any) => onChange(value, 'auditPass')}
      hidden={isNewOpp}
      {...formItemProps}
    />,
    <DformPicker
      fieldProps="backTache"
      placeholder={isEdit ? '请选择' : ''}
      title={'下一环节'}
      required={isEdit}
      data={backTacheOptions}
      disabled={!isEdit}
      alias={{
        label: 'flowNodeName',
        value: 'flowNodeCode',
      }}
      onChange={(value: any) => onChange(value, 'backTache')}
      hidden={isNewOpp}
      {...formItemProps}
    />,
    <DformText
      fieldProps="userName"
      placeholder={isEdit ? '请选择' : ''}
      title={'下一环节处理人'}
      required={isEdit}
      onClick={() => {
        isEdit && onClick('userName');
      }}
      extra={isEdit && extra.arrRight}
      hidden={backTache === 'FLOW_END'}
      {...formItemProps}
    />,
    <DformTextArea
      fieldProps="dealContent"
      placeholder={isEdit ? '请选择' : ''}
      title={'处理意见'}
      positionType="vertical"
      coverStyle={{
        background: 'rgba(237, 240, 245, 0.5)',
      }}
      rows={3}
      onChange={(value: any) => onChange(value, 'dealContent')}
      editable={isEdit}
      {...formItemProps}
    />,
    <DformRadio
      fieldProps="isRemind"
      title="是否短信通知审批人"
      required
      data={[
        {
          label: '是',
          value: '1',
        },
        {
          label: '否',
          value: '0',
        },
      ]}
      defaultValue="1"
      onChange={(value: any) => onChange(value, 'isRemind')}
      {...formItemProps}
    />,
    <DformText
      fieldProps="uploadFile"
      title="审批附件"
      extra={isEdit && extra.uploadFile}
      hidden={isNewOpp || backTache === 'FLOW_END'}
    />,
  ] as unknown as any[];

  return eleArr;
};

export const bcmcDformStates = {
  initBCMCDFormState,
  initLinkInfoFormState,
} as any;
