import { reactive } from 'vue'
import { getDict } from '@/hooks/web/useDictionaries'

const dictObj = reactive({
  payUnit: [], // 薪资单位
  scale: [], // 规模
  industry: [], // 行业
  edu: [], // 学历
  exp: [], // 工作经验
  area: [], // 地区
  financing: [] // 融资阶段
})
const dictList = [
  { type: 'menduner_pay_unit', value: 'payUnit', key: 'payUnit', label: 'payName' },
  { type: 'menduner_financing_status', value: 'financing', key: 'financingStatus', label: 'financingName' },
  { type: 'menduner_scale', value: 'scale', key: 'scale', label: 'scaleName' },
  { type: 'menduner_industry_type', value: 'industry', key: 'industryId', label: 'industryName', params: {}, apiType: 'industryList', nameKey: 'nameCn', valueKey: 'id' },
  { type: 'menduner_education_type', value: 'edu', key: 'eduType', label: 'eduName' },
  { type: 'menduner_exp_type', value: 'exp', key: 'expType', label: 'expName' },
  { type: 'menduner_area_type', value: 'area', key: 'areaId', label: 'areaName', params: {}, apiType: 'areaList', nameKey: 'name', valueKey: 'id' }
]

// 字典
const getDictList = async () => {
  dictList.forEach(async (val) => {
    const { data } = await getDict(val.type, val.params, val.apiType)
    dictObj[val.value] = data
  })
}
const getData = async () => {
  await getDictList()
}
getData()

export const dealDictData = (res, list) => {
  dictList.forEach(item => {
    const valueKey = item.nameKey ? item.nameKey : 'label'
    const idKey = item.valueKey ? item.valueKey : 'value'
    if (Array.isArray(list)) {
      res = list.map(e => {
        const obj = dictObj[item.value].find(k => Number(k[idKey]) === Number(e[item.key]))
        if (!obj) return
        e[item.label] = obj[valueKey] || ''
        e.active = false
        return e
      })
      return
    }
    for(let i in list) {
      if (i === item.key) {
        const obj = dictObj[item.value].find(k => Number(k[idKey]) === Number(list[i]))
        if (!obj) return
        res[item.label] = obj[valueKey]
      }
    }
  })
  return res
}