import { reactive, ref } from 'vue'
import { getDict } from '../hooks/useDictionaries'
import { getPublicRatio } from '@/api/user'

export const dictObj = reactive({
  payUnit: [], // 薪资单位
  scale: [], // 规模
  industry: [], // 行业
  edu: [], // 学历
  edu_extend: [], // 扩展
  exp: [], // 工作经验
  exp_extend: [], // 扩展
  area: [], // 地区
  jobStatus: [], // 求职状态
  marital: [], // 婚姻状态
  sex: [],
  jobType: [],
  areaTreeData: [],
  areaTreeData_extend: [], // 扩展
  areaTreeDataExtend: [],
  positionSecondData: [],
  eduSystemType: []
})
const dictList = ref([
  { type: 'menduner_pay_unit', value: 'payUnit', key: 'payUnit', label: 'payName' },
  { type: 'menduner_sex', value: 'sex', key: 'sex', label: 'sexName' },
  { type: 'menduner_job_type', value: 'jobType', key: 'jobType', label: 'jobTypeName' },
  { 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_education_system_type', value: 'eduSystemType', key: 'eduSystemType', label: 'eduSystemName' },
  { 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' },
  { type: 'areaTreeData', value: 'areaTreeData', key: 'areaId', label: 'areaName', params: {}, apiType: 'areaTreeData', nameKey: 'name', valueKey: 'id' },
  { type: 'areaTreeDataExtend', value: 'areaTreeDataExtend', key: 'areaId', label: 'areaName', params: {}, apiType: 'areaTreeDataExtend', nameKey: 'name', valueKey: 'id' },
  { type: 'positionTreeData', value: 'positionTreeData', key: 'positionTreeId', label: 'positionName', params: {}, apiType: 'positionTreeData', nameKey: 'nameCn', valueKey: 'id' },
  { type: 'positionSecondData', value: 'positionSecondData', key: 'positionSecondId', label: 'positionSecondName', params: {}, apiType: 'positionSecondData', nameKey: 'nameCn', valueKey: 'id' },
  { type: 'industryTreeData', value: 'industryTreeData', key: 'industryTreeId', label: 'industryName', params: {}, apiType: 'industryTreeData', 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.data

    // 字典扩展
    let obj = null
    if (['edu', 'exp'].indexOf(val.value) !== -1) obj = { label: '不限', value: -1 }
    else if (['areaTreeData'].indexOf(val.value) !== -1) obj = { name: '全国', id: -1 }
    else obj = null
    // 
    if (obj) dictObj[val.value + '_extend'] = [obj, ...data.data]
  })
  // console.log('dictObj:', dictObj)
}
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
      // 工作经验、学历为null则是不限
      const result = ['expType', 'eduType'].includes(e) && !item[e] ? { label: `${e === 'expType' ? '工作经验' : '学历' }不限` } : 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
    if (e === 'areaId' && !obj[e]) {  // areaId没有内容时展示全国
      res[data.label] = '全国'
      res = { ...obj, ...res }
      return
    }
    const valueKey = data.nameKey ? data.nameKey : 'label'
    const idKey = data.valueKey ? data.valueKey : 'value'
    const result = ['expType', 'eduType'].includes(e) && !obj[e] ? { label: `${e === 'expType' ? '工作经验' : '学历'}不限` } : 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 () => {
  const res = await getPublicRatio()
  data = res?.data
}
getRation()

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

export const commissionCalculation = (count, type) => {
  if (!data || !Object.keys(data).length) return
  // 所占的百分比
  const ratio = parseFloat(data[list[type]]) / 10 // 所占的百分比-> 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)
}



// 跳转企业详情（有优选集团页面则跳转集团页面，没有则跳转企业详情页面）
import { getWebContent } from '@/api/common'
const preferred = ref({})

const getSystemWebContent = async () => {
  const { data } = await getWebContent()
  // 优选集团
  preferred.value = data.appPreferredGroup
}
// getSystemWebContent()

export const jumpToEnterpriseDetail = async (id) => {
  if (!preferred.value || !Object.keys(preferred.value).length) await getSystemWebContent()

  // 跳转集团页面
  if (preferred.value[id] && Object.keys(preferred.value[id]).length > 0) return uni.navigateTo({ url: '/pagesB/preferredGroup/index?id=' + id })

  // 不在优选集团中跳转企业详情
  uni.navigateTo({ url: '/pagesB/companyDetail/index?id=' + id })
}