import {tools} from 'hq-tools'
import lodash from 'lodash';
export default (datas, columns, asyncDicts, vue) => {
  let subtotalObj = {
    _disableExpand:true,
    _disabled:true,
  };
  if (!datas) {
    return datas;
  }
  for (let column of columns) {
    //异步翻译
    if (column.asyncTranslate) {
      let left = new Set();
      for (let data of datas) {
        if ( asyncDicts[column.key] && !asyncDicts[column.key][data[column.key]]) {
          left.add(data[column.key]);
        }
      }
      let leftList = Array.from(left).filter((item) => {
        if (item && tools.trim(item) !== '') {
          return true;
        }
      })
      if (leftList.length > 0) {
        if ( typeof column.asyncTranslate === 'string'){
          vue.$http.post(column.asyncTranslate,
            leftList
          )
          .then(dict => {
            for (let key of Object.keys(dict)) {
              vue.$set(asyncDicts[column.key], key, dict[key])
            }
          })
        }else if ( column.asyncTranslate.length > 0 ) {
          Promise.all(column.asyncTranslate.map(item=>{
            return vue.$http.post(item,leftList)
          }))
          .then(dictList => {
            for ( let dict of dictList){
              for (let key of Object.keys(dict)) {
                vue.$set(asyncDicts[column.key], key, dict[key])
              }
            }
          })
        }
      }
    }
    //小计
    if ( column.subtotal === true  ){
      subtotalObj[column.key] = lodash.sumBy(datas,column.key);
    }
    if ( typeof column.subtotal === 'string' ){
      subtotalObj[column.key] = column.subtotal;
    }
    subtotalObj._subtotal_row = true;
  }
  if ( vue.subtotal ){
    datas.push(subtotalObj);
  }
  return datas;
}
