import http from '@/utils/http'
import store, { AppDispatch } from '..'
import { A2dataSonType, A2dataType } from '@/pages/A2statistics/data'
import { D1listType } from '@/pages/D1dict/data'
/**
 * 数据统计-获取数据
 */
export const A2_APIgetlist = (): any => {
  return async (dispatch: AppDispatch) => {
    const res = await http.get('cms/bench/count')
    if (res.code === 0) {
      // 获取完整字典数据
      const dictArrAll = store.getState().D1dict.list

      const dictArr: D1listType[] = []

      dictArrAll.forEach(v => {
        if (v.children && v.children.length) {
          v.children.forEach(v2 => {
            dictArr.push(v2)
          })
        }
      })

      const objAll = res.data

      const obj = { total: 0, data: [] } as A2dataType

      for (const k in objAll) {
        if (k === 'total') obj.total = objAll[k]
        else {
          const objSon = objAll[k]
          const arrSon: A2dataSonType[] = []
          for (const k2 in objSon) {
            const objName = dictArr.find(v => v.id === k2)

            arrSon.push({ name: objName ? objName.name : k2, value: objSon[k2] })
          }
          obj.data.push({ name: k, son: arrSon })
        }
      }

      dispatch({ type: 'A2/getInfo', payload: obj })
    }
  }
}
