import {
  FieldDataSourceTypeEnum,
  FieldRuleEnum,
  FieldTypeEnum,
  FormDesignKeyEnum,
} from '@lib/shared/enums/formDesignEnum';
import { useI18n } from '@lib/shared/hooks/useI18n';
import type { CommonList } from '@lib/shared/models/common';
import type { CustomFormDetail } from '@lib/shared/models/customForm';
import type { FormDesignConfigDetailParams } from '@lib/shared/models/system/module';

import {
  addClue,
  addClueFollowPlan,
  addClueFollowRecord,
  addContract,
  addCustomer,
  addCustomerContact,
  addCustomerFollowPlan,
  addCustomerFollowRecord,
  addCustomFormData,
  addFollowPlan,
  addFollowRecord,
  addInvoiced,
  addOpportunity,
  addOptFollowPlan,
  addOptFollowRecord,
  addOrder,
  addPaymentPlan,
  addPaymentRecord,
  addProduct,
  addProductPrice,
  addQuotation,
  advancedSearchOptPage,
  ClueTransitionCustomer,
  geAdvancedCustomerList,
  getAdvancedCluePoolList,
  getAdvancedCustomerContactList,
  getAdvancedOpenSeaCustomerList,
  getAdvancedSearchClueList,
  getBusinessTitleModuleForm,
  getClue,
  getClueFollowPlan,
  getClueFollowRecord,
  getClueFormConfig,
  getClueList,
  getCluePoolList,
  getClueTransitionCustomerList,
  getContactListUnderCustomer,
  getContractDetail,
  getContractDetailSnapshot,
  getContractFormConfig,
  getContractFormSnapshotConfig,
  getContractList,
  getContractPaymentPlanList,
  getCustomer,
  getCustomerContact,
  getCustomerContactFormConfig,
  getCustomerContactList,
  getCustomerFollowPlan,
  getCustomerFollowPlanFormConfig,
  getCustomerFollowRecord,
  getCustomerFollowRecordFormConfig,
  getCustomerFormConfig,
  getCustomerList,
  getCustomerOpportunityPage,
  getCustomerOrderList,
  getCustomFormDataDetail,
  getCustomFormDataPage,
  getCustomFormDetail,
  getFollowPlanDetail,
  getFollowPLanPage,
  getFollowRecordDetail,
  getFollowRecordPage,
  getInvoicedDetail,
  getInvoicedDetailSnapshot,
  getInvoicedFormConfig,
  getInvoicedFormSnapshotConfig,
  getInvoicedInContractList,
  getInvoicedList,
  getOpenSeaCustomer,
  getOpenSeaCustomerList,
  getOpportunityContactList,
  getOpportunityDetail,
  getOpportunityList,
  getOptFollowPlan,
  getOptFollowRecord,
  getOptFormConfig,
  getOrderDetail,
  getOrderDetailSnapshot,
  getOrderFormConfig,
  getOrderFormSnapshotConfig,
  getOrderInContractList,
  getOrderList,
  getPaymentPlanDetail,
  getPaymentPlanFormConfig,
  getPaymentPlanList,
  getPaymentRecordDetail,
  getPaymentRecordFormConfig,
  getPaymentRecordList,
  getPoolClue,
  getProduct,
  getProductFormConfig,
  getProductList,
  getProductPrice,
  getProductPriceFormConfig,
  getProductPriceList,
  getQuotationDetail,
  getQuotationFormConfig,
  getQuotationList,
  getQuotationSnapshotDetail,
  getQuotationSnapshotFormConfig,
  updateClue,
  updateClueFollowPlan,
  updateClueFollowRecord,
  updateContract,
  updateCustomer,
  updateCustomerContact,
  updateCustomerFollowPlan,
  updateCustomerFollowRecord,
  updateCustomFormData,
  updateFollowPlan,
  updateFollowRecord,
  updateInvoiced,
  updateOpportunity,
  updateOptFollowPlan,
  updateOptFollowRecord,
  updateOrder,
  updatePaymentPlan,
  updatePaymentRecord,
  updateProduct,
  updateProductPrice,
  updateQuotation,
} from '@/api/modules';

import type { FormCreateField, FormCreateFieldRule, FormDetail } from './types';

const { t } = useI18n();

export const multipleValueTypeList = [
  FieldTypeEnum.MEMBER_MULTIPLE,
  FieldTypeEnum.DEPARTMENT_MULTIPLE,
  FieldTypeEnum.DATA_SOURCE_MULTIPLE,
  FieldTypeEnum.CHECKBOX,
  FieldTypeEnum.SELECT_MULTIPLE,
  FieldTypeEnum.INPUT_MULTIPLE,
  FieldTypeEnum.ATTACHMENT,
  FieldTypeEnum.LINK,
];

// 所有数据源和表单
export const fullFormSettingList = [
  {
    label: t('crmFormDesign.customer'),
    dataSource: FieldDataSourceTypeEnum.CUSTOMER,
    formKey: FormDesignKeyEnum.CUSTOMER,
  },
  {
    label: t('crmFormDesign.contact'),
    dataSource: FieldDataSourceTypeEnum.CONTACT,
    formKey: FormDesignKeyEnum.CONTACT,
  },
  {
    label: t('crmFormDesign.opportunity'),
    dataSource: FieldDataSourceTypeEnum.BUSINESS,
    formKey: FormDesignKeyEnum.BUSINESS,
  },
  {
    label: t('crmFormDesign.product'),
    dataSource: FieldDataSourceTypeEnum.PRODUCT,
    formKey: FormDesignKeyEnum.PRODUCT,
  },
  {
    label: t('crmFormDesign.clue'),
    dataSource: FieldDataSourceTypeEnum.CLUE,
    formKey: FormDesignKeyEnum.CLUE,
  },
  {
    label: t('crmFormCreate.drawer.price'),
    dataSource: FieldDataSourceTypeEnum.PRICE,
    formKey: FormDesignKeyEnum.PRICE,
  },
  {
    label: t('crmFormCreate.drawer.quotation'),
    dataSource: FieldDataSourceTypeEnum.QUOTATION,
    formKey: FormDesignKeyEnum.OPPORTUNITY_QUOTATION,
  },
  {
    label: t('module.contract'),
    dataSource: FieldDataSourceTypeEnum.CONTRACT,
    formKey: FormDesignKeyEnum.CONTRACT,
  },
  {
    label: t('module.paymentPlan'),
    dataSource: FieldDataSourceTypeEnum.CONTRACT_PAYMENT,
    formKey: FormDesignKeyEnum.CONTRACT_PAYMENT,
  },
  {
    label: t('module.paymentRecord'),
    dataSource: FieldDataSourceTypeEnum.CONTRACT_PAYMENT_RECORD,
    formKey: FormDesignKeyEnum.CONTRACT_PAYMENT_RECORD,
  },
  {
    label: t('module.order'),
    dataSource: FieldDataSourceTypeEnum.ORDER,
    formKey: FormDesignKeyEnum.ORDER,
  },
  {
    label: t('module.invoiceApproval'),
    dataSource: FieldDataSourceTypeEnum.INVOICE,
    formKey: FormDesignKeyEnum.INVOICE,
  },
  {
    label: t('module.businessTitle'),
    dataSource: FieldDataSourceTypeEnum.BUSINESS_TITLE,
  },
];

export const inputDefaultFieldConfig: FormCreateField = {
  id: '',
  type: FieldTypeEnum.INPUT,
  name: 'crmFormDesign.input',
  icon: 'iconicon_single_line_text',
  fieldWidth: 1,
  showLabel: true,
  defaultValue: '',
  defaultValueType: 'custom',
  description: '',
  readable: true,
  editable: true,
  mobile: true,
  rules: [],
  formula: '',
};

export const textareaDefaultFieldConfig: FormCreateField = {
  id: '',
  type: FieldTypeEnum.TEXTAREA,
  icon: 'iconicon_multiline',
  name: 'crmFormDesign.textarea',
  fieldWidth: 1,
  showLabel: true,
  defaultValue: '',
  description: '',
  readable: true,
  editable: true,
  mobile: true,
  rules: [],
};

export const inputNumberDefaultFieldConfig: FormCreateField = {
  id: '',
  type: FieldTypeEnum.INPUT_NUMBER,
  icon: 'iconicon_hashtag_key',
  name: 'crmFormDesign.inputNumber',
  fieldWidth: 1,
  showLabel: true,
  defaultValue: null,
  description: '',
  readable: true,
  editable: true,
  mobile: true,
  rules: [],
  numberFormat: 'number',
  decimalPlaces: false,
  precision: 0,
  showThousandsSeparator: false,
  min: -999999999,
};

export const dateTimeDefaultFieldConfig: FormCreateField = {
  id: '',
  type: FieldTypeEnum.DATE_TIME,
  icon: 'iconicon_calendar1',
  name: 'crmFormDesign.dateTime',
  fieldWidth: 1,
  showLabel: true,
  defaultValue: null,
  description: '',
  readable: true,
  editable: true,
  mobile: true,
  rules: [],
  dateType: 'datetime',
  dateDefaultType: 'custom', // 自定义日期类型
};

export const radioDefaultFieldConfig: FormCreateField = {
  id: '',
  type: FieldTypeEnum.RADIO,
  icon: 'iconicon_radio',
  name: 'crmFormDesign.radio',
  fieldWidth: 1,
  showLabel: true,
  defaultValue: '',
  description: '',
  optionSource: 'custom',
  refId: null,
  options: [],
  readable: true,
  editable: true,
  mobile: true,
  rules: [],
  direction: 'vertical',
};

export const checkboxDefaultFieldConfig: FormCreateField = {
  id: '',
  type: FieldTypeEnum.CHECKBOX,
  icon: 'icona-icon_collectbeifen12',
  name: 'crmFormDesign.checkbox',
  fieldWidth: 1,
  showLabel: true,
  defaultValue: [],
  description: '',
  optionSource: 'custom',
  refId: null,
  options: [],
  readable: true,
  editable: true,
  mobile: true,
  rules: [],
  direction: 'vertical',
};

export const selectDefaultFieldConfig: FormCreateField = {
  id: '',
  type: FieldTypeEnum.SELECT,
  icon: 'iconicon_pull_down_single_choice',
  name: 'crmFormDesign.select',
  fieldWidth: 1,
  showLabel: true,
  optionSource: 'custom',
  refId: null,
  options: [],
  defaultValue: '',
  description: '',
  readable: true,
  editable: true,
  mobile: true,
  rules: [],
  multiple: false,
};

export const selectMultipleDefaultFieldConfig: FormCreateField = {
  id: '',
  type: FieldTypeEnum.SELECT_MULTIPLE,
  icon: 'iconicon_pull_down_single_choice',
  name: 'crmFormDesign.selectMultiple',
  fieldWidth: 1,
  showLabel: true,
  optionSource: 'custom',
  refId: null,
  options: [],
  defaultValue: [],
  description: '',
  readable: true,
  editable: true,
  mobile: true,
  rules: [],
  multiple: false,
};

export const memberDefaultFieldConfig: FormCreateField = {
  id: '',
  type: FieldTypeEnum.MEMBER,
  icon: 'iconicon_member_single_choice',
  name: 'crmFormDesign.memberSelect',
  fieldWidth: 1,
  showLabel: true,
  description: '',
  readable: true,
  editable: true,
  mobile: true,
  rules: [],
  defaultValue: [],
  initialOptions: [],
  hasCurrentUser: false,
  multiple: false,
};

export const memberMultipleDefaultFieldConfig: FormCreateField = {
  id: '',
  type: FieldTypeEnum.MEMBER_MULTIPLE,
  icon: 'iconicon_member_single_choice',
  name: 'crmFormDesign.memberSelectMultiple',
  fieldWidth: 1,
  showLabel: true,
  description: '',
  readable: true,
  editable: true,
  mobile: true,
  rules: [],
  defaultValue: [],
  initialOptions: [],
  hasCurrentUser: false,
  multiple: false,
};

export const departmentDefaultFieldConfig: FormCreateField = {
  id: '',
  type: FieldTypeEnum.DEPARTMENT,
  icon: 'iconicon_department_single_choice',
  name: 'crmFormDesign.departmentSelect',
  fieldWidth: 1,
  showLabel: true,
  description: '',
  readable: true,
  editable: true,
  mobile: true,
  rules: [],
  defaultValue: [],
  initialOptions: [],
  multiple: false,
};

export const departmentMultipleDefaultFieldConfig: FormCreateField = {
  id: '',
  type: FieldTypeEnum.DEPARTMENT_MULTIPLE,
  icon: 'iconicon_department_single_choice',
  name: 'crmFormDesign.departmentSelectMultiple',
  fieldWidth: 1,
  showLabel: true,
  description: '',
  readable: true,
  editable: true,
  mobile: true,
  rules: [],
  defaultValue: [],
  initialOptions: [],
  multiple: false,
};

export const dividerDefaultFieldConfig: FormCreateField = {
  id: '',
  type: FieldTypeEnum.DIVIDER,
  icon: 'iconicon_dividing_line',
  name: 'crmFormDesign.divider',
  fieldWidth: 1,
  showLabel: true,
  description: '',
  readable: true,
  editable: true,
  mobile: true,
  rules: [],
  dividerClass: 'divider--normal',
  dividerColor: '#edf0f1',
  titleColor: '#323535',
};

export const tagInputDefaultFieldConfig: FormCreateField = {
  id: '',
  type: FieldTypeEnum.INPUT_MULTIPLE,
  icon: 'iconicon_books',
  name: 'common.tag',
  fieldWidth: 1,
  showLabel: true,
  description: '',
  readable: true,
  editable: true,
  mobile: true,
  rules: [],
  defaultValue: [],
};

export const basicFields: FormCreateField[] = [
  inputDefaultFieldConfig,
  textareaDefaultFieldConfig,
  inputNumberDefaultFieldConfig,
  dateTimeDefaultFieldConfig,
  radioDefaultFieldConfig,
  checkboxDefaultFieldConfig,
  selectDefaultFieldConfig,
  selectMultipleDefaultFieldConfig,
  memberDefaultFieldConfig,
  memberMultipleDefaultFieldConfig,
  departmentDefaultFieldConfig,
  departmentMultipleDefaultFieldConfig,
  dividerDefaultFieldConfig,
  tagInputDefaultFieldConfig,
];

export const pictureDefaultFieldConfig: FormCreateField = {
  id: '',
  type: FieldTypeEnum.PICTURE,
  icon: 'iconicon_picture',
  name: 'crmFormDesign.picture',
  fieldWidth: 1,
  showLabel: true,
  description: '',
  defaultValue: [],
  readable: true,
  editable: true,
  mobile: true,
  rules: [],
  uploadLimit: 10,
  uploadSizeLimit: 20,
  pictureShowType: 'card',
  uploadLimitEnable: false,
  uploadSizeLimitEnable: false,
};

export const fileDefaultFieldConfig: FormCreateField = {
  id: '',
  type: FieldTypeEnum.ATTACHMENT,
  icon: 'iconicon_attach',
  name: 'crmFormDesign.file',
  fieldWidth: 1,
  showLabel: true,
  description: '',
  defaultValue: [],
  readable: true,
  editable: true,
  mobile: true,
  rules: [],
  onlyOne: false,
  accept: '',
  limitSize: '',
};

export const industryDefaultFieldConfig: FormCreateField = {
  id: '',
  type: FieldTypeEnum.INDUSTRY,
  icon: 'iconicon_industry',
  name: 'crmFormDesign.industry',
  fieldWidth: 1,
  showLabel: true,
  description: '',
  defaultValue: [],
  readable: true,
  editable: true,
  mobile: true,
  rules: [],
};

export const locationDefaultFieldConfig: FormCreateField = {
  id: '',
  type: FieldTypeEnum.LOCATION,
  icon: 'iconicon_map',
  name: 'crmFormDesign.location',
  fieldWidth: 1,
  showLabel: true,
  defaultValue: '',
  description: '',
  readable: true,
  editable: true,
  mobile: true,
  rules: [],
  scope: 'ALL',
  locationType: 'PCD',
};

export const phoneDefaultFieldConfig: FormCreateField = {
  id: '',
  type: FieldTypeEnum.PHONE,
  icon: 'iconicon_phone',
  name: 'crmFormDesign.phone',
  fieldWidth: 1,
  format: '255',
  showLabel: true,
  description: '',
  defaultValue: null,
  readable: true,
  editable: true,
  mobile: true,
  rules: [],
};

export const dataSourceDefaultFieldConfig: FormCreateField = {
  id: '',
  type: FieldTypeEnum.DATA_SOURCE,
  icon: 'iconicon_select_data',
  name: 'crmFormDesign.dataSourceSingle',
  fieldWidth: 1,
  showLabel: true,
  description: '',
  readable: true,
  editable: true,
  mobile: true,
  rules: [],
  multiple: false,
  defaultValue: [],
  initialOptions: [],
  dataSourceType: FieldDataSourceTypeEnum.CUSTOMER,
  combineSearch: {
    searchMode: 'OR', // 默认搜索模式
    conditions: [],
  },
};

export const dataSourceMultipleDefaultFieldConfig: FormCreateField = {
  id: '',
  type: FieldTypeEnum.DATA_SOURCE_MULTIPLE,
  icon: 'iconicon_select_data',
  name: 'crmFormDesign.dataSourceMultiple',
  fieldWidth: 1,
  showLabel: true,
  description: '',
  readable: true,
  editable: true,
  mobile: true,
  rules: [],
  multiple: false,
  defaultValue: [],
  initialOptions: [],
  dataSourceType: FieldDataSourceTypeEnum.CUSTOMER,
  combineSearch: {
    searchMode: 'OR', // 默认搜索模式
    conditions: [],
  },
};

export const serialNumberDefaultFieldConfig: FormCreateField = {
  id: '',
  defaultValue: '',
  type: FieldTypeEnum.SERIAL_NUMBER,
  icon: 'iconicon_serial_number',
  name: 'crmFormDesign.serialNumber',
  fieldWidth: 1,
  showLabel: true,
  description: '',
  readable: true,
  editable: false,
  mobile: true,
  rules: [],
  serialNumberRules: ['Opp', '-', 'yyyyMM', '-', 6],
  prefixType: 'custom',
  formula: '',
};

export const linkDefaultFieldConfig: FormCreateField = {
  id: '',
  type: FieldTypeEnum.LINK,
  icon: 'iconicon_link2',
  name: 'system.message.href',
  fieldWidth: 1,
  showLabel: true,
  description: '',
  readable: true,
  editable: true,
  mobile: true,
  openMode: 'openInNew',
  linkSource: 'userInput',
  rules: [],
};
export const formulaDefaultFieldConfig: FormCreateField = {
  id: '',
  type: FieldTypeEnum.FORMULA,
  icon: 'iconicon_arithmetic',
  name: 'crmFormDesign.calculation',
  fieldWidth: 1,
  showLabel: true,
  description: '',
  readable: true,
  editable: false,
  mobile: true,
  rules: [],
  formula: '',
  showThousandsSeparator: true,
};

export const productTableDefaultFieldConfig: FormCreateField = {
  id: '',
  type: FieldTypeEnum.SUB_PRODUCT,
  icon: 'iconicon_table',
  name: 'crmFormDesign.productTable',
  fieldWidth: 1,
  showLabel: true,
  description: '',
  readable: true,
  editable: true,
  mobile: true,
  rules: [],
  subFields: [],
  fixedColumn: 1,
};

export const priceTableDefaultFieldConfig: FormCreateField = {
  id: '',
  type: FieldTypeEnum.SUB_PRICE,
  icon: 'iconicon_table',
  name: 'crmFormDesign.quotationTable',
  fieldWidth: 1,
  showLabel: true,
  description: '',
  readable: true,
  editable: true,
  mobile: true,
  rules: [],
  subFields: [],
  fixedColumn: 1,
};

export const advancedFields: FormCreateField[] = [
  pictureDefaultFieldConfig,
  locationDefaultFieldConfig,
  phoneDefaultFieldConfig,
  dataSourceDefaultFieldConfig,
  dataSourceMultipleDefaultFieldConfig,
  serialNumberDefaultFieldConfig,
  linkDefaultFieldConfig,
  fileDefaultFieldConfig,
  industryDefaultFieldConfig,
  formulaDefaultFieldConfig,
  productTableDefaultFieldConfig,
  priceTableDefaultFieldConfig,
];

export function getFieldIcon(type: FieldTypeEnum) {
  const allFields = [...basicFields, ...advancedFields];
  const field = allFields.find((f) => f.type === type);
  return field?.icon;
}

export function getFieldTypeName(type: FieldTypeEnum) {
  const allFields = [...basicFields, ...advancedFields];
  const field = allFields.find((f) => f.type === type);
  return field?.name;
}

export const rules: FormCreateFieldRule[] = [
  {
    key: FieldRuleEnum.REQUIRED,
    required: true,
    message: 'common.notNull',
    label: 'common.required',
    trigger: ['change', 'blur'],
  },
  {
    key: FieldRuleEnum.UNIQUE,
    trigger: 'blur',
    label: 'common.onlyOne',
    validator: () => true, // 由后台验证
  },
];

export const showRulesMap: Partial<Record<FieldTypeEnum, FieldRuleEnum[]>> = {
  [FieldTypeEnum.INPUT]: [FieldRuleEnum.REQUIRED, FieldRuleEnum.UNIQUE],
  [FieldTypeEnum.TEXTAREA]: [FieldRuleEnum.REQUIRED],
  [FieldTypeEnum.INPUT_NUMBER]: [FieldRuleEnum.REQUIRED],
  [FieldTypeEnum.DATE_TIME]: [FieldRuleEnum.REQUIRED],
  [FieldTypeEnum.RADIO]: [FieldRuleEnum.REQUIRED],
  [FieldTypeEnum.CHECKBOX]: [FieldRuleEnum.REQUIRED],
  [FieldTypeEnum.SELECT]: [FieldRuleEnum.REQUIRED],
  [FieldTypeEnum.SELECT_MULTIPLE]: [FieldRuleEnum.REQUIRED],
  [FieldTypeEnum.MEMBER]: [FieldRuleEnum.REQUIRED],
  [FieldTypeEnum.MEMBER_MULTIPLE]: [FieldRuleEnum.REQUIRED],
  [FieldTypeEnum.DEPARTMENT]: [FieldRuleEnum.REQUIRED],
  [FieldTypeEnum.DEPARTMENT_MULTIPLE]: [FieldRuleEnum.REQUIRED],
  [FieldTypeEnum.DIVIDER]: [],
  [FieldTypeEnum.USER_TAG_SELECTOR]: [],
  [FieldTypeEnum.USER_SELECT]: [],
  [FieldTypeEnum.TIME_RANGE_PICKER]: [],
  [FieldTypeEnum.TREE_SELECT]: [],
  [FieldTypeEnum.INPUT_MULTIPLE]: [FieldRuleEnum.REQUIRED],
  [FieldTypeEnum.PICTURE]: [FieldRuleEnum.REQUIRED],
  [FieldTypeEnum.LOCATION]: [FieldRuleEnum.REQUIRED],
  [FieldTypeEnum.PHONE]: [FieldRuleEnum.REQUIRED, FieldRuleEnum.UNIQUE],
  [FieldTypeEnum.DATA_SOURCE]: [FieldRuleEnum.REQUIRED],
  [FieldTypeEnum.DATA_SOURCE_MULTIPLE]: [FieldRuleEnum.REQUIRED],
  [FieldTypeEnum.SERIAL_NUMBER]: [],
  [FieldTypeEnum.LINK]: [FieldRuleEnum.REQUIRED],
  [FieldTypeEnum.ATTACHMENT]: [FieldRuleEnum.REQUIRED],
  [FieldTypeEnum.INDUSTRY]: [FieldRuleEnum.REQUIRED],
  [FieldTypeEnum.FORMULA]: [],
  [FieldTypeEnum.SUB_PRODUCT]: [],
  [FieldTypeEnum.SUB_PRICE]: [],
  [FieldTypeEnum.INPUT_NUMBER_WITH_UNIT]: [],
};

export const getFormConfigApiMap: Record<
  FormDesignKeyEnum,
  (id?: string, approvalTaskId?: string) => Promise<FormDesignConfigDetailParams | CustomFormDetail>
> = {
  [FormDesignKeyEnum.CUSTOMER]: getCustomerFormConfig,
  [FormDesignKeyEnum.BUSINESS]: getOptFormConfig,
  [FormDesignKeyEnum.CONTACT]: getCustomerContactFormConfig,
  [FormDesignKeyEnum.CUSTOMER_CONTACT]: getCustomerContactFormConfig,
  [FormDesignKeyEnum.FOLLOW_PLAN_CUSTOMER]: getCustomerFollowPlanFormConfig,
  [FormDesignKeyEnum.FOLLOW_RECORD_CUSTOMER]: getCustomerFollowRecordFormConfig,
  [FormDesignKeyEnum.FOLLOW_PLAN_CLUE]: getCustomerFollowPlanFormConfig,
  [FormDesignKeyEnum.FOLLOW_RECORD_CLUE]: getCustomerFollowRecordFormConfig,
  [FormDesignKeyEnum.FOLLOW_PLAN_BUSINESS]: getCustomerFollowPlanFormConfig,
  [FormDesignKeyEnum.FOLLOW_RECORD_BUSINESS]: getCustomerFollowRecordFormConfig,
  [FormDesignKeyEnum.CLUE]: getClueFormConfig,
  [FormDesignKeyEnum.CLUE_TRANSITION_CUSTOMER]: getCustomerFormConfig,
  [FormDesignKeyEnum.CLUE_POOL]: getClueFormConfig,
  [FormDesignKeyEnum.PRODUCT]: getProductFormConfig,
  [FormDesignKeyEnum.CUSTOMER_OPEN_SEA]: getCustomerFormConfig,
  [FormDesignKeyEnum.BUSINESS_CONTACT]: getCustomerContactFormConfig,
  [FormDesignKeyEnum.CUSTOMER_OPPORTUNITY]: getOptFormConfig,
  [FormDesignKeyEnum.FOLLOW_RECORD]: getCustomerFollowRecordFormConfig,
  [FormDesignKeyEnum.FOLLOW_PLAN]: getCustomerFollowPlanFormConfig,
  [FormDesignKeyEnum.SEARCH_ADVANCED_CLUE]: getClueFormConfig,
  [FormDesignKeyEnum.SEARCH_ADVANCED_CUSTOMER]: getCustomerFormConfig,
  [FormDesignKeyEnum.SEARCH_ADVANCED_CONTACT]: getCustomerContactFormConfig,
  [FormDesignKeyEnum.SEARCH_ADVANCED_PUBLIC]: getCustomerFormConfig,
  [FormDesignKeyEnum.SEARCH_ADVANCED_CLUE_POOL]: getClueFormConfig,
  [FormDesignKeyEnum.SEARCH_ADVANCED_OPPORTUNITY]: getOptFormConfig,
  [FormDesignKeyEnum.OPPORTUNITY_QUOTATION]: getQuotationFormConfig,
  [FormDesignKeyEnum.OPPORTUNITY_QUOTATION_SNAPSHOT]: getQuotationSnapshotFormConfig,
  [FormDesignKeyEnum.CONTRACT_SNAPSHOT]: getContractFormSnapshotConfig,
  [FormDesignKeyEnum.CONTRACT]: getContractFormConfig,
  [FormDesignKeyEnum.CONTRACT_PAYMENT]: getPaymentPlanFormConfig,
  [FormDesignKeyEnum.CONTRACT_CONTRACT_PAYMENT]: getPaymentPlanFormConfig,
  [FormDesignKeyEnum.PRICE]: getProductPriceFormConfig,
  [FormDesignKeyEnum.CONTRACT_PAYMENT_RECORD]: getPaymentRecordFormConfig,
  [FormDesignKeyEnum.INVOICE]: getInvoicedFormConfig,
  [FormDesignKeyEnum.INVOICE_SNAPSHOT]: getInvoicedFormSnapshotConfig,
  [FormDesignKeyEnum.CONTRACT_INVOICE]: getInvoicedFormConfig,
  [FormDesignKeyEnum.BUSINESS_TITLE]: getBusinessTitleModuleForm,
  [FormDesignKeyEnum.ORDER]: getOrderFormConfig,
  [FormDesignKeyEnum.CONTRACT_ORDER]: getOrderFormConfig,
  [FormDesignKeyEnum.CUSTOMER_ORDER]: getOrderFormConfig,
  [FormDesignKeyEnum.ORDER_SNAPSHOT]: getOrderFormSnapshotConfig,
  [FormDesignKeyEnum.CUSTOM_FORM]: getCustomFormDetail,
};

export const createFormApi: Record<FormDesignKeyEnum, (data: any) => Promise<any>> = {
  [FormDesignKeyEnum.CUSTOMER]: addCustomer,
  [FormDesignKeyEnum.BUSINESS]: addOpportunity,
  [FormDesignKeyEnum.CONTACT]: addCustomerContact,
  [FormDesignKeyEnum.CUSTOMER_CONTACT]: addCustomerContact,
  [FormDesignKeyEnum.BUSINESS_CONTACT]: addCustomerContact,
  [FormDesignKeyEnum.FOLLOW_PLAN_CUSTOMER]: addCustomerFollowPlan,
  [FormDesignKeyEnum.FOLLOW_RECORD_CUSTOMER]: addCustomerFollowRecord,
  [FormDesignKeyEnum.FOLLOW_PLAN_CLUE]: addClueFollowPlan,
  [FormDesignKeyEnum.FOLLOW_RECORD_CLUE]: addClueFollowRecord,
  [FormDesignKeyEnum.FOLLOW_PLAN_BUSINESS]: addOptFollowPlan,
  [FormDesignKeyEnum.FOLLOW_RECORD_BUSINESS]: addOptFollowRecord,
  [FormDesignKeyEnum.FOLLOW_RECORD]: addFollowRecord,
  [FormDesignKeyEnum.FOLLOW_PLAN]: addFollowPlan,
  [FormDesignKeyEnum.CLUE]: addClue,
  [FormDesignKeyEnum.CLUE_TRANSITION_CUSTOMER]: ClueTransitionCustomer,
  [FormDesignKeyEnum.CLUE_POOL]: async () => ({}),
  [FormDesignKeyEnum.PRODUCT]: addProduct,
  [FormDesignKeyEnum.CUSTOMER_OPEN_SEA]: async () => ({}), // 公海无添加
  [FormDesignKeyEnum.CUSTOMER_OPPORTUNITY]: addOpportunity,
  [FormDesignKeyEnum.SEARCH_ADVANCED_CLUE]: addClue,
  [FormDesignKeyEnum.SEARCH_ADVANCED_CUSTOMER]: addCustomer,
  [FormDesignKeyEnum.SEARCH_ADVANCED_CONTACT]: addCustomerContact,
  [FormDesignKeyEnum.SEARCH_ADVANCED_PUBLIC]: async () => ({}),
  [FormDesignKeyEnum.SEARCH_ADVANCED_CLUE_POOL]: async () => ({}),
  [FormDesignKeyEnum.SEARCH_ADVANCED_OPPORTUNITY]: addOpportunity,
  [FormDesignKeyEnum.OPPORTUNITY_QUOTATION]: addQuotation,
  [FormDesignKeyEnum.OPPORTUNITY_QUOTATION_SNAPSHOT]: addQuotation,
  [FormDesignKeyEnum.CONTRACT]: addContract,
  [FormDesignKeyEnum.CONTRACT_SNAPSHOT]: addContract,
  [FormDesignKeyEnum.CONTRACT_PAYMENT]: addPaymentPlan,
  [FormDesignKeyEnum.CONTRACT_CONTRACT_PAYMENT]: addPaymentPlan,
  [FormDesignKeyEnum.PRICE]: addProductPrice,
  [FormDesignKeyEnum.CONTRACT_PAYMENT_RECORD]: addPaymentRecord,
  [FormDesignKeyEnum.INVOICE]: addInvoiced,
  [FormDesignKeyEnum.INVOICE_SNAPSHOT]: addInvoiced,
  [FormDesignKeyEnum.CONTRACT_INVOICE]: async () => ({}),
  [FormDesignKeyEnum.BUSINESS_TITLE]: async () => ({}),
  [FormDesignKeyEnum.ORDER]: addOrder,
  [FormDesignKeyEnum.ORDER_SNAPSHOT]: addOrder,
  [FormDesignKeyEnum.CONTRACT_ORDER]: async () => ({}),
  [FormDesignKeyEnum.CUSTOMER_ORDER]: async () => ({}),
  [FormDesignKeyEnum.CUSTOM_FORM]: addCustomFormData,
};

export const updateFormApi: Record<FormDesignKeyEnum, (data: any) => Promise<any>> = {
  [FormDesignKeyEnum.CUSTOMER]: updateCustomer,
  [FormDesignKeyEnum.BUSINESS]: updateOpportunity,
  [FormDesignKeyEnum.CONTACT]: updateCustomerContact,
  [FormDesignKeyEnum.CUSTOMER_CONTACT]: updateCustomerContact,
  [FormDesignKeyEnum.BUSINESS_CONTACT]: updateCustomerContact,
  [FormDesignKeyEnum.FOLLOW_PLAN_CUSTOMER]: updateCustomerFollowPlan,
  [FormDesignKeyEnum.FOLLOW_RECORD_CUSTOMER]: updateCustomerFollowRecord,
  [FormDesignKeyEnum.FOLLOW_PLAN_CLUE]: updateClueFollowPlan,
  [FormDesignKeyEnum.FOLLOW_RECORD_CLUE]: updateClueFollowRecord,
  [FormDesignKeyEnum.FOLLOW_PLAN_BUSINESS]: updateOptFollowPlan,
  [FormDesignKeyEnum.FOLLOW_RECORD_BUSINESS]: updateOptFollowRecord,
  [FormDesignKeyEnum.CLUE]: updateClue,
  [FormDesignKeyEnum.CLUE_TRANSITION_CUSTOMER]: async () => ({}),
  [FormDesignKeyEnum.CLUE_POOL]: async () => ({}),
  [FormDesignKeyEnum.PRODUCT]: updateProduct,
  [FormDesignKeyEnum.CUSTOMER_OPEN_SEA]: async () => ({}), // 公海无更新
  [FormDesignKeyEnum.CUSTOMER_OPPORTUNITY]: updateOpportunity,
  [FormDesignKeyEnum.FOLLOW_RECORD]: updateFollowRecord,
  [FormDesignKeyEnum.FOLLOW_PLAN]: updateFollowPlan,
  [FormDesignKeyEnum.SEARCH_ADVANCED_CLUE]: updateClue,
  [FormDesignKeyEnum.SEARCH_ADVANCED_CUSTOMER]: updateCustomer,
  [FormDesignKeyEnum.SEARCH_ADVANCED_CONTACT]: updateCustomerContact,
  [FormDesignKeyEnum.SEARCH_ADVANCED_PUBLIC]: async () => ({}),
  [FormDesignKeyEnum.SEARCH_ADVANCED_CLUE_POOL]: async () => ({}),
  [FormDesignKeyEnum.SEARCH_ADVANCED_OPPORTUNITY]: updateOpportunity,
  [FormDesignKeyEnum.OPPORTUNITY_QUOTATION]: updateQuotation,
  [FormDesignKeyEnum.OPPORTUNITY_QUOTATION_SNAPSHOT]: (data) => updateQuotation(data, data.approvalTaskId),
  [FormDesignKeyEnum.CONTRACT]: updateContract,
  [FormDesignKeyEnum.CONTRACT_SNAPSHOT]: (data) => updateContract(data, data.approvalTaskId),
  [FormDesignKeyEnum.CONTRACT_PAYMENT]: updatePaymentPlan,
  [FormDesignKeyEnum.CONTRACT_CONTRACT_PAYMENT]: updatePaymentPlan,
  [FormDesignKeyEnum.PRICE]: updateProductPrice,
  [FormDesignKeyEnum.CONTRACT_PAYMENT_RECORD]: updatePaymentRecord,
  [FormDesignKeyEnum.INVOICE]: updateInvoiced,
  [FormDesignKeyEnum.INVOICE_SNAPSHOT]: (data) => updateInvoiced(data, data.approvalTaskId),
  [FormDesignKeyEnum.CONTRACT_INVOICE]: async () => ({}),
  [FormDesignKeyEnum.BUSINESS_TITLE]: async () => ({}),
  [FormDesignKeyEnum.ORDER]: updateOrder,
  [FormDesignKeyEnum.ORDER_SNAPSHOT]: (data) => updateOrder(data, data.approvalTaskId),
  [FormDesignKeyEnum.CONTRACT_ORDER]: async () => ({}),
  [FormDesignKeyEnum.CUSTOMER_ORDER]: async () => ({}),
  [FormDesignKeyEnum.CUSTOM_FORM]: updateCustomFormData,
};

export const getFormDetailApiMap: Partial<
  Record<FormDesignKeyEnum, (id: string, approvalTaskId?: string) => Promise<FormDetail>>
> = {
  [FormDesignKeyEnum.CUSTOMER]: getCustomer,
  [FormDesignKeyEnum.BUSINESS]: getOpportunityDetail,
  [FormDesignKeyEnum.CONTACT]: getCustomerContact,
  [FormDesignKeyEnum.CUSTOMER_CONTACT]: getCustomerContact,
  [FormDesignKeyEnum.FOLLOW_PLAN_CUSTOMER]: getCustomerFollowPlan,
  [FormDesignKeyEnum.FOLLOW_RECORD_CUSTOMER]: getCustomerFollowRecord,
  [FormDesignKeyEnum.FOLLOW_PLAN_CLUE]: getClueFollowPlan,
  [FormDesignKeyEnum.FOLLOW_RECORD_CLUE]: getClueFollowRecord,
  [FormDesignKeyEnum.FOLLOW_PLAN_BUSINESS]: getOptFollowPlan,
  [FormDesignKeyEnum.FOLLOW_RECORD_BUSINESS]: getOptFollowRecord,
  [FormDesignKeyEnum.CLUE]: getClue,
  [FormDesignKeyEnum.CLUE_POOL]: getPoolClue,
  [FormDesignKeyEnum.PRODUCT]: getProduct,
  [FormDesignKeyEnum.CUSTOMER_OPEN_SEA]: getOpenSeaCustomer,
  [FormDesignKeyEnum.CUSTOMER_OPPORTUNITY]: getOpportunityDetail,
  [FormDesignKeyEnum.FOLLOW_RECORD]: getFollowRecordDetail,
  [FormDesignKeyEnum.FOLLOW_PLAN]: getFollowPlanDetail,
  [FormDesignKeyEnum.SEARCH_ADVANCED_CLUE]: getClue,
  [FormDesignKeyEnum.SEARCH_ADVANCED_CUSTOMER]: getCustomer,
  [FormDesignKeyEnum.SEARCH_ADVANCED_CONTACT]: getCustomerContact,
  [FormDesignKeyEnum.SEARCH_ADVANCED_PUBLIC]: getOpenSeaCustomer,
  [FormDesignKeyEnum.SEARCH_ADVANCED_CLUE_POOL]: getPoolClue,
  [FormDesignKeyEnum.SEARCH_ADVANCED_OPPORTUNITY]: getOpportunityDetail,
  [FormDesignKeyEnum.CONTRACT]: getContractDetail,
  [FormDesignKeyEnum.CONTRACT_SNAPSHOT]: getContractDetailSnapshot,
  [FormDesignKeyEnum.CONTRACT_PAYMENT]: getPaymentPlanDetail,
  [FormDesignKeyEnum.CONTRACT_CONTRACT_PAYMENT]: getPaymentPlanDetail,
  [FormDesignKeyEnum.OPPORTUNITY_QUOTATION]: getQuotationDetail,
  [FormDesignKeyEnum.OPPORTUNITY_QUOTATION_SNAPSHOT]: getQuotationSnapshotDetail,
  [FormDesignKeyEnum.PRICE]: getProductPrice,
  [FormDesignKeyEnum.CONTRACT_PAYMENT_RECORD]: getPaymentRecordDetail,
  [FormDesignKeyEnum.INVOICE]: getInvoicedDetail,
  [FormDesignKeyEnum.INVOICE_SNAPSHOT]: getInvoicedDetailSnapshot,
  [FormDesignKeyEnum.ORDER]: getOrderDetail,
  [FormDesignKeyEnum.CONTRACT_ORDER]: getOrderDetailSnapshot,
  [FormDesignKeyEnum.CUSTOMER_ORDER]: getOrderDetailSnapshot,
  [FormDesignKeyEnum.ORDER_SNAPSHOT]: getOrderDetailSnapshot,
  [FormDesignKeyEnum.CUSTOM_FORM]: getCustomFormDataDetail,
};

export const getFormListApiMap: Partial<Record<FormDesignKeyEnum, (data: any) => Promise<CommonList<any>>>> = {
  [FormDesignKeyEnum.CUSTOMER]: getCustomerList,
  [FormDesignKeyEnum.BUSINESS]: getOpportunityList,
  [FormDesignKeyEnum.CONTACT]: getCustomerContactList,
  [FormDesignKeyEnum.CUSTOMER_CONTACT]: getContactListUnderCustomer,
  [FormDesignKeyEnum.CLUE]: getClueList,
  [FormDesignKeyEnum.CLUE_POOL]: getCluePoolList,
  [FormDesignKeyEnum.PRODUCT]: getProductList,
  [FormDesignKeyEnum.CUSTOMER_OPEN_SEA]: getOpenSeaCustomerList,
  [FormDesignKeyEnum.BUSINESS_CONTACT]: getOpportunityContactList,
  [FormDesignKeyEnum.CUSTOMER_OPPORTUNITY]: getCustomerOpportunityPage,
  [FormDesignKeyEnum.FOLLOW_RECORD]: getFollowRecordPage,
  [FormDesignKeyEnum.FOLLOW_PLAN]: getFollowPLanPage,
  [FormDesignKeyEnum.CLUE_TRANSITION_CUSTOMER]: getClueTransitionCustomerList,
  [FormDesignKeyEnum.SEARCH_ADVANCED_CLUE]: getAdvancedSearchClueList,
  [FormDesignKeyEnum.SEARCH_ADVANCED_CUSTOMER]: geAdvancedCustomerList,
  [FormDesignKeyEnum.SEARCH_ADVANCED_CONTACT]: getAdvancedCustomerContactList,
  [FormDesignKeyEnum.SEARCH_ADVANCED_PUBLIC]: getAdvancedOpenSeaCustomerList,
  [FormDesignKeyEnum.SEARCH_ADVANCED_CLUE_POOL]: getAdvancedCluePoolList,
  [FormDesignKeyEnum.SEARCH_ADVANCED_OPPORTUNITY]: advancedSearchOptPage,
  [FormDesignKeyEnum.CONTRACT]: getContractList,
  [FormDesignKeyEnum.CONTRACT_PAYMENT]: getPaymentPlanList,
  [FormDesignKeyEnum.CONTRACT_CONTRACT_PAYMENT]: getContractPaymentPlanList,
  [FormDesignKeyEnum.OPPORTUNITY_QUOTATION]: getQuotationList,
  [FormDesignKeyEnum.PRICE]: getProductPriceList,
  [FormDesignKeyEnum.CONTRACT_PAYMENT_RECORD]: getPaymentRecordList,
  [FormDesignKeyEnum.INVOICE]: getInvoicedList,
  [FormDesignKeyEnum.CONTRACT_INVOICE]: getInvoicedInContractList,
  [FormDesignKeyEnum.ORDER]: getOrderList,
  [FormDesignKeyEnum.CONTRACT_ORDER]: getOrderInContractList,
  [FormDesignKeyEnum.CUSTOMER_ORDER]: getCustomerOrderList,
  [FormDesignKeyEnum.CUSTOM_FORM]: getCustomFormDataPage,
};

export const dataSourceFilterFormKeyMap: Partial<Record<FieldDataSourceTypeEnum, FormDesignKeyEnum>> = {
  [FieldDataSourceTypeEnum.CUSTOMER]: FormDesignKeyEnum.CUSTOMER,
  [FieldDataSourceTypeEnum.BUSINESS]: FormDesignKeyEnum.BUSINESS,
  [FieldDataSourceTypeEnum.CONTACT]: FormDesignKeyEnum.CONTACT,
  [FieldDataSourceTypeEnum.PRODUCT]: FormDesignKeyEnum.PRODUCT,
  [FieldDataSourceTypeEnum.CLUE]: FormDesignKeyEnum.CLUE,
  [FieldDataSourceTypeEnum.PRICE]: FormDesignKeyEnum.PRICE,
  [FieldDataSourceTypeEnum.CONTRACT]: FormDesignKeyEnum.CONTRACT,
  [FieldDataSourceTypeEnum.CONTRACT_PAYMENT]: FormDesignKeyEnum.CONTRACT_PAYMENT,
  [FieldDataSourceTypeEnum.CONTRACT_PAYMENT_RECORD]: FormDesignKeyEnum.CONTRACT_PAYMENT_RECORD,
  [FieldDataSourceTypeEnum.QUOTATION]: FormDesignKeyEnum.OPPORTUNITY_QUOTATION,
  [FieldDataSourceTypeEnum.ORDER]: FormDesignKeyEnum.ORDER,
  [FieldDataSourceTypeEnum.INVOICE]: FormDesignKeyEnum.INVOICE,
};
