const title_dict = {
  origonNm: '省份地区',
  belongBusinessList: '所在行业',
  startDateIntervalList: '成立年限',
  statusList: '经营状态',
  registerAll: '注册资本',
  giveAll: '实缴资本',
  isPhoneNumber: '有无联系电话',
  isEmail: '有无邮箱地址',
  companyTypeList: '机构类型',
  isHasBranchFlag: '是否有分支机构',
  isBranchFlag: '是否为分支机构',
  businessInfoPenInsurIntervalList: '养老保险参保人数',
  businessInfoMediInsurIntervalList: '医疗保险参保人数',
  nsxydjList: '纳税信用等级',
  businessInfoImExportFlag: '有无进出口信息',
  businessInfoImExportCreditGradeList: '海关认证',
  businessInfoGovSupportFlag: '有无政府扶持和奖励',
  businessInfoFinaInfoFlag: '有无融资信息',
  businessInfoFinaRoundList: '融资轮次',
  intelPropRightTrademarkInfoFlag: '有无商标信息',
  intelPropRightPatentInfoFlag: '有无专利信息',
  intelPropRightSoftCopyrightFlag: '有无软著&作者',
  zzzsCodeList: '资质证书',
  technologyLvlTypeList: '类型',
  technologyLvlStateList: '状态',
  technologyLvlLevelList: '级别',
  capMarketListInfoFlag: '有无上市信息',
  riskCharDisInfoFlag: '有无失信信息',
  riskCharBankruptcyLiquiFlag: '有无破产清算',
  riskCharChangeRecordFlag: '有无变更记录',
  riskCharDisPersonFlag: '有无失信被执行人',
  riskCharSncFlag: '有无严重失信违法',
  isSswf: '有无税收违法',
  riskCharRhcFlag: '有无限制高消费',
  isJyyc: '有无经营异常',
  operatRiskAdminPenaltyFlag: '有无行政处罚',
  operatRiskEnvirPenaltyFlag: '有无环保处罚',
  operatRiskOweTaxInfoFlag: '有无欠税信息',
  operatRiskFinalCaseFlag: '有无终本案件',
  operatRiskUnfinishCaseFlag: '有无未结案件',
  isFrbg: '有无法人变更',
  isGqdj: '有无股权冻结'
}

let options_dict = {}

// 直接展示数组的字段
const direct_show_dict = ['origonNm', 'belongBusinessList', 'statusList', 'companyTypeList', 'businessInfoImExportCreditGradeList', 'zzzsCodeList', 'technologyLvlTypeList', 'technologyLvlStateList', 'technologyLvlLevelList', 'nsxydjList', 'businessInfoFinaRoundList']

// 是否、有无字段
export const if_show_dict = ['isPhoneNumber', 'isEmail', 'isHasBranchFlag', 'isBranchFlag', 'businessInfoImExportFlag', 'businessInfoGovSupportFlag', 'businessInfoFinaInfoFlag', 'intelPropRightTrademarkInfoFlag', 'intelPropRightPatentInfoFlag', 'intelPropRightSoftCopyrightFlag', 'capMarketListInfoFlag', 'riskCharDisInfoFlag', 'riskCharBankruptcyLiquiFlag', 'riskCharChangeRecordFlag', 'riskCharDisPersonFlag', 'riskCharSncFlag', 'isSswf', 'riskCharRhcFlag', 'isJyyc', 'operatRiskAdminPenaltyFlag', 'operatRiskEnvirPenaltyFlag', 'operatRiskOweTaxInfoFlag', 'operatRiskFinalCaseFlag', 'operatRiskUnfinishCaseFlag', 'isFrbg', 'isGqdj']

function isEmpty(val) {
  if (val === null || val === undefined) return true
  if (typeof val === 'string') return val.trim() === ''
  if (Array.isArray(val)) return val.length === 0
}

function formatSearchData(formValues) {
  formValues.registCapiIntervalList = formValues.registCapiIntervalList || []
  formValues.recCapIntervalList = formValues.recCapIntervalList || []
  const newValues = {
    ...formValues
  }
  if (formValues.minYL != undefined && formValues.maxYL != undefined && formValues.minYL > formValues.maxYL) {
    formValues.minYL = ''
    formValues.maxYL = ''
  }
  if (formValues.minYN != undefined && formValues.maxYN != undefined && formValues.minYN > formValues.maxYN) {
    formValues.minYN = ''
    formValues.maxYN = ''
  }
  // 行业
  if (Array.isArray(formValues.belongBusinessList) && formValues.belongBusinessList.length > 0) {
    newValues.belongBusinessList = formValues.belongBusinessList.map(item => item[0])
  }

  newValues.statusList = formValues.statusList.concat(formValues.otherJYStatus)

  let companyTypeList = newValues.companyTypeList
  // 企业处理
  if (Array.isArray(formValues.companyType) && formValues.companyType.length > 0) {
    const newCompanyType = formValues.companyType.map(item => item[item.length - 1])
    companyTypeList = companyTypeList.concat(newCompanyType)
  }
  // 事业单位处理
  if (Array.isArray(formValues.SYStatus) && formValues.SYStatus.length > 0) {
    const newSYStatus = formValues.SYStatus.map(item => item[0])
    companyTypeList = companyTypeList.concat(newSYStatus)
  }
  newValues.companyTypeList = companyTypeList

  return newValues
}

function findLabelByValue(tree, targetValue) {
  if (!tree) return null

  if (tree.value === targetValue) {
    return tree.label
  }

  if (tree.children && tree.children.length > 0) {
    for (const child of tree.children) {
      const result = findLabelByValue(child, targetValue)
      if (result) return result
    }
  }

  return null
}

function findLabelTreeList(treeList, targetValue) {
  for (const tree of treeList) {
    const result = findLabelByValue(tree, targetValue)
    if (result) return result
  }
  return null
}

export function getSearchList(data, dict) {
  options_dict = {
    ...options_dict,
    ...dict
  }
  const formatData = formatSearchData(data)
  const finalResult = []

  for (const i in title_dict) {
    if (direct_show_dict.includes(i)) {
      if (isEmpty(formatData[i])) {
        continue
      }
      if (i === 'origonNm') {
        finalResult.push({
          key: i,
          label: title_dict[i],
          text: formatData[i].join('')
        })
      } else if (i === 'statusList') {
        finalResult.push({
          key: i,
          label: title_dict[i],
          text: `${formatData.statusInclude === 0 ? '不包含' : '包含'}${formatData[i].join('、')}`
        })
      } else if (i === 'companyTypeList') {
        const labelList = formatData.companyTypeList.map((item) => findLabelTreeList(options_dict['companyTypeList'], item))
        finalResult.push({
          key: i,
          label: title_dict[i],
          text: `${formatData.companyTypeInclude === 0 ? '不包含' : '包含'}${labelList.join('、')}`
        })
      } else {
        finalResult.push({
          key: i,
          label: title_dict[i],
          text: `${formatData[i].join('、')}`
        })
      }
    } else if (if_show_dict.includes(i)) {
      if (isEmpty(formatData[i])) {
        continue
      }
      if (i === 'isHasBranchFlag' || i === 'isBranchFlag') {
        finalResult.push({
          key: i,
          label: title_dict[i],
          text: formatData[i] === 1 ? '是' : '否'
        })
      } else {
        finalResult.push({
          key: i,
          label: title_dict[i],
          text: formatData[i] === 1 ? '有' : '无'
        })
      }
    } else if (i === 'startDateIntervalList') {
      if ((isEmpty(formatData[i]) && isEmpty(formatData.startYear) && isEmpty(formatData.endYear)) || Number(formatData.startYear) > Number(formatData.endYear)) {
        continue
      }
      const labelList = formatData['startDateIntervalList'].map((item) => findLabelTreeList(options_dict['groupYearArr'], item) || item)
      if (formatData.startYear && formatData.endYear) {
        if (Number(formatData.startYear) <= Number(formatData.endYear)) {
          labelList.push(`${formatData.startYear}年-${formatData.endYear}年`)
        }
      }
      finalResult.push({
        key: i,
        label: title_dict[i],
        text: labelList.join('、')
      })
    } else if (i === 'registerAll') {
      if ((isEmpty(formatData[i]) && isEmpty(formatData.minRegiterMoney) && isEmpty(formatData.maxRegiterMoney)) || Number(formatData.minRegiterMoney) > Number(formatData.maxRegiterMoney)) {
        continue
      }
      const labelList = formatData['registerAll'].map((item) => findLabelTreeList(options_dict['registerMoneyArr'], item.toString()) || item)
      if (formatData.minRegiterMoney != undefined && formatData.maxRegiterMoney != undefined) {
        if (formatData.minRegiterMoney <= formatData.maxRegiterMoney) {
          const unit = formatData.registerUnit === '2' ? '亿' : '万'
          labelList.push(`${formatData.minRegiterMoney}${unit}-${formatData.maxRegiterMoney}${unit}`)
        }
      }
      finalResult.push({
        key: i,
        label: title_dict[i],
        text: labelList.join('、')
      })
    } else if (i === 'giveAll') {
      if ((isEmpty(formatData[i]) && isEmpty(formatData.minGiveMoney) && isEmpty(formatData.maxGiveMoney)) || Number(formatData.minGiveMoney) > Number(formatData.maxGiveMoney)) {
        continue
      }
      const labelList = formatData['giveAll'].map((item) => findLabelTreeList(options_dict['registerMoneyArr'], item) || item)
      if (formatData.minGiveMoney != undefined && formatData.maxGiveMoney != undefined) {
        if (formatData.minGiveMoney <= formatData.maxGiveMoney) {
          const unit = formatData.giveUnit === '2' ? '亿' : '万'
          labelList.push(`${formatData.minGiveMoney}${unit}-${formatData.maxGiveMoney}${unit}`)
        }
      }
      finalResult.push({
        key: i,
        label: title_dict[i],
        text: labelList.join('、')
      })
    } else if (i === 'businessInfoPenInsurIntervalList') {
      if ((isEmpty(formatData[i]) && isEmpty(formatData.minYL) && isEmpty(formatData.maxYL)) || Number(formatData.minYL) > Number(formatData.maxYL)) {
        continue
      }
      const labelList = formatData['businessInfoPenInsurIntervalList'].map((item) => findLabelTreeList(options_dict['baoxianOptions'], item) || item)
      if (!isEmpty(formatData.minYL) && !isEmpty(formatData.maxYL)) {
        if (formatData.minYL <= formatData.maxYL) {
          labelList.push(`${formatData.minYL}-${formatData.maxYL}人`)
        }
      }
      finalResult.push({
        key: i,
        label: title_dict[i],
        text: labelList.join('、')
      })
    } else if (i === 'businessInfoMediInsurIntervalList') {
      if ((isEmpty(formatData[i]) && isEmpty(formatData.minYN) && isEmpty(formatData.maxYN)) || Number(formatData.minYN) > Number(formatData.maxYN)) {
        continue
      }
      const labelList = formatData['businessInfoMediInsurIntervalList'].map((item) => findLabelTreeList(options_dict['baoxianOptions'], item) || item)
      if (formatData.minYN != undefined && formatData.maxYN != undefined) {
        if (formatData.minYN <= formatData.maxYN) {
          labelList.push(`${formatData.minYN}-${formatData.maxYN}人`)
        }
      }
      finalResult.push({
        key: i,
        label: title_dict[i],
        text: labelList.join('、')
      })
    }
  }

  return finalResult
}
