import { reactive, ref } from 'vue'
import { getDict } from '@/hooks/web/useDictionaries'
import * as CommissionRatioApi from '@/api/menduner/system/hire/commissionRatio/index'

const dictObj = reactive({
  // payUnit: [], // 薪资单位
  // scale: [], // 规模
  // industry: [], // 行业
  // edu: [], // 学历
  // exp: [], // 工作经验
  area: [], // 地区
  position: [], // 职位类型
  // 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_seek_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: 'menduner_area_type', value: 'reg', key: 'regId', label: 'regName', 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 => Number(val[idKey]) === Number(item[e]))
      if (!result) return
      item[data.label] = result[valueKey] || ''
    })
    return { ...item }
  })
  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
}

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

export const rechargeRatio = () => { return 10 }

export const commissionCalculation = (count, type) => {
  if (!data || !Object.keys(data).length) return
  // 所占的百分比
  const ratio = parseFloat(data[list[type]]) / 100 // 所占的百分比-> 50%变为0.5 (不能改)
  // 积分变成金额
  const value = count * ratio / rechargeRatio()
  return value % 1 === 0 ? Math.floor(value) : value.toFixed(2)
}

// "分"和"元" 转化。 type-> toCent:提交给后端需要乘以100; toYuan:回显需要除以100）
export const FenYuanTransform = (count, type='toYuan') => {
  if ((count - 0) === 0) return 0
  if (!count) return ''
  const Magnification = type === 'toCent' ? 100 : 1/100 
  return type === 'toCent' ? (count - 0)*Magnification : ((count - 0)*Magnification).toFixed(2)
}