import React from 'react';
import { Enclosure, ProjectInfo } from '../../index';

// type: general | nestCard | custom

// 商机详情可编辑环节
const OPP_DETAILS_EDIT = {
  NEW_OPP_AUDIT: '商机审核',
  NEW_OPP_FOLLOW: '商机跟进',
} as any;

// 自定义产品信息
const customProjectInfo = (props: any) => {
  return {
    type: 'custom',
    customChildren: <ProjectInfo {...props} />,
  };
};

// 自定义附件信息
const customEnclosure = (props: any) => {
  return {
    type: 'custom',
    customChildren: <Enclosure {...props} />,
  };
};

const dealTacheInfo = (props: any) => {
  return {
    type: 'custom',
    customChildren: <DealTacheInfo {...props} />,
  };
};

// 流程环节处理
const initLinkInfoFormState = (props: any) => {
  return {
    title: '环节处理',
    type: 'general',
    formStateKey: 'initLinkInfoFormState',
    isEdit: props.isEdit,
  };
};

// 初始化BCMC
const initBCMCDFormState = (props: any) => {
  return {
    ...props,
    type: 'general',
    formStateKey: 'initBCMCDFormState',
    isEdit: props.isEdit,
  };
};

// 已经支持的特殊区块对象
const specialObj = {
  SaleOppHLJ_fileList: customEnclosure,
  OppPurchasing_noticeFlie: customEnclosure,
  ProdAndOfferList: customProjectInfo,
  DealTacheSelectBscDefault: initLinkInfoFormState,
  OppPlanSpuuort_commuSupportPlanFlie: customEnclosure,
  OppApproval_projectApprovalFile: customEnclosure,
  // ObjFlowView: customFlowChart,
} as any;

// 返回特殊区块
const renderSpecialBlock = (props: any, currentBlockInfo: any) => {
  const {
    flowNodeCode,
    queryType,
    oppFileRef,
    oppFilesObj = {},
    aCSupportProgramFileRef,
    isEdit = false,
  } = props;
  // console.log('oppFilesObj', oppFilesObj);

  const { oppAttachmentList = [] } = oppFilesObj;
  const {
    elementCode,
    componentCode,
    componentType,
    disabled = false,
    editable = true,
    label,
    tableProps = {},
  } = currentBlockInfo;
  const mDisabled = disabled || !editable;
  let renderItem = undefined as any;
  let key = componentType === 'Async' ? componentCode : componentType;
  switch (key) {
    case 'Table':
      renderItem = customEnclosure({
        ...props,
        isEdit: tableProps.add,
        title: label,
        cRef: oppFileRef,
        propsFileList: oppFilesObj[elementCode] || [],
        currentBlockInfo,
        saveKey: elementCode,
      });
      break;
    case 'ProdAndOfferList':
      renderItem = customProjectInfo({ ...props, isEdit: isEdit });
      break;
    case 'DealTacheSelectBscDefault':
      renderItem = initLinkInfoFormState({ isEdit: isEdit });
      break;
    default:
      break;
  }
  return renderItem;
};

// 初始化Bcmc数据
const initBcmcData = (props: any) => {
  const pageContent = [] as any[];
  const { bcmcData = {}, queryType = '', flowNodeCode = '' } = props;
  console.log('bcmcData', bcmcData);
  const { elementList = [] as any[] } = bcmcData;
  elementList.forEach((row: any, index: number) => {
    const {
      componentType,
      elementCode,
      componentCode,
      disabled = false,
      editable = true,
      visible = true,
      label,
      properties = [],
    } = row;
    let mDisabled = false;
    if (queryType === 'handle' && OPP_DETAILS_EDIT[flowNodeCode]) {
      mDisabled = true;
    }
    if (visible) {
      switch (componentType) {
        case 'Async':
          if (specialObj[componentCode]) {
            if (componentCode === 'DealTacheSelectBscDefault') {
              if (queryType === 'handle' && renderSpecialBlock(props, row)) {
                pageContent.push(dealTacheInfo({ ...props, isEdit: true }));
              }
            } else {
              if (renderSpecialBlock(props, row)) {
                pageContent.push(
                  renderSpecialBlock({ ...props, isEdit: mDisabled }, row),
                );
              }
            }
          }
          break;
        case 'Table':
          if (specialObj[elementCode]) {
            if (renderSpecialBlock(props, row)) {
              pageContent.push(
                renderSpecialBlock({ ...props, isEdit: mDisabled }, row),
              );
            }
          }
          break;
        case 'Card':
          pageContent.push(
            initBCMCDFormState({
              ...props,
              isEdit: !mDisabled,
              title: label,
              properties,
            }),
          );
          break;
        default:
          break;
      }
    }
  });
  console.log('pageContent', pageContent);
  return pageContent;
};

// 商机详情
// 可编辑条件：1:待办情况下，2: 商机审核和商机跟进环节下
const businessDetails = (props: any) => {
  const {
    flowNodeCode = '',
    queryType = '',
    aCSupportProgramFileRef,
    dealFileRef,
    oppFilesObj,
  } = props;
  // console.log('oppFilesObj', oppFilesObj);

  const pageContent = [] as any[];
  const { bcmcData = {} } = props;
  const { elementList = [] as any[] } = bcmcData;
  elementList.forEach((row: any, index: number) => {
    const {
      componentType,
      elementCode,
      componentCode,
      disabled = false,
      editable = true,
      visible = true,
      label,
      properties = [],
    } = row;
    let mDisabled = false;
    if (queryType === 'handle' && OPP_DETAILS_EDIT[flowNodeCode]) {
      mDisabled = true;
    }
    if (visible) {
      switch (componentType) {
        case 'Async':
          if (specialObj[componentCode]) {
            if (componentCode === 'DealTacheSelectBscDefault') {
              if (queryType === 'handle' && renderSpecialBlock(props, row)) {
                pageContent.push(dealTacheInfo({ ...props, isEdit: true }));
              }
            } else {
              if (renderSpecialBlock(props, row)) {
                pageContent.push(
                  renderSpecialBlock({ ...props, isEdit: mDisabled }, row),
                );
              }
            }
          }
          break;
        case 'Table':
          if (renderSpecialBlock(props, row)) {
            pageContent.push(
              renderSpecialBlock({ ...props, isEdit: mDisabled }, row),
            );
          }
          break;
        case 'Card':
          pageContent.push(
            initBCMCDFormState({
              ...props,
              isEdit: mDisabled,
              title: label,
              properties,
            }),
          );
          break;
        default:
          break;
      }
    }
  });
  return pageContent;
};

const pagesObj = {
  businessDetails,
  initBcmcData,
};

export { pagesObj };
