import { reactive, ref } from 'vue'
import { getDict } from '@/hooks/web/useDictionaries'
import { getPublicRatio } from '@/api/recruit/enterprise/position'

const dictObj = reactive({
  payUnit: [], // 薪资单位
  scale: [], // 规模
  industry: [], // 行业
  edu: [], // 学历
  exp: [], // 工作经验
  area: [], // 地区
  jobStatus: [], // 求职状态
  marital: [], // 婚姻状态
  financing: [] // 融资阶段
})
const dictList = ref([
  { 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_job_status', value: 'jobStatus', key: 'jobStatus', label: 'jobStatusName' },
  { type: 'menduner_marital_status', value: 'marital', key: 'maritalStatus', label: 'maritalStatusName' },
  { 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' },
  { type: 'positionData', value: 'position', key: 'positionId', label: 'positionName', params: {}, apiType: 'positionData', nameKey: 'nameCn', valueKey: 'id' }
])

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

export const dealDictArrayData = (res, list) => {
  res = list.map(item => {
    Object.keys(item).map(e => {
      const data = dictList.value.find(k => k.key === e)
      if (!data) return
      const valueKey = data.nameKey ? data.nameKey : 'label'
      const idKey = data.valueKey ? data.valueKey : 'value'
      if (!dictObj[data.value] || !Object.keys(dictObj[data.value]).length) return
      const result = dictObj[data.value].find(val => val[idKey] === item[e])
      if (!result) return
      item[data.label] = result[valueKey] || ''
    })
    return { ...item, active: false, select: false }
  })
  return res
}

export const dealDictObjData = (res, obj) => {
  res = obj
  Object.keys(obj).map(e => {
    const data = dictList.value.find(k => k.key === e)
    if (!data) return
    const valueKey = data.nameKey ? data.nameKey : 'label'
    const idKey = data.valueKey ? data.valueKey : 'value'
    const result = dictObj[data.value]?.find(val => val[idKey] === obj[e])
    if (!result) return
    res[data.label] = result[valueKey]
    res = { ...obj, ...res }
  })
  return res
}

// 获取单个字典对应的数值
export const getDictValueWithLabel = (dict, value, valueKey = 'value', labelKey = 'label') => {
  let result = ''
  getDict(dict).then(({ data }) => {
    if (!data || !data.length) return
    const obj = data.find(e => e[valueKey] === value)
    if (!obj) return
    result = obj[labelKey]
  })
  return result
}

// 计算众聘佣金
let data
const list = ['headhuntRate', 'recommendRate', 'cvRate'] // 平台、推荐人、投递人
const getRation = async () => {
  data = await getPublicRatio()
}
getRation()

export const commissionCalculation = (count, type) => {
  if (!data || !Object.keys(data).length) return
  const ratio = parseFloat(data[list[type]]) / 100
  const value = count * ratio
  return value % 1 === 0 ? Math.floor(value) : value.toFixed(2)
}