import request from '@/utils/request'

const sum = (arr) => arr.reduce((s, x) => s + (x.value || 0), 0)

function countType(url) {
  return request({ url, method: 'get' }).then(res => res.data || [])
}

export function getStats() {
  return Promise.all([
    countType('/slgc/reservoir/countType'),
    countType('/slgc/dike/countType'),
    countType('/slgc/pust/countType'),
    countType('/slgc/waga/countType')
  ]).then(([reservoir, dike, pust, waga]) => ({
    code: 200,
    data: {
      reservoirCount: sum(reservoir),
      dikeCount: sum(dike),
      pustCount: sum(pust),
      wagaCount: sum(waga),
      reservoirCapacity: 0,
      dikeLength: 0,
      totalStations: sum(reservoir) + sum(dike) + sum(pust) + sum(waga)
    }
  }))
}

const categoryMap = [
  { type: 'B', name: '水库', icon: 'water' },
  { type: 'D', name: '堤防', icon: 'trend' },
  { type: 'K', name: '水闸', icon: 'switch' },
  { type: '4', name: '泵站', icon: 'gear' }
]

export function getCategoryList() {
  return Promise.all([
    countType('/slgc/reservoir/countType'),
    countType('/slgc/dike/countType'),
    countType('/slgc/pust/countType'),
    countType('/slgc/waga/countType')
  ]).then((counts) => ({
    code: 200,
    data: categoryMap.map((m, i) => ({ ...m, count: sum(counts[i]) }))
  }))
}

export function getRecentList() {
  return Promise.resolve({ code: 200, data: [], total: 0, rows: [] })
}

export function getReservoirList(params) {
  return request({ url: '/slgc/reservoir/list', method: 'get', params })
}

export function getDikeList(params) {
  return request({ url: '/slgc/dike/list', method: 'get', params })
}

export function getPustList(params) {
  return request({ url: '/slgc/pust/list', method: 'get', params })
}

export function getWagaList(params) {
  return request({ url: '/slgc/waga/list', method: 'get', params })
}
