import { getBatchDynamicData } from '@/service/index/api'

export const calcTypeName = (type: number) => {
  const style = {
    2: {
      text: '冲',
      color: '#EB5241',
      bgColor: 'rgba(235,82,65,0.15)',
      roundedBgColor: '#E75859',
    },
    1: {
      text: '稳',
      color: '#FA8E23',
      bgColor: 'rgba(250,142,35,0.15)',
      roundedBgColor: '#FF8800',
    },
    0: {
      text: '保',
      color: '#15C496',
      bgColor: 'rgba(21,196,150,0.15)',
      roundedBgColor: '#34C724',
    },
  }[type] || { text: '保', color: '#15C496', bgColor: 'rgba(21,196,150,0.15)' }

  return {
    text: style.text,
    style: {
      color: style.color,
      backgroundColor: style.bgColor,
    },
    roundedBgColor: style.roundedBgColor,
  }
}
export const coverTypeModel = (tModel: Record<keyof typeof TYPE_LABELS, number>, total: number) => {
  const TYPE_LABELS = {
    c: '冲',
    w: '稳',
    b: '保',
  } as const

  let _result = Object.entries(TYPE_LABELS).map(([key, label]) => ({
    name: `${label}(${tModel[key]})`,
    value: key === 'b' ? '0' : key === 'w' ? '1' : '2',
  }))
  _result.unshift({
    name: `全部${total}`,
    value: '-1',
  })
  return _result
}

export const countModel = (list: any[]) => {
  const tModel = ref([
    { type: 2, count: 0 },
    { type: 1, count: 0 },
    { type: 0, count: 0 },
  ])

  list.forEach((item) => {
    item.vItems.forEach((vItem) => {
      const target = tModel.value.find((t) => t.type === vItem.type)
      if (target) target.count++
    })
  })

  return { tModel }
}

export const useScore = (provinceCode, batchName, requireSubject) => {
  const score = ref(0)
  const minScore = ref(0)
  const maxScore = ref(0)
  const _requireSubject = requireSubject.length > 0 ? requireSubject[0].name : ''
  getBatchDynamicData({ LocationCode: provinceCode, Course: _requireSubject }).then((resp) => {
    if (resp.code === 200) {
      const _result = resp.result as { batches: any[]; maxScore: number; minScore: number }
      if (_result.batches.length > 0) {
        const _score = _result.batches.find((item) => item.batch === batchName)?.score || 0
        score.value = _score
      }

      minScore.value = _result.minScore
      maxScore.value = _result.maxScore
    }
  })
  return { score, minScore, maxScore }
}
