import { OperatorEnum } from '@lib/shared/enums/commonEnum';
import { CustomerSearchTypeEnum } from '@lib/shared/enums/customerEnum';
import { FieldTypeEnum, FormDesignKeyEnum } from '@lib/shared/enums/formDesignEnum';

import { FilterFormItem } from '@/components/pure/crm-advance-filter/type';

import {
  addAccountPoolView,
  addBusinessView,
  addClueView,
  addContactView,
  addContractInvoicedView,
  addContractView,
  addCustomerView,
  addFollowPlanView,
  addFollowRecordView,
  addLeadPoolView,
  addOrderView,
  addPaymentPlanView,
  addPaymentRecordView,
  addQuotationView,
  deleteAccountPoolView,
  deleteBusinessView,
  deleteClueView,
  deleteContactView,
  deleteContractInvoicedView,
  deleteContractView,
  deleteCustomerView,
  deleteFollowPlanView,
  deleteFollowRecordView,
  deleteLeadPoolView,
  deleteOrderView,
  deletePaymentPlanView,
  deletePaymentRecordView,
  deleteQuotationView,
  dragAccountPoolView,
  dragBusinessView,
  dragClueView,
  dragContactView,
  dragContractInvoicedView,
  dragContractView,
  dragCustomerView,
  dragFollowPlanView,
  dragFollowRecordView,
  dragLeadPoolView,
  dragOrderView,
  dragPaymentPlanView,
  dragPaymentRecordView,
  dragQuotationView,
  enableAccountPoolView,
  enableBusinessView,
  enableClueView,
  enableContactView,
  enableContractInvoicedView,
  enableContractView,
  enableCustomerView,
  enableFollowPlanView,
  enableFollowRecordView,
  enableLeadPoolView,
  enableOrderView,
  enablePaymentPlanView,
  enablePaymentRecordView,
  enableQuotationView,
  fixedAccountPoolView,
  fixedBusinessView,
  fixedClueView,
  fixedContactView,
  fixedContractInvoicedView,
  fixedContractView,
  fixedCustomerView,
  fixedFollowPlanView,
  fixedFollowRecordView,
  fixedLeadPoolView,
  fixedOrderView,
  fixedPaymentPlanView,
  fixedPaymentRecordView,
  fixedQuotationView,
  getAccountPoolViewDetail,
  getAccountPoolViewList,
  getBusinessViewDetail,
  getBusinessViewList,
  getClueViewDetail,
  getClueViewList,
  getContactViewDetail,
  getContactViewList,
  getContractInvoicedViewDetail,
  getContractInvoicedViewList,
  getContractViewDetail,
  getContractViewList,
  getCustomerViewDetail,
  getCustomerViewList,
  getFollowPlanViewDetail,
  getFollowPlanViewList,
  getFollowRecordViewDetail,
  getFollowRecordViewList,
  getLeadPoolViewDetail,
  getLeadPoolViewList,
  getOrderViewDetail,
  getOrderViewList,
  getPaymentPlanViewDetail,
  getPaymentPlanViewList,
  getPaymentRecordViewDetail,
  getPaymentRecordViewList,
  getQuotationViewDetail,
  getQuotationViewList,
  updateAccountPoolView,
  updateBusinessView,
  updateClueView,
  updateContactView,
  updateContractInvoicedView,
  updateContractView,
  updateCustomerView,
  updateFollowPlanView,
  updateFollowRecordView,
  updateLeadPoolView,
  updateOrderView,
  updatePaymentPlanView,
  updatePaymentRecordView,
  updateQuotationView,
} from '@/api/modules';
import { TabType } from '@/hooks/useHiddenTab';
import useUserStore from '@/store/modules/user';

const userStore = useUserStore();

type ViewAction = 'list' | 'add' | 'update' | 'delete' | 'detail' | 'fixed' | 'enable' | 'drag';

export const viewApiMap: Record<ViewAction, Record<TabType, (...args: any[]) => Promise<any>>> = {
  list: {
    [FormDesignKeyEnum.CLUE]: getClueViewList,
    [FormDesignKeyEnum.CUSTOMER]: getCustomerViewList,
    [FormDesignKeyEnum.CONTACT]: getContactViewList,
    [FormDesignKeyEnum.BUSINESS]: getBusinessViewList,
    [FormDesignKeyEnum.CLUE_POOL]: getLeadPoolViewList,
    [FormDesignKeyEnum.CUSTOMER_OPEN_SEA]: getAccountPoolViewList,
    [FormDesignKeyEnum.FOLLOW_PLAN]: getFollowPlanViewList,
    [FormDesignKeyEnum.FOLLOW_RECORD]: getFollowRecordViewList,
    [FormDesignKeyEnum.CONTRACT]: getContractViewList,
    [FormDesignKeyEnum.CONTRACT_PAYMENT]: getPaymentPlanViewList,
    [FormDesignKeyEnum.OPPORTUNITY_QUOTATION]: getQuotationViewList,
    [FormDesignKeyEnum.CONTRACT_PAYMENT_RECORD]: getPaymentRecordViewList,
    [FormDesignKeyEnum.INVOICE]: getContractInvoicedViewList,
    [FormDesignKeyEnum.ORDER]: getOrderViewList,
  },
  add: {
    [FormDesignKeyEnum.CLUE]: addClueView,
    [FormDesignKeyEnum.CUSTOMER]: addCustomerView,
    [FormDesignKeyEnum.CONTACT]: addContactView,
    [FormDesignKeyEnum.BUSINESS]: addBusinessView,
    [FormDesignKeyEnum.CLUE_POOL]: addLeadPoolView,
    [FormDesignKeyEnum.CUSTOMER_OPEN_SEA]: addAccountPoolView,
    [FormDesignKeyEnum.FOLLOW_PLAN]: addFollowPlanView,
    [FormDesignKeyEnum.FOLLOW_RECORD]: addFollowRecordView,
    [FormDesignKeyEnum.CONTRACT]: addContractView,
    [FormDesignKeyEnum.CONTRACT_PAYMENT]: addPaymentPlanView,
    [FormDesignKeyEnum.OPPORTUNITY_QUOTATION]: addQuotationView,
    [FormDesignKeyEnum.CONTRACT_PAYMENT_RECORD]: addPaymentRecordView,
    [FormDesignKeyEnum.INVOICE]: addContractInvoicedView,
    [FormDesignKeyEnum.ORDER]: addOrderView,
  },
  update: {
    [FormDesignKeyEnum.CLUE]: updateClueView,
    [FormDesignKeyEnum.CUSTOMER]: updateCustomerView,
    [FormDesignKeyEnum.CONTACT]: updateContactView,
    [FormDesignKeyEnum.BUSINESS]: updateBusinessView,
    [FormDesignKeyEnum.CLUE_POOL]: updateLeadPoolView,
    [FormDesignKeyEnum.CUSTOMER_OPEN_SEA]: updateAccountPoolView,
    [FormDesignKeyEnum.FOLLOW_PLAN]: updateFollowPlanView,
    [FormDesignKeyEnum.FOLLOW_RECORD]: updateFollowRecordView,
    [FormDesignKeyEnum.CONTRACT]: updateContractView,
    [FormDesignKeyEnum.CONTRACT_PAYMENT]: updatePaymentPlanView,
    [FormDesignKeyEnum.OPPORTUNITY_QUOTATION]: updateQuotationView,
    [FormDesignKeyEnum.CONTRACT_PAYMENT_RECORD]: updatePaymentRecordView,
    [FormDesignKeyEnum.INVOICE]: updateContractInvoicedView,
    [FormDesignKeyEnum.ORDER]: updateOrderView,
  },
  delete: {
    [FormDesignKeyEnum.CLUE]: deleteClueView,
    [FormDesignKeyEnum.CUSTOMER]: deleteCustomerView,
    [FormDesignKeyEnum.CONTACT]: deleteContactView,
    [FormDesignKeyEnum.BUSINESS]: deleteBusinessView,
    [FormDesignKeyEnum.CLUE_POOL]: deleteLeadPoolView,
    [FormDesignKeyEnum.CUSTOMER_OPEN_SEA]: deleteAccountPoolView,
    [FormDesignKeyEnum.FOLLOW_PLAN]: deleteFollowPlanView,
    [FormDesignKeyEnum.FOLLOW_RECORD]: deleteFollowRecordView,
    [FormDesignKeyEnum.CONTRACT]: deleteContractView,
    [FormDesignKeyEnum.CONTRACT_PAYMENT]: deletePaymentPlanView,
    [FormDesignKeyEnum.OPPORTUNITY_QUOTATION]: deleteQuotationView,
    [FormDesignKeyEnum.CONTRACT_PAYMENT_RECORD]: deletePaymentRecordView,
    [FormDesignKeyEnum.INVOICE]: deleteContractInvoicedView,
    [FormDesignKeyEnum.ORDER]: deleteOrderView,
  },
  detail: {
    [FormDesignKeyEnum.CLUE]: getClueViewDetail,
    [FormDesignKeyEnum.CUSTOMER]: getCustomerViewDetail,
    [FormDesignKeyEnum.CONTACT]: getContactViewDetail,
    [FormDesignKeyEnum.BUSINESS]: getBusinessViewDetail,
    [FormDesignKeyEnum.CLUE_POOL]: getLeadPoolViewDetail,
    [FormDesignKeyEnum.CUSTOMER_OPEN_SEA]: getAccountPoolViewDetail,
    [FormDesignKeyEnum.FOLLOW_PLAN]: getFollowPlanViewDetail,
    [FormDesignKeyEnum.FOLLOW_RECORD]: getFollowRecordViewDetail,
    [FormDesignKeyEnum.CONTRACT]: getContractViewDetail,
    [FormDesignKeyEnum.CONTRACT_PAYMENT]: getPaymentPlanViewDetail,
    [FormDesignKeyEnum.OPPORTUNITY_QUOTATION]: getQuotationViewDetail,
    [FormDesignKeyEnum.CONTRACT_PAYMENT_RECORD]: getPaymentRecordViewDetail,
    [FormDesignKeyEnum.INVOICE]: getContractInvoicedViewDetail,
    [FormDesignKeyEnum.ORDER]: getOrderViewDetail,
  },
  fixed: {
    [FormDesignKeyEnum.CLUE]: fixedClueView,
    [FormDesignKeyEnum.CUSTOMER]: fixedCustomerView,
    [FormDesignKeyEnum.CONTACT]: fixedContactView,
    [FormDesignKeyEnum.BUSINESS]: fixedBusinessView,
    [FormDesignKeyEnum.CLUE_POOL]: fixedLeadPoolView,
    [FormDesignKeyEnum.CUSTOMER_OPEN_SEA]: fixedAccountPoolView,
    [FormDesignKeyEnum.FOLLOW_PLAN]: fixedFollowPlanView,
    [FormDesignKeyEnum.FOLLOW_RECORD]: fixedFollowRecordView,
    [FormDesignKeyEnum.CONTRACT]: fixedContractView,
    [FormDesignKeyEnum.CONTRACT_PAYMENT]: fixedPaymentPlanView,
    [FormDesignKeyEnum.OPPORTUNITY_QUOTATION]: fixedQuotationView,
    [FormDesignKeyEnum.CONTRACT_PAYMENT_RECORD]: fixedPaymentRecordView,
    [FormDesignKeyEnum.INVOICE]: fixedContractInvoicedView,
    [FormDesignKeyEnum.ORDER]: fixedOrderView,
  },
  enable: {
    [FormDesignKeyEnum.CLUE]: enableClueView,
    [FormDesignKeyEnum.CUSTOMER]: enableCustomerView,
    [FormDesignKeyEnum.CONTACT]: enableContactView,
    [FormDesignKeyEnum.BUSINESS]: enableBusinessView,
    [FormDesignKeyEnum.CLUE_POOL]: enableLeadPoolView,
    [FormDesignKeyEnum.CUSTOMER_OPEN_SEA]: enableAccountPoolView,
    [FormDesignKeyEnum.FOLLOW_PLAN]: enableFollowPlanView,
    [FormDesignKeyEnum.FOLLOW_RECORD]: enableFollowRecordView,
    [FormDesignKeyEnum.CONTRACT]: enableContractView,
    [FormDesignKeyEnum.CONTRACT_PAYMENT]: enablePaymentPlanView,
    [FormDesignKeyEnum.OPPORTUNITY_QUOTATION]: enableQuotationView,
    [FormDesignKeyEnum.CONTRACT_PAYMENT_RECORD]: enablePaymentRecordView,
    [FormDesignKeyEnum.INVOICE]: enableContractInvoicedView,
    [FormDesignKeyEnum.ORDER]: enableOrderView,
  },
  drag: {
    [FormDesignKeyEnum.CLUE]: dragClueView,
    [FormDesignKeyEnum.CUSTOMER]: dragCustomerView,
    [FormDesignKeyEnum.CONTACT]: dragContactView,
    [FormDesignKeyEnum.BUSINESS]: dragBusinessView,
    [FormDesignKeyEnum.CLUE_POOL]: dragLeadPoolView,
    [FormDesignKeyEnum.CUSTOMER_OPEN_SEA]: dragAccountPoolView,
    [FormDesignKeyEnum.FOLLOW_PLAN]: dragFollowPlanView,
    [FormDesignKeyEnum.FOLLOW_RECORD]: dragFollowRecordView,
    [FormDesignKeyEnum.CONTRACT]: dragContractView,
    [FormDesignKeyEnum.CONTRACT_PAYMENT]: dragPaymentPlanView,
    [FormDesignKeyEnum.OPPORTUNITY_QUOTATION]: dragQuotationView,
    [FormDesignKeyEnum.CONTRACT_PAYMENT_RECORD]: dragPaymentRecordView,
    [FormDesignKeyEnum.INVOICE]: dragContractInvoicedView,
    [FormDesignKeyEnum.ORDER]: dragOrderView,
  },
};

export const internalConditionsMap: Record<string, FilterFormItem[]> = {
  [CustomerSearchTypeEnum.ALL]: [
    {
      dataIndex: 'createTime',
      type: FieldTypeEnum.TIME_RANGE_PICKER,
      operator: OperatorEnum.GT,
      value: 0,
    },
  ],
  [CustomerSearchTypeEnum.DEPARTMENT]: [
    {
      dataIndex: 'departmentId',
      type: FieldTypeEnum.TREE_SELECT,
      operator: OperatorEnum.IN,
      value: [userStore.userInfo.departmentId],
      containChildIds: [],
    },
  ],
  [CustomerSearchTypeEnum.SELF]: [
    {
      dataIndex: 'owner',
      type: FieldTypeEnum.MEMBER,
      operator: OperatorEnum.IN,
      value: [userStore.userInfo.id],
      selectedUserList: [{ id: userStore.userInfo.id, name: userStore.userInfo.name }],
    },
  ],
  [CustomerSearchTypeEnum.SELF_QUOTATION]: [
    {
      dataIndex: 'createUser',
      type: FieldTypeEnum.MEMBER,
      operator: OperatorEnum.IN,
      value: [userStore.userInfo.id],
      selectedUserList: [{ id: userStore.userInfo.id, name: userStore.userInfo.name }],
    },
  ],
};

export default {};
