import { getDict } from '@/hooks/useDictionaries'

export const getText = (value, arr, itemText = 'label', itemValue = 'value') => { // 一维数组
  // console.log('getText', value, arr)
  if (!arr?.length || !(value && value !== 0)) return
  const item = arr.find(formItem => formItem[itemValue] === value)
  if (!item) return
  return item[itemText]
}

export const getBaseInfoDictOfName = async (index = 0, baseInfo, value, addKeyName) => { // 一维数组
  const test = [
    { dictType: 'menduner_area_type', itemText: 'name', itemValue: 'id', saveType: 'areaList' },
    { dictType: 'menduner_education_type', itemText: 'label', itemValue: 'value' },
    { dictType: 'menduner_exp_type', itemText: 'label', itemValue: 'value' },
    { dictType: 'menduner_sex', itemText: 'label', itemValue: 'value' },
    { dictType: 'menduner_job_type', itemText: 'label', itemValue: 'value' },
    { dictType: 'menduner_job_seek_status', itemText: 'label', itemValue: 'value' },
    { dictType: 'menduner_marital_status', itemText: 'label', itemValue: 'value' },
  ]
  const e = test[index]

  if (!value && value !== 0 && !e) baseInfo[addKeyName] = '暂无'
  const params = e.saveType === 'areaList' ? {} : null
  const { data } = await getDict(e.dictType, params, (e.saveType || 'dict'))
  if (data.code !== 0) {
    return
  }
  const item = data.data.find(formItem => Number(formItem[e.itemValue]) === Number(value))
  baseInfo[addKeyName] = item ? item[e.itemText] : '暂无'
}

// export const getBaseInfoDictOfName1 = async (baseInfo, value, addKey, itemText = 'name', itemValue = 'id') => { // 一维数组
//   if (!value && value !== 0) baseInfo[addKey] = '暂无'
//   const { data } = await getDict('menduner_area_type', {}, 'areaList')
//   const item = data.find(formItem => Number(formItem[itemValue]) === Number(value))
//   baseInfo[addKey] = item ? item[itemText] : '暂无'
// }

export const dealCanBeInputtedValueAndLabel = (formItem, data) => {
  if (!formItem.key || !formItem.itemTextName) return
  //
  formItem.value = data[formItem.key] || data[formItem.itemTextName]
  formItem[formItem.itemTextName] = data[formItem.itemTextName]
  if (data[formItem.key] && data[formItem.itemTextName]) {
    formItem.search(data[formItem.itemTextName]) // 存在id的情况下->回显下拉框列表
  }
  // if (data[formItem.key] && data[formItem.itemTextName]) {
  //   formItem.items = [{ [formItem.itemText]: data[formItem.itemTextName], [formItem.itemValue]: data[formItem.key] }] // 存在id的情况下->回显下拉框
  // }
}
export const dealCanBeInputtedSave = (formItem, params) => {
  if (formItem.value === formItem[formItem.itemTextName]) {
    params[formItem.key] = null; params[formItem.itemTextName] = formItem[formItem.itemTextName]
  }
  else { params[formItem.key] = formItem.value; params[formItem.itemTextName] = formItem[formItem.itemTextName] }
}

// 格式化企业名称、职位名称
export const formatName = (name) => {
  return name ? name.replace(/&amp;/g, '&') : ''
}
